0

The WebDriverJS API says I can write code like this:

driver.get("http://www.google.com");
driver.findElement(webdriver.By.name("q")).sendKeys("webdriver");
driver.findElement(webdriver.By.name("btnG")).click();
driver.getTitle().then(function(title) {
 console.log(title);
 //assertEquals("webdriver - Google Search", title);
});

Here title is "Google". I can modify the code:

driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("webdriver");
driver.findElement(By.name("btnG")).click().then(function(){
  /*WebDriverWait.until(function() {
    driver.getTitle().then(function(title) {
      console.log(title);
      title = "webdriver - Google Search"
    });
  });*/

  driver.sleep(3000).then(function() {
    driver.getTitle().then(function(title) {
      console.log(title);
    });
  });
});

And now the title is "webdriver - Google Search". The promise changes status before the page has a chance to load. This example comes from the API documentation so it should work.

I saw an example of waiting on SO, however I do not not know to translate this to working code in WebDriverJS and the API makes no mention of "until".

Community
  • 1
  • 1
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

1 Answers1

0

You can see how to use wait and until in the source code here: https://code.google.com/p/selenium/source/browse/javascript/webdriver/webdriver.js#653

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
  return driver.getTitle().then(function(title) {
    console.log(title);
    return title === 'webdriver - Google Search';
  });
}, 1000).then(function() {
  console.log('done!')
});

driver.quit();

Gives the output:

webdriver - Google Search
done!
olan
  • 3,568
  • 5
  • 24
  • 30
  • I don't see `Webdriver.prototype.until` defined in the link. I am unable to get this to function in any way that solves the problem. The code simply tries once and fails out. – P.Brian.Mackey Apr 13 '15 at 15:41
  • 1
    Sorry - original was overly complex, you can just wait on the promise from `getTitle.then` based on the following from the docs: > Each call to then() will return a new promise that represents the observer's result. If the callback (or errback) returns a value (no return is treated as returning undefined), the promise will be resolved. It wont resolve unless your return is true, so it will time out after trying for 1000ms. – olan Apr 13 '15 at 15:52
  • The documentation should say "If the callback (or errback) returns a **truthy** value (no return is treated as returning undefined), the promise will be resolved." The truthy value causes the next `then` in the sequence to execute. – P.Brian.Mackey Apr 13 '15 at 16:05
  • Note that this answer shows me how to get the effect of a `wait` without actually using `wait`. I still do not know if `wait` is exposed on the WebDriverJS API. – P.Brian.Mackey Apr 13 '15 at 19:36