i

Selenium Step By Step Guide

Synchronization

Synchronization is a process to keep the Test Automation utility in sync with the Application under test (AUT). Test automation tool and AUT may have their speeds to complete the flow. We need to arrange the test automation in such a way to maintain the same speed the same.

Synchronization is an essential part of any automation process to avoid any unwanted failures of the test. Sometimes we see the web pages take some time to get loaded due to some specific reasons, and this leads to failure chances if the test script does not manage this delay time.

Synchronization issues become a critical problem when a large number of test cases get executed in a test suite. Any delay in web element loading leads to "NoSuchElementException" if the desired element is not found on the web page.

We have various ways to handle the synchronization problems in Selenium automation. Selenium provides a rich set of "Wait" options to deal with such a problem. Let us discuss these "Waits" in detail:

  1. Wait without condition:

We make automation script to wait for a certain amount of time, and once the time gets passed, the next test script gets executed. Like we see below how we can achieve specific wait time using Thread class method sleep(waitTimeInms). This method receives a long data type parameter, which is nothing but time in milliseconds.

Thread.sleep(5000);

However, this approach is not recommended in most cases as it leads to unnecessary wait time even if the web page gets loaded before the specified time. But if we have a situation where no condition is available to create a wait, then this approach can be utilized.

  1. Wait with a condition:

There are cases where we need to wait till a condition is matched, and then further code resumes its execution even if the specified wait time is not completed. There are different types of waits in this category, let us discuss them one by one in detail:

  1. Implicit wait:

The implicit wait is a wait that tells the WebDriver to check for the element on DOM for the defined time if the element is not available immediately and needs more time to get loaded.

The default setting of implicit wait is 0; this means the implicit wait remains disabled by default and needs to be set manually enabled. Once this wait is set, this remains in place for the entire life of WebDriver instance.

WebDriver driver=new ChromeDriver();

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

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

 

  1. Explicit wait:

The 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. If the condition is matched before the timeout gets completed, then the wait operation is aborted, and the next available script is executed.

Polling time is the time for which WebDriver looks for the condition; for example, if the total timeout is 20 seconds and polling time is 500 milliseconds. Then every WebDriver checks for a condition in every 500 milliseconds until 20 seconds are not completed. Let us see the syntax for writing the explicit wait:

        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:

The fluent wait 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 below:

WebDriver driver=new ChromeDriver();

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

        @SuppressWarnings("deprecation")

                                Wait wait=new FluentWait(driver)

        .withTimeout(20, TimeUnit.SECONDS)

        .pollingEvery(250, TimeUnit.MILLISECONDS)

        .ignoring(NoSuchElementException.class);

        WebElement element=wait.until(new Function()

        {

        public WebElement apply(WebDriver driver)

        {

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

        }

        });

 

Firefox Profile:

In some situations, the user needs to run tests in a specific configuration of the Firefox browser, where a Firefox profile keeps a set of settings, configuration, add-ons, and other customized settings. But whenever we run automation and we see that the opened browser does not have expected plugins, add-ons and settings available. This can be achieved by setting a profile and then configure it before creating a Firefox driver object.

How to create a Firefox profile?

Click “Win+R” to open “Run” pop-up and enter Firefox.exe -p

 

Firefox “Create Profile” window opens, if this does not work then try to give full path of Firefox.exe file and something like this:

For 32 bit- Win OS: "C:\Program Files\Mozilla Firefox\firefox.exe" -p

For 64 bit Win OS : "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -p

Click on the Create Profile and click on the Next button, then give a profile name and click on the finish button.

This profile does not show Bookmarks and favourite icons in new Firefox window. Once a profile is set, we can use this profile before creating a Firefox driver object in selenium.

We can check the created profiles in below path for Windows OS:

%AppData%\Roaming\Mozilla\Firefox\Profiles\xxxx.default

To use the Firefox profile in Selenium, we need to use “ProfilesIni” class in selenium to set the Firefox profile.

ProfilesIni profile = new ProfilesIni();

Now let’s create an object for Firefox profile

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

We need to create a FirefoxOptions object to set the capabilities of Firefox browser.

FirefoxOptions option = new FirefoxOptions();

option.setCapability(FirefoxDriver.PROFILE, fp); 

Now we can instantiate the FirefoxDriver object.

WebDriver driver = new FirefoxDriver(option)