【问题标题】:Is there a way to wait for another WebDriverIO promise chain to finish?有没有办法等待另一个 WebDriverIO 承诺链完成?
【发布时间】:2015-08-26 02:01:25
【问题描述】:

我正在尝试在 Cucumber 中构建一个测试步骤,该步骤将登录具有给定角色的测试用户。我的方法是尝试查看是否存在“退出”选项并单击它,然后尝试登录。问题在于,promise 结构不能保证首先注销,而且由于我不能假设我总是首先登录,所以我需要一种方法来基本上等到这种情况发生后再继续。

我的测试步骤如下所示:

this.Given(/^I am logged in as a\/an "([^"]*)"$/, function(roleName, callback) {
  console.log('Starting login as ', roleName);
  var myBrowser = this.browser;
  this.browser
    .setViewportSize({width: 1000, height: 600})
    .url(url.resolve(process.env.HOST, '/'));

  this.browser
    .waitForVisible('#main_menu').then(function() {
      console.log('#main_menu is visible' );

      myBrowser.waitForVisible('#appSignOut').then(function() {
        console.log('Logging out now');
        myBrowser.click('#appSignOut');
      });

      myBrowser.waitForVisible('#appSignIn').then(function() {
        console.log('Logging in as ', roleName);
        myBrowser.click('#appSignIn');

        myBrowser.waitForVisible('#username').then(function() {
          myBrowser
            .setValue('#username', 'test' + roleName)
            .setValue('#password', 'test' + roleName)
            .leftClick('#signin')
            .call(callback);
        });
      });
    });
    console.log('Done logging in as ', roleName);
});

有没有办法在myBrowser.waitForVisible('#appSignOut') 之后停止?我只是错过了预期的用途吗?

更新

我尝试了一种新方法,但还是不行:

this.Given(/^I am logged in as a\/an "([^"]*)"$/, function(roleName, callback) {
  var myBrowser = this.browser;
  this.browser
    .setViewportSize({width: 1000, height: 600})
    .url(url.resolve(process.env.HOST, '/'));

  this.browser
    .waitForVisible('#main_menu')
    .then(myBrowser.isVisible('#appSignOut').then(function(isVisible) {
      if (isVisible) {
        myBrowser.click('#appSignOut');
      }
    }))
    .then(myBrowser.waitForVisible('#appSignIn').then(function() {
      myBrowser
        .click('#appSignIn')
        .waitForVisible('#username')
        .setValue('#username', 'test' + roleName)
        .setValue('#password', 'test' + roleName)
        .leftClick('#signin');
    }))
    .call(callback);;
});

这里的逻辑是:

  1. #main_menu 可见时(页面已加载)
  2. 然后 - 如果存在 #appSignOut,请单击它
  3. 然后 - 等待 #appSignIn 变为可见,然后完成登录

我得到的错误是:

[chimp] Detected an unhandledRejection.
[chimp][hooks] Reason:
[chimp][hooks] RuntimeError
[chimp][hooks] no such element

但我认为这些都不起作用。弹出式浏览器的速度太快,我看不到发生了什么,而且 cucumber.log 也没有给我任何关于它在做什么的好的指示。

【问题讨论】:

  • 我很好奇,您是否尝试过直接使用 Meteor.logout() 和 Meteor.loginWithPassword() 函数而不是手动使用 UI?也许使用 waitUntil() webdriver.io/api/utility/waitUntil.html ,当 Meteor.userId() 为空时,注销将解决。当 Meteor.userId() 为非 null 时,登录将在哪里解析。

标签: meteor cucumber webdriver-io meteor-velocity


【解决方案1】:

这是个老问题,希望你从那以后找到答案,但我会回答它可能对其他人有所帮助。

使用 Promise 时,您必须确保在任何地方都使用它们(在大多数情况下,或者您真的希望事情异步运行)。

在您的代码中是:

this.Given(/^I am logged in as a\/an "([^"]*)"$/, function(roleName) {
  return this.browser
    .setViewportSize({width: 1000, height: 600})
    .url(url.resolve(process.env.HOST, '/'))
    .waitForVisible('#main_menu')
    .isVisible('#appSignOut')
    .then(function(isVisible) {
      if (isVisible) {
        return this.click('#appSignOut');
      }
    })
    .waitForVisible('#appSignIn')
    .click('#appSignIn')
    .waitForVisible('#username')
    .setValue('#username', 'test' + roleName)
    .setValue('#password', 'test' + roleName)
    .leftClick('#signin');
});

您会发现它比以前更具可读性。

我改变的关键点:

  • 我将所有内容都链接在一起(webdriverio 允许这样做)
  • 当使用 .Then() this 对应 this.browser 所以你仍然可以链接
  • 通过从 then() 返回 this.click() 的结果,您可以继续链接
  • CucumberJS 可以处理 Promise,为此您需要删除回调参数并返回 Promise。在这种情况下,只需返回 webdriverio API 调用的结果

干杯

【讨论】:

    【解决方案2】:

    我在使用 ChimpJS 使用登录表单自动登录到我的 Meteor 应用程序时遇到问题,我得到“禁止登录”,使用 http://webdriver.io/api/protocol/execute.html 执行 Meteor.loginWithPassword()(由于 ChimpJS 版本的同步性质,请小心WebDriverIO 的不使用 .then()) 正如 looshi 建议的那样为我工作:http://docs.meteor.com/#/full/meteor_loginwithpassword

    【讨论】:

      猜你喜欢
      • 2019-08-19
      • 1970-01-01
      • 2016-10-16
      • 2020-05-26
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      相关资源
      最近更新 更多