psugar
psugar

Reputation: 1885

Simple protractor test for isElementPresent failing with unsupported locator strategy

My test:

it('should allow login', function() {
  browser.get('index.html');

  $('#username').sendKeys('administrator');
  $('#password').sendKeys('password');
  $('#login').click();

  var logout = $('#logout');
  expect($p.isElementPresent(logout)).to.eventually.be.true;
}); 

But this errors out with:

Error: Unsupported locator strategy: click
  at Error (<anonymous>)
  at Function.webdriver.Locator.createFromObj (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/locators.js:97:9)
  at Function.webdriver.Locator.checkLocator (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/locators.js:111:33)
  at webdriver.WebDriver.findElements (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:805:31)
  at webdriver.WebDriver.isElementPresent (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:787:29)
  at Protractor.isElementPresent (/usr/local/lib/node_modules/protractor/lib/protractor.js:476:22)
  at /Users/pschuegr/wt/client/e2e/login_test.js:26:15

Strangely, it points to the isElementPresent line, rather than the line with the click. I'm pretty new to webdriver, so apologies if I missed something obvious. I'm running using the mocha framework (which means the canary version of protractor), fwiw.

Any ideas appreciated.

Upvotes: 6

Views: 16180

Answers (4)

Simple-Solution
Simple-Solution

Reputation: 4289

The safest approach I would take is depicted in the following code snippet:

it('should return true when element is present', function () {
 var logout;
 logout = $('#logout');

  browser.driver.isElementPresent(logout).then(function (isPresent) {
    isPresent = (isPresent) ? true : browser.wait(function () {
      return browser.driver.isElementPresent(logout );
    }, 15000); //timeout after 15s 
    expect(isPresent).toBeTruthy();
  });
});

Above code starts of with a promise to check if an element exists, and if true then assign it true, otherwise wait and keep pooling for the next 15sec to see if element is present, and in both cases we expect it to be true.

Upvotes: 0

mmai
mmai

Reputation: 745

This should work :

var logout = $('#logout');
expect(logout.isPresent()).to.eventually.be.true;

Upvotes: -1

Coding Smackdown
Coding Smackdown

Reputation: 1203

Using the latest Protractor build, you can shorten the above answer to the following:

expect(element(by.css('#logout')).isPresent()).toBeTruthy();

This way you do not have to perform the browser.wait and you reduce the number of calls to isElementPresent.

Upvotes: 17

alan.myrvold
alan.myrvold

Reputation: 164

$('#logout') is a WebElement. isElementPresent takes a locator, like by.css

$('#username').sendKeys('administrator');
$('#password').sendKeys('password');
$('#login').click();

var logout = by.css('#logout');
browser.wait(function() { return $p.isElementPresent(logout); }, 8000);
expect($p.isElementPresent(logout)).toBeTruthy();

Upvotes: 5

Related Questions