夜间观看正确的方法以获取API结果填充变量,然后运行使用该变量的测试



我是节点的新手和做事的异步方法。

我想创建并运行一个使用nightwatch.js的测试套件,我已经阅读了所有文档,我对如何做我想做的事情感到困惑(已经工作了3天)。

我是错误的方式吗?

module.exports = {
    before: function(browser) {
        /*
        Here I just want to make a web call to an api and get a result and then
        store that result in a variable which we will use later in test1 and other test cases
        */
        browser.globals.myVariable = resultofsomeapicalll;
        //wait here until proceeding
    },
    after: function(browser) {
        browser.end();
    },
    beforeEach: function(browser) {
    },
    afterEach: function() {
    },
    'test1': function(browser) {
        browser.url(browser.launchUrl + browser.globals.myVariable, function(result) {
            browser.waitForElementPresent('body', 1000);
            browser.expect.element("#something").to.be.present;
            browser.saveScreenshot('./screenshots/' + browser.currentTest.module + '/' + browser.currentTest.name + '.png');
        });
    },
};

在[每个]或[每个]挂钩之前或之后,在nightwatch.js中执行异步任务。

在下面的示例中,这将是使用Axios库的API调用;

module.exports = {
    before: function(browser, done) {
        axios.get('https://example.com/api?ID=12345')
          .then(function (response) {
            browser.globals.myVariable = response;
            done();
          })
          .catch(function (error) {
            done(error);
          });
    },
    after: function(browser) {
        browser.end();
    },
    beforeEach: function(browser) {
    },
    afterEach: function() {
    },
    'test1': function(browser) {
        console.log()
    },
};

控制完成的调用超时

默认情况下,已完成的调用超时设置为10秒(2秒 用于单位测试)。在某些情况下,这可能不够 避免超时错误,您可以通过定义一个超时 外部全球群体中的AsynchookTimeOut属性(以毫秒为单位) 文件(有关外部Globals的详细信息,请参见下文)。

http://nightwatchjs.org/guide/#asynchronous-before-each-and-after-each--

最好的问候

riku

最新更新