[英]How do you store the result of getText in a variable to use later on in Nightwatch?
I'm new to Nightwatch.js.我是 Nightwatch.js 的新手。
I was wondering how I can store the result of the getText command into a variable to be used outside of the command.我想知道如何将 getText 命令的结果存储到要在命令之外使用的变量中。
This is my code now:这是我现在的代码:
var endDate;
client.getText("span.date", function(result){
LOGGER(`INSIDE FXN ${result.value}`);
endDate = result.value;
});
LOGGER(`OUTSIDE FXN ${endDate}`);
Results:结果:
*******INSIDE FXN 3/30/2018*******
*******OUTSIDE FXN UNDEFINED*******
As you can see, I'm trying to get the text in span.date and displaying it with LOGGER (which works successfully inside the function).如您所见,我正在尝试获取 span.date 中的文本并使用 LOGGER 显示它(在函数内成功运行)。 Once we move outside of the callback function of getText, when I display it with LOGGER, it outputs undefined.
一旦我们移出getText的回调function,当我用LOGGER显示它时,它输出未定义。
It's really important that the date is store in a variable because I intend to use it in a test element (for a UI Smoke test).将日期存储在变量中非常重要,因为我打算在测试元素中使用它(用于 UI 冒烟测试)。
Thank you in advance!先感谢您!
Hmm without out being able to test it myself, I'd just have to guess.嗯,我自己无法测试,我只能猜测。
If i had to take a guess, I'd speculate that getText is running after the second LOGGER如果我不得不猜测,我推测 getText 在第二个 LOGGER 之后运行
let result;
function changeResult(){
result = 5;
}
// This is probably when your code is getting logged
console.log(result);
changeResult();
// here is when you would get result
console.log(result);
https://nightwatchjs.org/api/getText.html#apimethod-page https://nightwatchjs.org/api/getText.html#apimethod-page
My guess was correct.我的猜测是正确的。
per the documentation根据文档
module.exports = {
demoTest(browser) {
browser.getText('#main ul li a.first', function(result) {
this.assert.equal(typeof result, 'object);
this.assert.strictEqual(result.status, 0); // only when using Selenium / JSONWire
this.assert.equal(result.value, 'nightwatchjs.org');
});
// with explicit locate strategy
browser.getText('css selector', '#main ul li a.first', function(result) {
console.log('getText result', result.value);
});
// with selector object - see https://nightwatchjs.org/guide#element-properties
browser.getText({
selector: '#main ul li a',
index: 1
}, function(result) {
console.log('getText result', result.value);
});
browser.getText({
selector: '#main ul li a.first',
timeout: 2000 // overwrite the default timeout (in ms) to check if the element is present
}, function(result) {
console.log('getText result', result.value);
});
},
demoTestAsync: async function(browser) {
const result = await browser.getText('#main ul li a.first');
console.log('getText result', result);
}
}
which means you can.then() to get your result这意味着你可以.then() 得到你的结果
My way is storing result in browser.globals.variableName,我的方法是将结果存储在 browser.globals.variableName 中,
browser.globals.variableName = 'some_value'
Then you can use browser.globals.variableName
anywhere in nightwatch.然后你可以在守夜人的任何地方使用
browser.globals.variableName
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.