This class has three methods that do the same thing, they wait three seconds for the page to load and return true if the page has loaded.
My Question is, how do I decide which piece of code is best?
I know that Lambda is preferred above Anon classes, but why is a static nested class (pageLoaded3) not fine? According to this post it should also be suited.
public final class ExpectedConditions {
private ExpectedConditions() {
}
public static ExpectedCondition<Boolean> pageLoaded1() {
return (driver) -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return !((JavascriptExecutor) driver).executeScript("return performance.timing.loadEventEnd", new Object[0])
.equals("0");
};
}
public static ExpectedCondition<Boolean> pageLoaded2() {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return !((JavascriptExecutor) driver)
.executeScript("return performance.timing.loadEventEnd", new Object[0]).equals("0");
}
};
}
public static ExpectedCondition<Boolean> pageLoaded3() {
return new PageLoaded();
}
private static class PageLoaded implements ExpectedCondition<Boolean> {
@Override
public Boolean apply(WebDriver driver) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return !((JavascriptExecutor) driver).executeScript("return performance.timing.loadEventEnd", new Object[0])
.equals("0");
}
}
}
This is the interface of the return type of the three methods:
public interface ExpectedCondition<T> extends Function<WebDriver, T> {}