javascript - 在 webdriver-io 中使用 beforeEach

标签 javascript automated-tests webdriver-io

我想知道如何在 webdriver-io 中使用 beforeEach。我正在测试 CMS,希望能够在每次测试之前登录,并认为 beforeEach 会很好,但这不起作用。终端显示出现错误并且找不到该元素。我基本上希望它每次都能让我登录,而不必编写 browser.login()。

browser.addCommand('login', function () {
    return this
        .url('/admin/index.php')
        .setValue('[name="username"]', 'foo')
        .setValue('[name="password"]', 'bar')
        .click('[name="login"]');
});

这是我的登录命令。

beforeEach(function(){
     browser.login();
});

这是我的 beforeEach。

我也尝试过返回函数调用。

最佳答案

您可以将登录步骤保存为帮助方法。

module.exports = function *() {
  // steps required for successful login
}

然后调用这个帮助器方法,例如

'use strict';

import loginHelper from '../path/login_helper.js';

describe ('...', () => {
  beforeEach(function *() {
    yield* loginHelper();
  });

  it('...', function *() {
    // steps
  });
});

关于javascript - 在 webdriver-io 中使用 beforeEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33986243/

相关文章:

javascript - 尝试在 WebdriverIO 中断言 getText 时出错

javascript - 如何使用 wdio.conf.js?

javascript - 为什么我们在react native router flux中需要一个 "root"的场景

javascript - 如何从 iOS 中保存为应用程序的页面打开新页面?

javascript - 如何通过单击第一页中的按钮来激活 Bootstrap 4 模式,将您重定向到激活模式的第二页?

testing - 如何在嵌入式系统中进行回归测试

testing - 如何使用katalon studio上传文件/照片?

javascript - 重用 react 组件

testing - Thread.Sleep() 和 selenium.setSpeed ("2000"之间有什么区别?

webdriver-io - 网络驱动.io : what's the difference between the capabilities and desiredCapabilities keywords?