300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > nightwatch + selenium 基于浏览器的web自动化测试 教程(二)

nightwatch + selenium 基于浏览器的web自动化测试 教程(二)

时间:2022-10-16 23:53:31

相关推荐

nightwatch + selenium 基于浏览器的web自动化测试 教程(二)

上一篇已经讲述了如何搭建环境,这一片主要针对语法进行

目录

测试案例

Using XPath选择器

测试案例

module.exports = {'Demo test Google' : function (browser) {browser.url('').waitForElementVisible('body', 1000).setValue('input[type=text]', 'nightwatch').waitForElementVisible('button[name=btnG]', 1000).click('button[name=btnG]').pause(1000).assert.containsText('#main', 'Night Watch').end();}};

这里提示下,每个测试最好在结尾执行end(), 这样nigthwatch才会关闭selenium session

上述为nightwatch的一个案例,nigthwatch默认使用css选择器作为定位元素。

在nightwatch里,source目录下的每个js文件为一个测试类,例如如下工程中有两个测试文件,则为两个类

一个文件作为一个测试类,一个测试类可以有多个步骤

module.exports = {'step one' : function (browser) {browser.url('').waitForElementVisible('body', 1000).setValue('input[type=text]', 'nightwatch').waitForElementVisible('button[name=btnG]', 1000)},'step two' : function (browser) {browser.click('button[name=btnG]').pause(1000).assert.containsText('#main', 'Night Watch').end();}};

也可以如下表示

this.demoTestGoogle = function (browser) {browser.url('').waitForElementVisible('body', 1000).setValue('input[type=text]', 'nightwatch').waitForElementVisible('button[name=btnG]', 1000).click('button[name=btnG]').pause(1000).assert.containsText('#main', 'The Night Watch').end();};

Using XPath选择器

默认nightwatch是使用css选择器进行元素定位的,在测试类中使用useXpath()函数进行切换,例如

module.exports = {'get csdn access time' : function (browser) {browser.url('/Viogs').waitForElementVisible('body', 1000).useXpath() //使用xpath函数切换选择器为xpath.getAttribute('//*[@id="asideProfile"]/div[3]/dl[2]/dd', "title", function (res) {console.log("访问次数为"+res.value); //获取博客的访问次数,并且打印出来})browser.end()}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。