driver.get多个url导致错误的测试失败

时间:2014-03-11 03:07:26

标签: javascript selenium-webdriver

我开始使用的系统将包含越来越多的单页应用程序,我正在研究如何组织他们的ui测试来进行明智的扩展。我想在每页编写一个测试模块作为练习来验证它,但我在连续调用driver.get()时遇到了一些麻烦。

规格/ index.js

var webdriver = require('selenium-webdriver');
var test = require('selenium-webdriver/testing');
var expect = require('chai').expect;
var server = new require('selenium-webdriver/remote').SeleniumServer(path, {port: 4444});
var specs = [
    require('./google'), require('./bing')
];

//...

var driver = new webdriver.Builder()
         .usingServer(server.address())
         .withCapabilities(webdriver.Capabilities.firefox())
         .build();

test.describe('while learning webdriverjs', function() {
    test.after(function() {
        driver.quit();
    });

    specs.forEach(function(spec) {
        spec.run(driver, webdriver, test);
    });
});

规格/ bing.js

var expect = require('chai').expect;

function run(browser, webdriver, test) {
    test.describe('a trip to bing', function() {
        browser.get('http://www.bing.com');

        test.it('does not fail', function() {
            browser.getTitle().then(function(title) {
               expect(title).to.equal('Bing');
            });
        });
    });
}

module.exports = { run: run };

规格/ google.js

var expect = require('chai').expect;

function run(browser, webdriver, test) {
    test.describe('a trip to google', function() {
        browser.get('http://www.google.com');

        test.it('does not fail', function() {
            browser.getTitle().then(function(title) {
                expect(title).to.equal('Google');
            });
        });
    });
}

module.exports = { run: run };

现在单独运行google.js或bing.js运行正常,但连续运行两个返回:

while learning webdriverjs
    a trip to bing
      1) does not fail
    a trip to google
      ✓ does not fail


1 passing (12s)
1 failing

1) while learning webdriverjs a trip to bing does not fail me:

  + expected - actual

  +"Bing"
  -"Google"

  at <anonymous>
==== async task ====
WebDriver.getTitle()
  at webdriver.WebDriver.schedule (/.../node_modules/selenium-    webdriver/lib/webdriver/webdriver.js:267:15)
  at webdriver.WebDriver.getTitle (/.../node_modules/selenium-webdriver/lib/webdriver/webdriver.js:640:15)
  at /.../projects/learn-webdriver/specs/bing.js:9:11
  at webdriver.promise.ControlFlow.runInNewFrame_ (/.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1445:20)
  at webdriver.promise.ControlFlow.runEventLoop_ (/.../node_modules/selenium-webdriver/lib/webdriver/promise.js:1310:8)
  at wrapper [as _onTimeout] (timers.js:252:14)
  at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
==== async task ====
  at Context.<anonymous> (/.../node_modules/selenium-webdriver/testing/index.js:119:12)
  at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:194:15)
  at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:372:10)
  at /usr/local/lib/node_modules/mocha/lib/runner.js:448:12
  at next (/usr/local/lib/node_modules/mocha/lib/runner.js:297:14)
  at /usr/local/lib/node_modules/mocha/lib/runner.js:307:7
  at next (/usr/local/lib/node_modules/mocha/lib/runner.js:245:23)
  at Object._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:274:5)
  at processImmediate [as _immediateCallback] (timers.js:330:15)

不完全确定我做错了什么,但我确定driver.get()的速度比规格要快。我还注意到使用done()来完成异步测试在selenium-webdriver / testing中是不可用的(我得到了#34; undefined不是函数&#34;当我尝试时)。< / p>

我还尝试过不使用硒包裹的摩卡全局变量,结果很奇怪 - 在我的Win 7企业版上,所有测试都立即通过,然后浏览器被激活,之后失败的任何东西抛出异常;在我的Ubuntu 12.04机器上,所有测试都会立即通过,并且没有任何浏览器实例加载。

我确定我没有用尽可能的方法来组织这些测试,以便他们能够正常运行,但我无法看到它们。

2 个答案:

答案 0 :(得分:0)

您似乎并行运行测试,但是您已经通过相同的驱动程序实例,因此他们将共享浏览器窗口。

我不确定您是否也正确使用了webdriverjs测试内容。我自己没有使用它,但如果有test.describe(...)我希望有test.it(...)

看看one of the test files in web driver他们如何编写测试。

答案 1 :(得分:0)

我崩溃了,决定和protractor一起去。

我的问题主要是组织性的,这解决了它。