当前位置:首页 > Windows程序 > 正文

NightWatch API学习之expect,assert

2024-03-31 Windows程序

NightWatch在0.7版本 新推出一种BDD-style界面 来执行断言,叫做Expect.它以Chai Expect断言库为参考,比传统的asset界面更灵活更高效.

Language Chains

以下命令仅作辅助,提高断言可读性。本身不具有测试能力

to

be

been

is

that

which

and

has

have

with

at

does

of

.equal(value)/.contain(value)/.match(regex)

判断组件是否

值是否与指定值相等
browser.expect.element(‘#main‘).text.to.equal(‘The Night Watch‘);

值是否包含指定值
browser.expect.element(‘#main‘).text.to.contain(‘The Night Watch‘);

是否有指定样式
browser.expect.element(‘#main‘).to.have.css(‘display‘).which.equals(‘block‘);

.not

取反 写在equal,contain,,match前面 text.to.not.equal()

.before(ms)/.after(ms)

在指定的时间再执行断言一次断言,可与任何断言配合使用,使其可以再执行

browser.expect.element(‘#main‘).text.to.contain(‘The Night Watch‘).before(1000);

browser.expect.element(‘#main‘).text.to.not.contain(‘The Night Watch‘).after(500);

.a(type) /an

检查指定组件的类型是否与预期相符。参数与:type,message(optional)

browser.expect.element(‘#q‘).to.be.an(‘input‘);

browser.expect.element(‘#q‘).to.be.an(‘input‘, ‘Testing if #q is an input‘);

browser.expect.element(‘#w‘).to.be.a(‘span‘);

.attribute(name)

检查元素的某个属性是否存在,是否有预期的值(optional)

browser.expect.element(‘body‘).to.have.attribute(‘data-attr‘);

.css

同attribute

browser.expect.element(‘#main‘).to.have.css(‘display‘).which.matches(/some\ value/);

.enabled

检查元素现在是否可用

browser.expect.element(‘#weblogin‘).to.be.enabled;

browser.expect.element(‘#main‘).to.not.be.enabled;

.present

检测元素是否在dom中出现

browser.expect.element(‘#main‘).to.be.present;

.select

检测下拉框,单选框,复选框当前是否被选中

.text

检测组件是否有指定文本/表达式,可与contains,equals,matches配合使用

.text.to.match(/The\ Night\ Watch/)

.value

同text

.to.have.value.which.contains/.not.equals/.that.equals

.visible

是否可见

assert

经典的assert&verify断言库在NightWatch仍然可用.它们作用相同,不同之处在于当前断言失败后assert会跳过之后的断言,结束测试(相当于break);verify会跳过当前断言,输出错误报告,继续执行其他断言(相当于continue).

.attributeContains()

检查某元素给定的属性是否有预期的值
参数:

selector,用来定位元素

attribute,属性名

expected,期望包含的值

message (optional),测试结果中的信息说明

eg. browser.assert.attributeContains(‘#someElement‘, ‘href‘, ‘google.com‘);

.attributeEquals()

同上(attributeContains)

.containsText()

参数:

cssSelector

expectedText

msg(Optional)

eg. browser.assert.containsText("#main", "The Night Watch");

.cssClassPresent()

检查元素是否有指定的CSS类名
参数:

cssSelector,用来定位元素

className,期望有的类名

message (optional),测试结果中的信息说明

eg. browser.assert.cssClassPresent("#main", "container");

.cssClassNotPresent()

楼上(.cssClassPresent)取反

.cssProperty()

检查元素的指定属性是否是期望的状态
参数:

cssSelector,用来定位元素

cssProperty,属性

expected,

message (optional),测试结果中的信息说明

eg. browser.assert.cssProperty("#main", "display", "block");

.elementPresent()

dom中是否含有指定元素

browser.assert.elementPresent("#main");

.elementNotPresent()

楼上取反

.hidden()

检查指定元素是否在页面不可见

browser.assert.hidden(".should_not_be_visible");

.title()

检查页面标题是否是指定标题

browser.assert.title("Nightwatch.js");

.urlContains()

检查当前的URL是否包含指定值

browser.assert.urlContains(‘google‘);

.urlEquals()

browser.assert.urlEquals(‘‘);

.value()

检查指定的表单元素的值 是否与指定值相等

browser.assert.value("form.login input[type=text]", "username");

.valueContains()

browser.assert.valueContains("form.login input[type=text]", "username");

.visible()

browser.assert.visible(".should_be_visible");

Commands

Commands可在页面上执行不同的操作,通常包含两个及以上的页面驱动协议行为.

回调函数

以下的方法均支持回调函数.回调函数将在command完成之后执行

.clearValue()

可清理文本域或文本输入框里的内容

client.clearValue(‘input[type=text]‘);

.click()

模仿点击事件

client.click("#main ul li a.first");

感觉更适合与回调函数配合使用 
browser.click("#main ul li a.first", function(response) { 
this.assert.ok(browser === this, "Check if the context is right."); 
this.assert.ok(typeof response == "object", "We got a response object."); 
});

.closeWindow()

关闭当前窗口,当你使用多个窗口时适用

.deleteCookie()

删除cookie

NightWatch API学习之expect,assert

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/43115.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494595访问次数
  • 建站天数
  • 友情链接