Skip to content

Commit

Permalink
Fixing Implicit wait to pass selenium python tests. Adding tests for …
Browse files Browse the repository at this point in the history
…Implicit waits.
  • Loading branch information
lukeis authored and detro committed Oct 11, 2012
1 parent e9fc774 commit 1e20d12
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/request_handlers/session_request_handler.js
Expand Up @@ -753,10 +753,11 @@ ghostdriver.SessionReqHand = function(session) {
res.success(_session.getId(), _protoParent.getSessionCurrWindow.call(this, _session, req).title);
},

_locateElementCommand = function(req, res, locatorMethod) {
_locateElementCommand = function(req, res, locatorMethod, startTime) {
// Search for a WebElement on the Page
var elementOrElements,
searchStartTime = new Date().getTime(),
searchStartTime = startTime || new Date().getTime(),
stopSearchByTime,
request = {};

// If a "locatorMethod" was not provided, default to "locateElement"
Expand All @@ -771,23 +772,32 @@ ghostdriver.SessionReqHand = function(session) {
}

// Try to find the element
// and retry if "startTime + implicitTimeout" is
// greater (or equal) than current time
do {
elementOrElements = locatorMethod(request);
elementOrElements = locatorMethod(request);

// console.log("Element or Elements: "+JSON.stringify(elementOrElements));
// console.log("Element or Elements: "+JSON.stringify(elementOrElements));

if (elementOrElements &&
elementOrElements.hasOwnProperty("status") &&
elementOrElements.status === 0 &&
elementOrElements.hasOwnProperty("value")) {
if (elementOrElements &&
elementOrElements.hasOwnProperty("status") &&
elementOrElements.status === 0 &&
elementOrElements.hasOwnProperty("value")) {

// return if elements found OR we passed the "stopSearchByTime"
stopSearchByTime = searchStartTime + _session.getTimeout(_session.timeoutNames().IMPLICIT);
if (elementOrElements.value.length !== 0 || new Date().getTime() > stopSearchByTime) {
res.success(_session.getId(), elementOrElements.value);
return;

}
} while(searchStartTime + _session.getTimeout(_session.timeoutNames().IMPLICIT) >= new Date().getTime());
}

// retry if we haven't passed "stopSearchByTime"
stopSearchByTime = searchStartTime + _session.getTimeout(_session.timeoutNames().IMPLICIT);
if (stopSearchByTime >= new Date().getTime()) {
// Recursive call in 50ms
setTimeout(function(){
_locateElementCommand(req, res, locatorMethod, searchStartTime);
}, 50);
return;
}

// Error handler. We got a valid response, but it was an error response.
if (elementOrElements) {
Expand Down
48 changes: 48 additions & 0 deletions test/java/src/test/java/ghostdriver/ElementFindingTest.java
Expand Up @@ -31,6 +31,7 @@
import org.openqa.selenium.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -128,4 +129,51 @@ public void failFindElementsForInvalidXPathLocator() {
d.get("http://www.google.com");
List<WebElement> inputField = d.findElements(By.xpath("this][isnot][valid"));
}

@Test
public void findElementWithImplicitWait() {
WebDriver d = getDriver();

d.get("about:blank");
String injectLink = "document.body.innerHTML = \"<a onclick=\\\"setTimeout(function(){var e=document.createElement('span');e.innerText='test';e.id='testing'+document.body.childNodes.length;document.body.appendChild(e);}, 750)\\\" id='add'>add</a>\"";
((JavascriptExecutor)d).executeScript(injectLink);
d.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
WebElement add = d.findElement(By.id("add"));
add.click();
try {
d.findElement(By.id("testing1"));
throw new RuntimeException("expected NoSuchElementException");
} catch (NoSuchElementException nse) {
}
d.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
add.click();
d.findElement(By.id("testing2"));
d.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);
add.click();
try {
d.findElement(By.id("testing3"));
throw new RuntimeException("expected NoSuchElementException");
} catch (NoSuchElementException nse) {
}
}

@Test
public void findElementsWithImplicitWait() {
WebDriver d = getDriver();

d.get("about:blank");
String injectLink = "document.body.innerHTML = \"<a onclick=\\\"setTimeout(function(){var e=document.createElement('span');e.innerText='test';e.id='testing'+document.body.childNodes.length;document.body.appendChild(e);}, 750)\\\" id='add'>add</a>\"";
((JavascriptExecutor)d).executeScript(injectLink);
d.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
WebElement add = d.findElement(By.id("add"));
add.click();
List<WebElement> spans = d.findElements(By.id("testing1"));
assertEquals(0, spans.size());
((JavascriptExecutor)d).executeScript(injectLink);
add = d.findElement(By.id("add"));
d.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
add.click();
spans = d.findElements(By.tagName("span"));
assertEquals(1, spans.size());
}
}

0 comments on commit 1e20d12

Please sign in to comment.