Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Is this a promise error? Selenium-webdriver test fails to find element after submitting a successful login

Is this a promise error? Selenium-webdriver test fails to find element after submitting a successful login

Problem

I have written the following test, using selenium-webdriver. (There is also some setup for mocha and chai, but I haven't begun that work, yet.)

My problem is that the test runs up to the point where I want it to wait for the page to load and to find an Element with an ID of 'usergreeting', containing the text 'Welcome, ' + username. At that point, the test throws the error: Uncaught NoSuchElementError: Unable to locate element: {"method":"id","selector":"usergreeting"} Command duration or timeout: 986 milliseconds

However, I can see (watching Firefox go through the steps) that the page has loaded and is displaying the '#usergreeting' element.

I don't know what I have done wrong, but I have obviously done SOMETHING wrong. Probably, I have not grasped the whole concept of promises. (See the section where I have written a driver.wait() statement.)

Thanks for your help.

var assert = require('assert'),
  fs = require('fs'),
  webdriver = require('selenium-webdriver');

require('mocha-as-promised')();
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();

describe('Login test', function() {
  var driver, configObj, username, pwd;
  this.timeout(6000);
  before(function(done) {
    driver = new webdriver
      .Builder()
      .usingServer('http://localhost:4444/wd/hub')
      .withCapabilities(
      {browserName: 'firefox'}
    )
      .build();
    configObj = require('../test/config.json');
    username = configObj.testuser;
    pwd = configObj.testpass;
    done();
  });

  it('User should be able to login', function(done) {
    driver.get('http://127.0.0.1:5207/Login.php');
    driver.findElement({id: 'UserNameEdit'}).sendKeys(username);
    driver.findElement({id: 'PasswordEdit'}).sendKeys(pwd);
    driver.findElement({id: 'LoginBtn'}).click();
    driver.wait(function() {
      return driver.findElement({id: 'usergreeting'}).innerText;
    }, 3000)
      .then(function(value) {
        assert.equal(value, 'Welcome, ' + username);
        done();
      });
    });
});
Problem courtesy of: stackleit

Solution

I think the problem is in your wait statement, which is a polling function, it calls findElement which will return a NO_SUCH_ELEMENT error code, which in webdriverjs is also an assert that the element exists.

You need to call the driver.isElementPresent() function instead, once the element has appeared, then you can call the findElement function to get it, so try something like:

var locator = By.id('usergreeting');

driver.wait(function () {
    return driver.isElementPresent(locator);
}, 3000);

driver.findElement(locator).getText().then(function(txt) {
    assert.equal(txt, 'Welcome, ' + username);
});
Solution courtesy of: jamesk

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Is this a promise error? Selenium-webdriver test fails to find element after submitting a successful login

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×