E2E测试神器nightwatch.js使用

由于测试资源紧张,以及人工测试覆盖率有限。为了规避因改动代码而产生新的bug,最近项目要加自动化测试。经过组内有经验的大哥介绍。推荐了今天的主角nightwatch.js

官网是这么介绍的:

  Nightwatch.js is an automated testing framework for web applications and websites, written in Node.js and using the W3C WebDriver API (formerly Selenium WebDriver).

It is a complete End-to-End testing solution which aims to simplify writing automated tests and setting up Continuous Integration. Nightwatch can also be used for writing Node.js unit and integration tests.

大概意思是:Nightwatch.js是一个用于Web应用程序和网站的自动化测试框架,使用Node.js编写并使用W3C WebDriver API(以前称为Selenium WebDriver)。

它是一个完整的浏览器(端到端)测试解决方案,旨在简化设置持续集成和编写自动化测试的过程。 Nightwatch也可用于编写Node.js单元测试。

首先要安装相关依赖

npm install nightwatch -D

npm install chromedriver -D

npm install selenium-server -D  (目前我没有用到)
项目目录如下

 

新建nightwatch.conf.js 配置设置信息
自己按照官网通过nightwatch.json设置时,在windows上总是提示找不到server_path文件路径,故改写。
相关配置根据自己需求对照官网配置信息配置即可。这里chromeOptions.args若设置--headless是不会打开浏览器的。(我这里多了ss是可以打开的)
const chromedriverPath = require('chromedriver').path
module.exports = {
  "src_folders" : ["test/e2e/specs"],

  "webdriver" : {
    "start_process": true,
    "server_path": chromedriverPath,
    "port": 9515,
    "log_path" : "test/e2e/reports"
  },

  "test_settings" : {
    "default" : {
      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions": {
          "args": [
            "--headlessss"
          ]
        }
      }
    }
  }
}

测试代码test/e2e/specs目录下的***_specs.js。可支持多种的断言风格。从 Nightwatch0.7 版本开始, 引入了新的 BDD风格的断言库,大大提升了灵活性和代码的可读性。 
expect 断言是 chai 框架 expect api 的子集,在此对元素类型也能使用。我仅仅用百度首页做个测试例子。

const testUrl = 'https://www.baidu.com/'
module.exports = {
  'test for baidu' : function (browser) {
    browser
      .url(testUrl)
      .waitForElementVisible('body', 3000)
      .setValue('input[id=kw]', 'nightwatch')
      .waitForElementVisible('input[type=submit]', 1000)
      .click('input[type=submit]')
      .pause(1000)
      .waitForElementVisible('div[class=s_tab_inner]', 3000)
      .assert.containsText('.s_tab_inner', '网页')
      .end();
  }
};

启动测试 packae.json

 "scripts": {
    "test": "nightwatch -c  test/e2e/nightwatch.conf.js",
    "build": "node build/build.js"
  },

 在项目根目录下执行npm test即可。它会模拟界面操作,打开浏览器执行。控制台信息如下。

 源码git地址:https://github.com/shichangchun404/nightWatchTest

 

 

转载于:https://www.cnblogs.com/shichangchun/p/10553275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3支持E2E测试。在Vue 3中进行E2E测试的一种常见方法是使用Nightwatch.jsNightwatch.js是一个基于Node.js的自动化测试框架,可以用于编写端到端(E2E测试。 要在Vue 3中进行E2E测试,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Nightwatch.js。你可以使用npm全局安装Nightwatch.js:npm install -g nightwatch。 2. 在你的Vue 3项目中创建一个spec文件夹,用于存放测试文件。 3. 在spec文件夹中创建一个.js文件,例如first.js。在这个文件中,你可以编写你的E2E测试代码。 4. 在测试文件中,你可以使用Nightwatch.js提供的API来编写测试用例。例如,你可以使用.browser.url()方法来访问你的Vue应用的URL,使用.assert.elementPresent()方法来断言页面上是否存在某个元素,使用.assert.containsText()方法来断言页面上是否包含特定的文本等等。你可以根据你的需求编写自己的测试用例。 5. 运行E2E测试。你可以使用命令nightwatch来运行Nightwatch.js测试Nightwatch.js会自动查找你的spec文件夹中的测试文件,并执行其中的测试用例。 总结起来,要在Vue 3中进行E2E测试,你需要安装Nightwatch.js,并在你的项目中创建一个spec文件夹,然后在其中编写测试文件。你可以使用Nightwatch.js提供的API来编写测试用例,并使用nightwatch命令来运行测试。这样就可以进行Vue 3的E2E测试了。[1][2]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值