[英]How do you store the result of getText in a variable to use later on in Nightwatch?
我是 Nightwatch.js 的新手。
我想知道如何將 getText 命令的結果存儲到要在命令之外使用的變量中。
這是我現在的代碼:
var endDate;
client.getText("span.date", function(result){
LOGGER(`INSIDE FXN ${result.value}`);
endDate = result.value;
});
LOGGER(`OUTSIDE FXN ${endDate}`);
結果:
*******INSIDE FXN 3/30/2018*******
*******OUTSIDE FXN UNDEFINED*******
如您所見,我正在嘗試獲取 span.date 中的文本並使用 LOGGER 顯示它(在函數內成功運行)。 一旦我們移出getText的回調function,當我用LOGGER顯示它時,它輸出未定義。
將日期存儲在變量中非常重要,因為我打算在測試元素中使用它(用於 UI 冒煙測試)。
先感謝您!
嗯,我自己無法測試,我只能猜測。
如果我不得不猜測,我推測 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
我的猜測是正確的。
根據文檔
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);
}
}
這意味着你可以.then() 得到你的結果
我的方法是將結果存儲在 browser.globals.variableName 中,
browser.globals.variableName = 'some_value'
然后你可以在守夜人的任何地方使用browser.globals.variableName
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.