【发布时间】:2017-08-15 08:16:59
【问题描述】:
更新:问题已解决,请查看评论
我正在尝试将一些经常使用的测试转换为页面对象,并且我有以下 pg.js:
var myCommands ={
security:function(username){
this.click('@logon')
.waitForElementVisible('@id',20000)
.click('@id')
.setValue('@id',username)
.click('@device')
},
password:function(username){
console.log(this)
return this.useXpath()
.navigate()
.assert.elementPresent('@logon', 20000)
.click('@logon')
.waitForElementVisible('@id',20000)
.click('@id')
.setValue('@id',username)
.click('@password')
.waitForElementVisible('//input[@name="Answer"]', 20000);
}
};
module.exports={
url : 'https://mywebsite',
commands :[myCommands],
elements:{
logon:{
locateStrategy: 'xpath',
selector:'//a[@title="Log on"]'
},
id:{
locateStrategy: 'xpath',
selector:'//input[@name="userid"]'
},
device:{
locateStrategy: 'xpath',
selector:'//a[text() = "Login with PIN"]'
},
password:{
locateStrategy: 'xpath',
selector:'//a[text() = "Login with passwords"]'
}
}
};
从 console.log(this) 中,我可以看到会话 ID 和上下文为 null :
在我的 test.js 中,我有这两行:
var logon=client.page.pg()
....
logon.password(username)
当我运行测试时,它显示
Error: Creating screenshot was not successful. Response was:
{ status: -1,
value:
{ error: 'invalid session id',
message: 'No active session with ID null',
stacktrace: '' },
errorStatus: 6,
error: '' }
我的问题是为什么会话为空?如果有任何问题我如何在 pg.js 或 test.js 中设置页面对象。
【问题讨论】:
-
问题解决:在测试中使用页面对象后,需要使用Pageobject.api调用函数,不能做client.whateveritis()。例如
logon.password(username).api.elements(blablabla) -
您能否将您的评论转换为答案并接受它。这样,其他人也可以从您的发现中受益,我们会改进 SO。
标签: selenium cucumber nightwatch.js pageobjects