What is "Synchronization" in selenium || How we can achieved synchronization in our Testing || What is "Waits" - Types || Example of Synchronization

Synchronization

 

What is Synchronization?

If you are driving a car and don't apply the accelerator, breaks, clutch as per the needs, can you drive smoothly? No. in the same way while testing an application, if we don't perform actions on various elements in a synch we can not perform the automated test smoothly. 

Being an test script writer we have to ensure that all our operation are in perfect synch to avoid exceptions in selenium. 

Fun Fact: PlayWright has in built waits to perform the operations and thus using playwright can give you an edge while testing the application. 

  • Synchronization is achieving the coordination between the WebElements
  • Synchronization helps us avoid exceptions in selenium such an no such element exception by adding the waits.

Types of Waits

  • UnConditional Waits 
  • Conditional Waits

Types of UnConditional Waits

When we add waits without any predefined conditions, its called unconditional waits. For Instance:

  • driver.sleep
  • driver.wait

Types of Conditional Waits

When we add waits based on predefined conditions or parameters, its called conditional waits. For Instance:

    • Implicate Wait
    • Explicate Wait
    • Fluent Wait.

    Implicate Wait

    • This type of wait is only applicable to FindElement or FindElements methods in Selenium.
    • Implicate Wait is use after we initiate the driver. 

      • For instance

    Public class ImpliciteWait{

    static void wait(WebDriver driver) {

    driver = new ChromeDriver();

    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

     }

    }


    Note: Even though we set a wait for 10 seconds, selenium will not wait if we find the 

    element before 10 seconds.


    If selenium is unable to find the element in specified time, it will give us timeout Exception

    Explicate Wait


    Explicate wait help us add waits based on Expected conditions
    Here we use ExpectedConditions Class and its various methods to add conditions in the selenium.

    Code Base: 


    static WebDriver driver = null;

    static void wait(WebDriver driver) {

    driver= new ChromeDriver();

    String UserName = null;

    WebElement MyUserName = driver.findElement(By.name(UserName));

    WebDriverWait Mywaits = new WebDriverWait(driver, Duration.ofSeconds(20));

    Mywaits.until(ExpectedConditions.elementToBeClickable(MyUserName));

       }

    }

    Fluent Wait.

    Fluent wait help us in adding the polling for wait. This means if we add a code to wait for 30 Seconds and then we add polling for 10 then it will check after every 10 seconds till 30 seconds if the element is visible or not :

    Code Base: 

    Public class Fluentait{

    static void wait(WebDriver driver) {

    driver = new ChromeDriver();

    WebDriverWait MyFluentWait = new WebDriverWait(driver, Duration.ofSeconds(30));

    MyFluentWait.pollingEvery(Duration.ofSeconds(10));

     }

    }


      

    Comments