简体   繁体   中英

selenium-webdriver in nodejs sample not returning current title

I was working through the selenium web-driver example and it didn't work. Several months ago it worked just fine so I am wondering if I am doing something wrong or if the testing methods have changed.

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

var By = webdriver.By;

test.describe('Google Search', function() {
    test.it('should work', function(done) {
        var driver = new webdriver.Builder().
            withCapabilities(webdriver.Capabilities.chrome()).
            build();

        driver.get("http://www.google.com");
        driver.findElement(By.name("q")).sendKeys("webdriver");
        driver.findElement(By.name("btnG")).click();

        driver.getTitle().then(function(title) {
            assert.equal("webdriver - Google Search", title);
            done();
        });

        driver.quit();
    });
});

The output is:

AssertionError: "webdriver - Google Search" == "Google"
Expected :"Google"
Actual   :"webdriver - Google Search"

This tells me that the page has not updated yet but I am not sure why. The example appears here: https://code.google.com/p/selenium/wiki/WebDriverJs#Getting_Started

Selenium version from package.json: 2.39.0

Update

I should have also stated that the test is being run through Mocha. Is Mocha the culprit? When I tried this last time it was using Jasmine.

Straight from the example in the documentation , use wait :

driver.wait(function() {
 return driver.getTitle().then(function(title) {
   return title === 'webdriver - Google Search';
 });
}, 1000);

Why do you need to use wait? Because the Google page works asynchronously. After you enter the keys, it may take a bit of time before the server sends a response and the page is updated.

You should also remove done . While in general you need it for asynchronous tests, it seems the sequencer that comes with this incarnation of Selenium's webdriver will block on quit until all actions are performed. This example in the documentation does not use it.

Also, if you wonder why there is no assertion: you'll know your test has failed if you get a timeout exception when the timeout has expired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM