LayoutTitle

Hoverable Dropdown

Wednesday, February 10, 2021

Headless Browser using Selenium & Java

1. What is Headless Browser?

    Executing the tests on background without viewing the execution on any Browser that you desired to.

2. How Headless Browser helps QA?

    Helps in the executing the tests in a Non-UI mode i.e., Selenium Headless Browser

3. Is Headless Brower supports widely available Browsers like Chrome or Firefox etc?

    Latest versions of SeleniumWebDriver 3 & 4supports headless versions of real browsers                     like Chrome, Firefox, and Edge

 4. Limitations of Headless Browser?

        1. Page load issues

        2. Sync

        3. Tracing the defects of the application

        4. UI validations like page align, background colors etc

5. How Java helps in Headless Browser?

    HtmlUnitDriver is a Java based implementation of Selenium WebDriver based on HtmlUnit, without     a GUI.

6. Headless Browser Maven Dependency!! 

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver -->

        <dependency>

            <groupId>org.seleniumhq.selenium</groupId>

            <artifactId>selenium-htmlunit-driver</artifactId>

            <version>2.52.0</version>

        </dependency>

NOTE: Version may vary based on the selenium version you are using.


7. Ways to implement in the Selenium Code using Java!

    1. Using HtmlUnitDriver, which is the default usage. Refer the Example 1.

    2. Using widely used browsers. Refer the Example 2.


Example 1: Using the HtmlUnitDriver()

    import org.openqa.selenium.htmlunit.HtmlUnitDriver;

     public class headlessBrowserDemo {

     public static void main(String[] args) {        

            HtmlUnitDriver htmlUnitDriver = new HtmlUnitDriver();        

            htmlUnitDriver.get("https://google.com/"); 

           System.out.println("Title of the page is = " + htmlUnitDriver.getTitle());        

        }

    }


Example 2: Using the ChromeDriver()

    import org.openqa.selenium.htmlunit.HtmlUnitDriver;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.chrome.ChromeDriver; 

    public class headlessBrowserDemo {

     public static void main(String[] args) {        

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

    ChromeOptions options = new ChromeOptions();

    options.setHeadless(true);

    WebDriver driver=new ChromeDriver(options);        

            driver.get("https://google.com/"); 

           System.out.println("Title of the page is = " + driver.getTitle());        

        }

    }





Tuesday, February 9, 2021

How to solve Stale Element Reference exception in Selenium Webdriver

What is Stale Element?

Means a Stale element points to any of the GUI Object, which is not present any more.

What is the main Cause?

When an GUI Object was in DOM initially, later point in time during that Object become stale as it might not have present in Document Object Model (DOM).

How to catch stale elements/objects?

Using StaleElementReferenceException. This exception is cuases whenever an element is not present in the DOM, or deleted.

Are there any ways to handle? Yes, below are options to resolve the Stale elements.

1.     Page refresh and verifying again for the same element/object.

2.     Implement using Exception Handling method

3.     Use the point 2 using Iteration/loop statement based on the application behavior

4.     Explicit wait

 

Example for point 1:

       //refresh page

      driver.navigate().refresh();


Example for point 2:

       WebElement staleEle = driver.findElement(By.id("UserName"));
       try{
             staleEle.sendKeys("Selenium");
          }catch(StaleElementReferenceException e){
         staleEle = driver.findElement(By.id("UserName"));
         staleEle.sendKeys("Selenium");
         }


Example for point 3:

       
    for(int i =0;i<=3; i++){
        WebElement staleEle = driver.findElement(By.id("UserName"));
               try{
                     staleEle.sendKeys("Selenium");
                  }catch(StaleElementReferenceException e){
             staleEle = driver.findElement(By.id("UserName"));
             staleEle.sendKeys("Selenium");
         }
}


Example for point 4: 

WebDriverWait wait= new WebDriverWait(driver, 10);

      wait.until(ExpectedConditions.presenceOfElementLocated(By.id("UserName")));