繁体   English   中英

您如何将 getText 的结果存储在变量中以供以后在 Nightwatch 中使用?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM