Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java?

With "HTML" Selenium tests (created with Selenium IDE or manually), you can use some very handy commands like WaitForElementPresent or WaitForVisible.

<tr>     <td>waitForElementPresent</td>     <td>id=saveButton</td>     <td></td> </tr> 

When coding Selenium tests in Java (Webdriver / Selenium RC—I'm not sure of the terminology here), is there something similar built-in?

For example, for checking that a dialog (that takes a while to open) is visible...

WebElement dialog = driver.findElement(By.id("reportDialog")); assertTrue(dialog.isDisplayed());  // often fails as it isn't visible *yet* 

What's the cleanest robust way to code such check?

Adding Thread.sleep() calls all over the place would be ugly and fragile, and rolling your own while loops seems pretty clumsy too...

like image 846
Jonik Avatar asked Jun 07 '12 23:06

Jonik


People also ask

Can we automate .NET application using Selenium Java?

We can use Selenium for . NET applications. We should have Visual Studio 2019 installed in the system along with Selenium webdriver and any browser like Firefox, Chrome, and so on. Then we must utilize the NUnit framework.

How does Selenium WebDriver compare two strings in Java?

Syntax: boolean equals(Object obj); equals() method compares two references and returns true only if two references are pointing to same object but in String class equals method compares based on content of the string. If the content is same in two different objects, it returns true.

How does Selenium check background color in Java?

We can verify the color of a webelement in Selenium webdriver using the getCssValue method and then pass color as a parameter to it. This returnsthe color in rgba() format. Next, we have to use the class Color to convert the rgba() format to Hex. Let us obtain the color an element highlighted in the below image.

What is Webdriverwait in Selenium?

It is applied on certain element with defined expected condition and time. This wait is only applied to the specified element. This wait can also throw exception when element is not found. We could avoid throwing exception in Selenium.


1 Answers

Implicit and Explicit Waits

Implicit Wait

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

Explicit Wait + Expected Conditions

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(         ExpectedConditions.visibilityOfElementLocated(By.id("someid"))); 
like image 144
Petr Janeček Avatar answered Sep 17 '22 14:09

Petr Janeček