2

Can someone explain clearly what is that Boolean generic type for ExpectedCondition ?

new WebDriverWait(driver, 60).until((ExpectedCondition<Boolean>) wd->((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));

2 Answers 2

1

Boolean is the return type of your lamda expression. In your example, the final line of the javascript .equals("complete") in javascript executor returns boolean value.

For e.g., below examples returns WebElement,

new WebDriverWait(driver, 60).until((ExpectedCondition<WebElement>) wd->((JavascriptExecutor) wd).executeScript("return document.getElementById(someid)"));

new WebDriverWait(driver, 60).until((ExpectedCondition<WebElement>) wd-> wd.findElement(By.id("someid")););
Sign up to request clarification or add additional context in comments.

Comments

1

In the code of ExpectedCondition we can see:

public interface ExpectedCondition<T> extends Function<WebDriver, T> 

That could be read as "Expected condition is a function that always accepts a WebDriver as a parameter and returns value of a generic type T>

In the code of Function we can see:

/**
 * @param `<T>` the type of the input to the function
 * @param `<R>` the type of the result of the function
*/
public interface Function<T, R> 

Your function compares two objects:

1) Object returned by executeScript("return document.readyState"). Actually js document.readyState returns a String value loading/interactive/complete. more info here

2) String "complete"

using method .equals() that returns a booleanvalue:

((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete")

So the return value of your function is a boolean and it should have been passed as a return value type but generics in java do no allow primitives so you have to use Boolean.

Generics in Java are compile-time construct - the compiler turns all generic uses into casts to the right type. Anything that is used as generics has to be convertible to Object and primitive types aren't.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.