Created
September 29, 2014 17:29
-
-
Save alb-i986/0c54cb1fa8455c136640 to your computer and use it in GitHub Desktop.
Shows a possible bug in selenium-java: looks like there exists a weird inter-dependency between explicit and implicit wait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.TimeoutException; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.support.ui.ExpectedCondition; | |
import org.openqa.selenium.support.ui.ExpectedConditions; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
public class Main { | |
public static void main(String[] args) { | |
List<int[]> data = new ArrayList<>(); | |
// each tuple = (explicit_timeout, implicit_timeout) | |
data.add(new int[] {27, 27}); // => 27 | |
data.add(new int[] {16, 52}); // => 52 | |
data.add(new int[] {52, 16}); // => 65 <-- weird | |
data.add(new int[] {12, 4}); // => 13 <-- weird | |
data.add(new int[] {61, 29}); // => 88 <-- weird | |
Main main = new Main(); | |
System.out.println("explicitWaitTimeout ; implicitWaitTimeout ; actualTimeout"); | |
System.out.println("Testing on CHROME"); | |
for(int[] tuple : data) { | |
main.implicitVsExplicitWaitTest( | |
new ChromeDriver(), tuple[0], tuple[1]); | |
} | |
System.out.println("Testing on FIREFOX"); | |
for(int[] tuple : data) { | |
main.implicitVsExplicitWaitTest( | |
new FirefoxDriver(), tuple[0], tuple[1]); | |
} | |
System.exit(0); | |
} | |
private void implicitVsExplicitWaitTest(WebDriver driver, long explicitWaitTimeout, long implicitWaitTimeout) { | |
int actualTimeout = 0; | |
// set implicit wait | |
driver.manage().timeouts().implicitlyWait(implicitWaitTimeout, TimeUnit.SECONDS); | |
Clock clock = null; | |
try { | |
driver.get("http://www.google.it"); | |
clock = new Clock(); | |
clock.start(); | |
waitUntil( | |
ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#asd")), | |
driver, | |
// set explicit wait | |
explicitWaitTimeout | |
); | |
} catch(TimeoutException e) { | |
actualTimeout = clock.getAndStop(); | |
} finally { | |
System.out.println(explicitWaitTimeout + ";" + implicitWaitTimeout + ";" + actualTimeout); | |
driver.quit(); | |
clock.interrupt(); | |
} | |
} | |
public class Clock extends Thread { | |
private int secondsPassed = 0; | |
@Override | |
public void run() { | |
while(true) { | |
try { | |
Thread.sleep(1000); | |
secondsPassed++; | |
} catch (InterruptedException e) { | |
System.out.println("clock was stopped"); | |
} | |
} | |
} | |
/** | |
* @return how many seconds passed since this clock was started | |
*/ | |
public int getAndStop() { | |
interrupt(); | |
return secondsPassed; | |
} | |
} | |
/** | |
* Generic explicit wait, taking an {@link ExpectedCondition} as a parameter. | |
* Times out after the given number of seconds. | |
* | |
* @param expectedCondition | |
* @param driver | |
* @param timeOutInSeconds | |
* @see WebDriverWait#until(com.google.common.base.Function) | |
* @throws TimeoutException if the timeout expires | |
*/ | |
public static void waitUntil(ExpectedCondition<?> expectedCondition, | |
WebDriver driver, long timeOutInSeconds) { | |
new WebDriverWait(driver, timeOutInSeconds) | |
.until(expectedCondition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment