i

Selenium Step By Step Guide

Handling WebTables, Synchronization issues, Firefoxprofiles – Code

As we discussed that we have various ways to handle the synchronization problems in Selenium automation. Let us see how to implement the same in Selenium scripts one by one:

Wait without condition:

As we are aware of using the Thread class to call the sleep() method and pass some milliseconds value as parameter to stop threads for a specific time. Let us see the small sample code to use this wait in a Selenium script.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class ThreadWait

{

    public static void main( String[] args ) throws InterruptedException

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/");

        Thread.sleep(5000);

        WebElement element=driver.findElement(By.xpath("/html/body/section[1]/div/div/section/div[1]/div[2]/ul/li[2]/a"));     

    }

}

  1. Wait with a condition::
  1. Implicit wait:

The implicit wait is declared before launching the web page URL, and then it remains for the entire session of the driver object. Let us see how to utilize this wait in our Selenium scripts.

package SeleniumTest.SeleniumTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class WaitSelenium

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

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

        driver.get("https://selflearning.io/");

    }

}

  1. Explicit wait:

As we have already discussed that Explicit wait is a wait which is set with a condition and a timeout time. This checks for the condition with a polling frequency during the entire timeout. Let us see the complete sample code to implement this wait in the Selenium script.

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWait

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/");

        WebDriverWait wait=new WebDriverWait(driver,10);

        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='popular-it-skill']/div[5]/a")));

    }

}

  1. Fluent Wait:

We discussed detail about Fluent wait that it gives the option to define the maximum wait time and the polling time in which the WebDriver checks for the condition. This gives an option to set any specific exception to ignore during the polling. Let us see how to define a fluent wait in a Selenium script:

package SeleniumTest.SeleniumTest;

import java.util.NoSuchElementException;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.FluentWait;

import org.openqa.selenium.support.ui.Wait;

import com.google.common.base.Function;

public class FluentWaitSelenium

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\AC42610\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/");

        @SuppressWarnings("deprecation")

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

        .withTimeout(20, TimeUnit.SECONDS)

        .pollingEvery(250, TimeUnit.MILLISECONDS)

        .ignoring(NoSuchElementException.class);

        WebElement element=wait.until(new Function<WebDriver,WebElement>()

        {

        public WebElement apply(WebDriver driver)

        {

               return driver.findElement(By.xpath("//*[@id='popular-it-skill']/div[5]/a"));

        }

        });

    }

}

 

Firefox Profile:

As we have created a Firefox profile in the previous topic and now, we need to see how to use the same in our Selenium script. We can use the “ProfilesIni” class to set the Firefox profile with the Firefox driver object.

 

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxOptions;

import org.openqa.selenium.firefox.FirefoxProfile;

import org.openqa.selenium.firefox.internal.ProfilesIni;

public class FirefoxProfileCreation

{

    public static void main( String[] args )

    {

        ProfilesIni profile = new ProfilesIni();

        FirefoxProfile fp = profile.getProfile("SelflearningSeleniumTest");

        FirefoxOptions opt = new FirefoxOptions();

                                opt.setCapability(FirefoxDriver.PROFILE, fp);  

        WebDriver driver = new FirefoxDriver();

      driver.get("https://selflearning.io/");

}

}