Selenium
Selenium is one of the most popular and efficient automation tool used today. It helps to perform robust browser-based automation testing.
“Selenium Webdriver” the primary feature in Selenium, provide better support to dynamic web pages where elements of the webpage may change without the page itself being reloaded.
In addition to it, it helps testers to perform “Headless Testing“.
Headless Testing
To test an app rapidly in multiple browsers without any visual interference, Headless Testing came into the picture.
Headless Testing is a term used to define a testing which is performed on Headless Browser which is a browser simulation program that does not have a graphical user interface. And when it is achieved with selenium then it is said to be ‘Headless Testing with Selenium’.
In Headless Browsers when Selenium tests are run, they get executed in the background.
The widely used Headless Browsers available are:
- PhantomJS
- HtmlUnit
- Headless Chrome
- NodeJS
Let us dive into the implementation of Selenium Headless Testing with PhantomJS and HtmlUnit Driver the two widely used headless browsers.
PhantomJS is a headless WebKit scriptable JavaScript API.
Pre-requisites:
- Get the binary executable file of PhantomJS. Download
- PhantomJS driver is itself readily available with the Selenium library, you just need to import it.
Try the below example code:
package HeadlessTesting; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; public class PhantomJS { static WebDriver driver; String phantom_path; @Test public void RunPhantom() throws InterruptedException { phantom_path= System.getProperty("user.dir")+ "/phantomjs"; //Set the path of the phantomjs in the properties System.setProperty("phantomjs.binary.path", phantom_path); //Initialize PhantomJS Driver driver= new PhantomJSDriver(); driver.manage().window().maximize(); //Open desired link driver.navigate().to("https://store.webkul.com/"); //Get & Print the title of navigated page System.out.println("Title of the page is ----> "+driver.getTitle()); } @AfterTest public void closebrowser() { System.out.println("Thank You! for reading the blog"); driver.quit(); } }
HtmlUnitDriver is a headless driver based on HtmlUnit and said to be one of the fastest and light-weight browsers.
Likewise, PhantomJS it is also itself readily available with the Selenium library, so just import it.
Try the below demo code:
package HeadlessTesting; import org.openqa.selenium.WebDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class HtmlUnitDemo { static WebDriver driver; @Test public void HtmlUnitDriverExample() throws InterruptedException { //Initialize HtmlUnitDriver driver = new HtmlUnitDriver(); //Navigate to desired URL driver.navigate().to("https://store.webkul.com/"); //Get & Print the title of navigated page System.out.println("Title of the page is ----> " +driver.getTitle()); } @AfterTest public void closebrowser() { System.out.println("Thank You! for reading the blog"); driver.quit(); } }
This is all about how one can start with Headless Testing with Selenium using PhantomJS and HtmlUnitDriver. Please feel free to share your feedback with us using the comment section.
Thank You! Have a nice day 🙂
Be the first to comment.