使用Selenium WebdriverJS与FireFox和IE的问题

时间:2014-05-28 09:01:40

标签: javascript internet-explorer firefox selenium selenium-webdriver

我创建了多个使用chromedriver在Chrome中运行测试的JavaScript测试,现在我想在FireFox和IE中运行相同的测试。 这是我的一项测试,它在Chrome中正常运行,没有任何问题:

var assert = require('assert'),
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver');


test.describe('Click current location button.', function () {
    test.it('Seems to have worked.', function () {
        var driver = new webdriver.Builder().
        withCapabilities(webdriver.Capabilities.chrome()).
        build();
        // Open Chrome (as specified by the Capabilities above) and go to the specified web page
        driver.get('website url').

        then(function () {

            driver.wait(function () {
                console.log("Looking for username");
                return driver.findElement(webdriver.By.id('user_username')).isDisplayed();
            }, 5000, 'Page did not load within 5 seconds');

            driver.findElement(webdriver.By.id("user_username")).sendKeys('user');
            driver.findElement(webdriver.By.id("user_password")).sendKeys('pword');

            return driver.findElement(webdriver.By.id("signIn")).click();
        }).
        then(function () {

            driver.sleep(4000);

            // make sure page has loaded        
            driver.wait(function () {
                console.log("Looking for current button");
                return driver.findElement(webdriver.By.id('gaz_input')).isDisplayed();
            }, 5000, 'Page did not load within 5 seconds');

            // Click Accept cookies to prevent issues 
            if (driver.findElement(webdriver.By.xpath("//a[@class='cc-cookie-accept']")).isDisplayed()) {
                driver.findElement(webdriver.By.xpath("//a[@class='cc-cookie-accept']")).click();
            }
            driver.sleep(1000);

            // Click the current location button
            driver.findElement(webdriver.By.xpath("//button[@class='btn']")).click();

            console.log("Looking for search results");
            driver.manage().timeouts().implicitlyWait(60000);

            if (!driver.findElement(webdriver.By.xpath("//div[@class='panel panel-default']")).isDisplayed()) {
                driver.wait(function () {                   
                    return driver.findElement(webdriver.By.xpath("//div[@class='panel panel-default']")).isDisplayed();
                }, 2000, 'Query did not complete within 60 seconds.');
            }

            driver.sleep(1000);
        }).
        then(function () {
            // Close the browser
            return driver.quit();
        });
    });
});

所以现在我想在FF和IE中运行这个测试。我认为使用FF,我所要做的就是改变功能,它应该可以工作,但这并没有发生。 对于IE我认为我只需要下载IEDriverServer并将其放在与chromedriver相同的文件夹中,但是当我这样做并将驱动程序功能从chrome更改为internetexplorer时 测试对它也不起作用。

如果我在功能设置为internetexplorer时尝试运行测试,则会出现此错误:

C:\Projects\build>mocha ietest.js
.
0 passing (42ms)
1 failing

1) Click current location button. Seems to have worked.:
 TypeError: Object function (opt_other) {

    /** @private {!Object} */
    this.caps_ = {};

    if (opt_other) {
        this.merge(opt_other);
    }
} has no method 'internetexplorer'
at Context.<anonymous> (C:\Projects\build\ietest.js:9:43)
at webdriver.promise.ControlFlow.runInNewFrame_ (C:\Projects\build\node_modules\selenium-webdriver\lib\webdriver\promise.js:1598:20)
at webdriver.promise.ControlFlow.runEventLoop_ (C:\Projects\build\node_modules\selenium-webdriver\lib\webdriver\promise.js:1463:8)
at wrapper [as _onTimeout] (timers.js:252:14)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
==== async task ====
at Context.<anonymous> (C:\Projects\build\node_modules\selenium-webdriver\testing\index.js:126:14)
at Test.Runnable.run (C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:196:15)
at Runner.runTest (C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:374:10)
at C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:452:12
at next (C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:299:14)
at C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:309:7
at next (C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:247:23)
at Object._onImmediate (C:\Users\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)

如果我将功能更改为firefox,我会收到此错误:

C:\Projects\build>mocha ietest.js
  .
  0 passing (1s)
  1 failing
1) Click MyNearest current location button. Seems to have worked.:
Error: ECONNREFUSED connect ECONNREFUSED
at ClientRequest.<anonymous> (C:\Projects\build\node_modules\selenium-webdriver\http\index.js:128:16)
at ClientRequest.EventEmitter.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1547:9)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:415:13)
==== async task ====
WebDriver.createSession()
at Function.webdriver.WebDriver.acquireSession_ (C:\Projects\build\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:131:49)
at Function.webdriver.WebDriver.createSession (C:\Projects\build\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:111:30)
at Builder.build (C:\Projects\build\node_modules\selenium-webdriver\builder.js:106:20)
at Context.<anonymous> (C:\Projects\build\ietest.js:10:3)
at webdriver.promise.ControlFlow.runInNewFrame_ (C:\Projects\build\node_modules\selenium-webdriver\lib\webdriver\promise.js:1598:20)
at webdriver.promise.ControlFlow.runEventLoop_ (C:\Projects\build\node_modules\selenium-webdriver\lib\webdriver\promise.js:1463:8)
at wrapper [as _onTimeout] (timers.js:252:14)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
==== async task ====
at Context.<anonymous> (C:\Projects\build\node_modules\selenium-webdriver\testing\index.js:126:14)
at Test.Runnable.run (C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:196:15)
at Runner.runTest (C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:374:10)
at C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:452:12
at next (C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:299:14)
at C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:309:7
at next (C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:247:23)
at Object._onImmediate (C:\Users\akeogh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)

我一直在争论这个问题很长一段时间没有任何成功,任何帮助真的很值得赞赏。它可能只是一些小的东西,我没有正确地做,但我还没有能够弄明白。

感谢, 安东尼

2 个答案:

答案 0 :(得分:1)

对于Firefox,请参阅https://code.google.com/p/selenium/wiki/WebDriverJs#Using_the_Stand-alone_Selenium_Server中WebDriverJs文档中的代码示例。您需要安装独立的Selenium Server,并在代码示例中使用适当的值填写pathToSeleniumJar。

对于IE,你可以做类似的事情:

var webdriver = require('selenium-webdriver'),
    SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;

var pathToSeleniumJar = 'C:\\selenium-2.41.0\\selenium-server-standalone-2.41.0.jar';

var server = new SeleniumServer(pathToSeleniumJar, {
  port: 4444
});

server.start();

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

这应该可以正常工作,因为IE驱动程序应该已经在您的路径中。另请参阅https://code.google.com/p/selenium/wiki/InternetExplorerDriver以了解您可能需要对IE设置进行的调整。

答案 1 :(得分:0)

我知道自问这个问题已经有一段时间了,但对于遇到这个问题的任何人来说,这可能有所帮助。

看起来Selenium Server还没有为您启动。我等待Server执行代码之前启动服务器。

function startSeleniumServer(callback) {

   seleniumServer = new SeleniumServer(path/to/selenium/server/jar, { port: 4444 });
   var promise = seleniumServer.start();
   promise.then(function() {
      console.log("Selenium Server: Started");
      return callback();
   });
};