首页 > 解决方案 > Mocha 与 Selenium 错误:TypeError:等待条件必须是类似 promise 的对象、函数或 Condition 对象

问题描述

我是 Mocha 和 Selenium 测试 Express 应用程序的新手。我有以下简单的代码,但我不知道出了什么问题。

describe("authenticateWithGoogle", function() {
    it("return a valid access token for our tests", function() {
        return new Promise( function(resolve) {

           var driver = new Builder().forBrowser('chrome').build();
           driver.get('https://www.google.com');

           driver.wait("Google",5000).then( (quitDriver,handleFailure) => {
               console.log("wait over");
               assert.ok(true); 
           });

           resolve();
      }).then();
    });
});

运行“mocha”时收到以下错误:

TypeError: Wait condition must be a promise-like object, function, or a Condition object

这发生在上面代码中的“driver.wait”行。我真的不明白错误是什么意思。

标签: node.jsseleniumexpressmocha.js

解决方案


我尝试了相同的 selenium-webdriver 4.0.0 alpha.1 并且它有效。基于它的例子,它使用async await所以我使用相同的方式。

const {Builder, By, Key, until} = require('selenium-webdriver');
const chai = require('chai');
const assert = chai.assert;

describe("authenticateWithGoogle", function() {
  it("return a valid access token for our tests", async function() {
    this.timeout(5000);

    let driver = await new Builder()
      .usingServer('http://localhost:4444/wd/hub')
      .forBrowser('chrome').build();

    try {
      await driver.get('http://www.google.com');
      await driver.wait(until.titleIs('Google'), 1000);
    } finally {
      assert.ok(true);
      await driver.quit();        
    }      
  });
});

我的设置:

是的,对于测试,oauth您可以为其创建集成/e2e 测试。你已经在正确的道路上。

希望能帮助到你


推荐阅读