简体   繁体   English

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

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

相关问题 如何从 nightwatch 中的 .getText 返回变量? - How to return a variable from .getText in nightwatch? 当用户点击一个按钮时,你如何存储一个以后可以使用的变量? - When a user clicks a button, how do you store a variable that can be used later? 如何将获取数据存储到以后可以访问的全局变量中? - How to store fetch data into a global variable that you can access later? 如何存储变量的内容供以后在其他函数中使用 (Javascript) - How do I store the content of a variable for later use in other function (Javascript) 如何将while循环中生成的值存储到变量中以供以后在JQuery中使用 - How to store a value generated in a while loop to a variable for later use in JQuery 如何将表单中的值存储在变量中以供以后在函数中使用? - How to store the value from a form in a variable for later use in a function? 如何将文件输入内容存储在变量中供以后在 Nodejs 中使用? - How to store file input content in a variable for later use in Nodejs? 我如何在赛普拉斯中存储一个值以供以后在测试中使用? - How Do I Store A Value In Cypress For Later Use Within A Test? 如何存储文本框值供以后与Javascript一起使用? - How do I store text box values for later use with Javascript? 如何将对象存储在临时变量中? - How do you store an object in a temporary variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM