js e2e测试-nightwatch入门

nightwatch是基于selenium(webdriver)的e2e测试框架。

selenium还需要java环境。提前安装java环境。

实现效果:自动打开bing页面,搜索内容,关闭页面。

1 安装

mkdir nightwatchDemo

cd nightwatchDemo

npm init -y

npm install nightwatch selenium-server chromedriver --save

2 创建nightwatch.conf.js

const path = require('path');
module.exports = {
  // 测试文件入口
  src_folders: ['tests'],
  // 输出报表路径
  output_folder: 'reports',

  // selenium配置
  selenium: {
    start_process: true,
    server_path: require('selenium-server').path,
    host: '127.0.0.1',
    // selenium log输出
    log_path: 'reports',
    port: 9090,
    cli_args: {
      'webdriver.chrome.driver': require('chromedriver').path,
      'webdriver.gecko.driver': require('chromedriver').path
    }
  },

  test_settings: {
    default: {
      launch_url: 'http://localhost',
      selenium_port: 9090,
      selenium_host: 'localhost',
      silent: true,
      screenshots: {
        enabled: false,
        path: ''
      }
    },

    chrome: {
      desiredCapabilities: {
        browserName: 'chrome',
        javascriptEnabled: true,
        acceptSslCerts: true
      }
    }
  }
};

3 根据配置的src_folders创建tests文件夹,下面创建个demo.js

module.exports = {
  'Find the answer.': function(client) {
    // 定义 Bing 页面中的节点.
    const searchInput = '#sb_form_q';
    const searchBtn = '#sb_form_go';
    const question = '测试nightwatch自动打开网站搜索';

    // 启动浏览器并打开 bing.com.
    client.url('http://bing.com').maximizeWindow();

    // 确保 "body" 和输入框可以使用.
    client.expect.element('body').to.be.present;
    client.expect.element(searchInput).to.be.visible;
    client.pause(2000); // 稍等两秒.

    // 输入 "what is microsoft" 然后搜索.
    client.setValue(searchInput, question);
    client.click(searchBtn);
    client.pause(2000);

    // 截一张图然后保存到 "reports/answer.png".
    client.expect.element('body').to.be.present;
    client.saveScreenshot('reports/answers.png');
    client.end();
  }
};

4 修改package.json的script字段

"scripts": {
    "test": "./node_modules/.bin/nightwatch --env"
 },

5 运行 npm run test

成功完成,输出日志。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值