Tuesday 13 May 2014

Synchronization in Selenium

Synchronization is machanism for working more than one component working parellel each other.

Generally in test Automation, we can deal with two components,

1) Application under test.
2) Test Automation Tool

The above both componenets are working in different speed. So we can write the script for such a way to maintain same speed for above two components. SO that we will not encounter "Elements not found" error.

Two type of Synchronization

1) Unconditional Synchronization
2) Conditional Synchronization



1) Unconditional Synchronization

-> Here we can mention only time out value.

-> Its tell to the tool waiting for some specify amount of time.

EX: Thread.Sleep(30000)

The tool waitining for 30 sec while using above code.

Advantage : If we want to interact with third party systems like interface, its not possible to check the condition or write the condition. Here in the situation, we tell to the tool waiting for some amount of time by specified timeout value.

Disadvantage : If we specify the timeout value for 30 sec, the tool should waiting 30 sec, eventhough the application ready for 20 sec. So here the executing waiting for 10 sec unnecessarily. This is one of disadvantage for using un-conditional synchronization.




2) Conditional Synchronization

-> We specify a condition along with timeout value, so that tool waits to check for the condition and then come out if nothing happens. If the condition satisfied before timout value, its come proceed next step. So that we can avoid the tool waiting for lone time unnecessarily.

-> we have two condition synchronization

i) implicit synchronization
ii) Explicit Synchronization


i) Implicit synchronization

The implicit statement is used to tell web driver for wait certian time to find the element.

a) Element implicit Synchronization

-> We can set the default element existance timeout. Below statement will set the default object synchronization timeout as 20. This means that selenium script will wait for maximum 20 seconds for element to exist.

->If Web element does not exist within 20 seconds, it will throw an exception.

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

-> This machanism which written once and applied for entire script for find element. Its should defined once initiate the web driver.

-> Implicit wait will not work all the commands/statements in the application. It will work only for "FindElement" and "FindElements" statements.

-> We should always remember to add the below syntax immediately below the Webdriver statement.


b) Page Load Synchronization

-> We can set the default page navigation timeout. Below statement will set the navigation timeout as 50. This means that selenium script will wait for maximum 50 seconds for page to load.

-> If page does not load within 50 seconds, it will throw an exception.

driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);



ii) Explicit Synchronization

-> We need to define the wait statement for certian condition to be satisfied until specified time period. if the webdriver find the element then it will execute.

-> we need to import WebDriverWait class using this statement.


 WebDriverWait wait = new WebDriverWait(driver, 10);

 wait.ignoring(NoSuchElementException.class);

 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("statedropdown")));





Example code

WebDriverWait wait = new WebDriverWait(FireFoxDriver, 10);

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(“list-cnt”)));

visibilityOf(WebElement element) – An expectation for checking that an element, known to be present on the DOM of a page, is visible.

titleIs(java.lang.String title) – An expectation for checking the title of a page.

titleContains(java.lang.String title) - An expectation for checking that the title contains a case-sensitive substring

textToBePresentInElementValue(By locator, java.lang.String text) – An expectation for checking if the given text is present in the specified elements value attribute.

textToBePresentInElement(By locator, java.lang.String text) – An expectation for checking if the given text is present in the specified element.

stalenessOf(WebElement element) – Wait until an element is no longer attached to the DOM.

refreshed(ExpectedCondition<T> condition) – Wrapper for a condition, which allows for elements to update by redrawing.

presenceOfElementLocated(By locator) – An expectation for checking that an element is present on the DOM of a page.

presenceOfAllElementsLocatedBy(By locator) – An expectation for checking that there is at least one element present on a web page.

not(ExpectedCondition<?> condition) - An expectation with the logical opposite condition of the given condition.

invisibilityOfElementWithText(By locator, java.lang.String text) – An expectation for checking that an element with text is either invisible or not present on the DOM.

invisibilityOfElementLocated(By locator) – An expectation for checking that an element is either invisible or not present on the DOM.

frameToBeAvailableAndSwitchToIt(java.lang.String frameLocator) – An expectation for checking whether the given frame is available to switch to.

elementToBeSelected(WebElement element) – An expectation for checking if the given element is selected.

elementToBeSelected(By locator)

elementToBeClickable(By locator) – An expectation for checking an element is visible and enabled such that you can click it.


WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“someid”)));






Fluent Wait:

Using FluentWait we can define the maximum amount of time to wait for a condition, as well as the frequency with which to check for the condition.

And also the user can configure to ignore specific types of exceptions such as "NoSuchElementExceptions" when searching for an element.

Syntax:



Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

//Wait for the condition

      .withTimeout(30, TimeUnit.SECONDS)

        // which to check for the condition with interval of 5 seconds.

      .pollingEvery(5, TimeUnit.SECONDS)

    //Which will ignore the NoSuchElementException

      .ignoring(NoSuchElementException.class);

No comments:

Post a Comment