我已经研究过这个问题,但无法找到合适的解决方案。这就是我尝试做的事情(我使用的是夜班v0.9.8和#34; selenium-server-standalone-3.0.0-beta3"):
var servico, dataCriado, a, selector;
module.exports = {
'test case': function(client) {
client
*** do login stuff ***
*** go to a page and then execute this: ***
.execute (function (){
// Get all <a/> tags inside 'listaEtapasServicos':
var estados = document.getElementsByClassName('listaEtapasServicos')[0].getElementsByTagName("a");
// Get all divs inside with the class 'grid-cons-medium' inside 'listaEtapasServicos'
var div = document.getElementsByClassName('listaEtapasServicos')[0].getElementsByClassName('grid-cons-medium');
// What I'm trying to do here is to look for an element containing the text "Suspender" inside a table, save the info inside this specific table line ('Serviço' and 'Data criado') and then click on "Suspender":
for (var i = 0; i < estados.length; i++){
if (estados[i].innerHTML == "Suspender") {
a = i+1;
servico = div[i].getElementsByTagName('span')[0].innerHTML;
dataCriado = div[i].getElementsByTagName('span')[1].innerHTML;
selector = "#div-container > div:nth-child(4) > div > div > div > div:nth-child(2) > div.div-group-info-medium > div > div > div.listaEtapasServicos.scroll-pane-pattern.scroll-pane-medium.ps-container.ps-active-y > div:nth-child("+ a +") > div.grid-padroes-geral.grid-col-100.text-left > a";
break;
}
}
}, [])
// Where the error happens:
.perform(function () {
client.click(selector);
})
*** Test failed ***
错误:找不到元素:&#34; undefined&#34;使用:css选择器
表格的图像:
事情是,守夜人告诉我变量&#39;选择器&#39;未定义,即使我使用命令&#39; alert(selector)&#39;在单击函数之前编写另一个执行程序,警报将完全返回我想要的选择器。这样的问题已在此处指定:https://groups.google.com/forum/#!topic/nightwatchjs/AzRC-Sbgt4Q
但是给定的解决方案并不适用于我。只做&#39; .click(选择器)&#39;也没有工作。有人可以帮我找到解决方案吗?
更新
好的,这里找到了一个解决方法:Nightwatch.js cannot set a string variable to an input field?
然后代码将如下所示:
for (var i = 0; i < estados.length; i++){
if (estados[i].innerHTML == "Suspender") {
a = i+1;
servico = div[i].getElementsByTagName('span')[0].innerHTML;
dataCriado = div[i].getElementsByTagName('span')[1].innerHTML;
selector = "#div-container > div:nth-child(4) > div > div > div > div:nth-child(2) > div.div-group-info-medium > div > div > div.listaEtapasServicos.scroll-pane-pattern.scroll-pane-medium.ps-container.ps-active-y > div:nth-child("+ a +") > div.grid-padroes-geral.grid-col-100.text-left > a";
client.click(selector);
break;
}
}
}, [])
的工作。但后来我的问题是我仍然需要这些变量供以后使用,但是守夜人不会定义它们。如何让守夜人定义我的变量,以便他们不会保持未定义?此外,为什么此警报会显示选择器,因为它是在firts executor中定义的:
.execute [...] (first executor)
.execute(function (){ alert (selector) })
...但是&#39; .perform()&#39;内的点击功能不起作用?
答案 0 :(得分:2)
找到解决方案!
我的问题是我不知道.execute()
函数实际上是如何工作的。它的范围仅限于文档对象,因此如果我希望我的变量保留我希望它们具有的值,我必须通过callback
函数来完成。
此外,client
对象及其步骤在.execute()
函数执行之前全部实例化,此时selector
值仍未定义。这就是需要perform()
功能的原因。代码保持如下:
.execute(function () {
var estados = document.getElementsByClassName('listaEtapasServicos')[0].getElementsByTagName("a");
var div = document.getElementsByClassName('listaEtapasServicos')[0].getElementsByClassName("grid-cons-medium");
for (var i = 0; i < estados.length; i++) {
if (estados[i].innerHTML == "Suspender") {
a = i + 1;
servico = div[i].getElementsByTagName('span')[0].innerHTML;
dataCriado = div[i].getElementsByTagName('span')[1].innerHTML;
selector = "div.grid-cons-medium:nth-child("+a+") > div:nth-child(4) > a:nth-child(1)";
return [a, servico, dataCriado, selector];
}
}
}, [], function (result) {
a = result.value[0];
servico = result.value[1];
dataCriado = result.value[2];
selector = result.value[3];
})
.perform(function(){
client.waitForElementPresent(selector, 10000);
client.click(selector);
})
现在一切正常。