PhantomJS is a headless web browser scriptable with JavaScript which is similar to HTMLUnitDriver. Click on this link to view the examples of the HTMLUnitDriver.
PhantomJS runs on Windows, macOS, Linux, and FreeBSD. Currently the PhantomJS development is stopped due to less support from the group.
Pros of Headless Browser:
- Headless browser executes the scripts faster then the real browser.
- Helps us in improves the performance execution of the scripts.
Headless browser saves time.
Cons of Headless Browser:
- Headless browser does not mimic the real execution of the test-cases.
- It not possible to view the steps visually during the execution of the test-cases.
Like our Facebook Page to get frequent updates->
https://www.facebook.com/totalqa
Selenium WebDriver Code
1. Add the following dependency in the pom.xml to add the PhantomJS Library to the build path:
1 2 3 4 5 |
<dependency> <groupId>com.github.detro.ghostdriver</groupId> <artifactId>phantomjsdriver</artifactId> <version>1.0.3</version> </dependency> |
2. Instead of the setting the path to the Phantom JS executable. Use the WebDriverManager class ensure the following depedency is added in the maven project object model (pom.xml) file.
1 2 3 4 5 |
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>3.2.0</version> </dependency> |
3. Execute the below code in eclipse and verify the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class GoogleTests { @Test public void verifyTitle() { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("http://www.total-qa.com"); String actual = driver.getTitle(); String expected="Total-QA - Future of Software Testing"; Assert.assertEquals(actual,expected); } } |
Conclusion
Selenium Code executed successfully without launching the Browser.