Thought of the Day!!!!!!!

Don't make EXCUSES, make IMPROVEMENTS. - Tyra Banks

Search This Blog

Showing posts with label Xpath. Show all posts
Showing posts with label Xpath. Show all posts

Thursday, July 27, 2017

How to perform HEADLESS TESTING using HtmlUnitDriver in Selenium?

Question: How to perform Headless Testing using HtmlUnitDriver in Selenium?

Answer: 
Basically, headless testing is, running a script in the background without any visual of the browser. In simple words, this process doesn't need any browser. This method can be used on the machines which don't have any browser installed on them.
This technique has been used for simple websites to get data or input from them without interfering to the user in a quick and easy way, as the user doesn't have to wait for launching the browser or loading the URL etc.

Example: 

STEPS: 
  1. Create an object of HtmlUnitDriver.
  2. Launch above mentioned URL using the driver.
  3. Getting Window Title after launching the URL.
  4. Getting Post Content (Website Content)
Selenium CODE:

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.Test;


public class HtmlUnitExample
{
    @Test(description="Headless Testing using HtmlUnitDriver")
    public void getPostContent() throws InterruptedException, IOException
    {
        // To turn off all the HTMLUnit warnings from console output.
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
       
        System.out.println("Headless Testing using HtmlUnitDriver Execution Start");
       
        WebDriver driver=new HtmlUnitDriver();                               // Step #1
        driver.get("https://seleniumqna.blogspot.in/");                           // Step #2
        System.out.println("Window Title : "+driver.getTitle());             // Step #3
        By locator_post=By.xpath("//*[contains(text(),'Question:')]/../..");
       
       List<WebElement> postList=driver.findElements(locator_post);
        for (WebElement postData : postList)
        {
            System.out.println(postData.getText());                               // Step #4
        }
   
        driver.quit();
    }

}
 

Conclusion:
The user can observe the difference of the time and speed of running the same script on any other browser such as Chrome, Firefox etc. and on the headless browser using HtmlUnitDriver.

Disadvantages:
1. We can't use this technique for complex websites. 
2. HtmlUnitDriver doesn't support screenshot functionality. The PhantomJS Driver supports screenshots functionality as well. Will share the POST for the same very soon.
3. Debugging of the script is also too complex as compared to non-headless technique.

Please do comment and share the post with your friends and colleagues. For any query or question, you can also mail us at ashu.kumar940@gmail.com.

Other Blogs:

https://agilehelpdoc.blogspot.in/

Sunday, July 02, 2017

How to access Login Screen of TimesJobs which opens in a pop-up window?

 

Question: How to access Login Screen of timesjobs.com which opens in a pop-up window?


URL:

Screen:
1. Navigate to above URL.
2. Click on SIGN IN link present on the top-right corner of the web page.

Screenshot:
Login Screen of timesjobs.com
Login Screen















Problem: 
Able to extract xpath of Login ID text field, Password text field, and SIGN IN button, but unable to access these components while running the script in Selenium web driver.

Please do comment and share the post with your friends and colleagues. For any query or question, you can also mail us at ashu.kumar@magicsw.com.