diff --git a/.gitignore b/.gitignore index a1c2a23..3d7de72 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ *.zip *.tar.gz *.rar +/target/* # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..ce2ba5e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d2fa357..1b2b945 100644 --- a/README.md +++ b/README.md @@ -1 +1,76 @@ -# headless-testing-in-selenium \ No newline at end of file +# Headless-testing-in-selenium + +### Introduction:- +This repository contains a plug and play template for using HtmlUnitDriver for headless execution of the +tests without actually launching the browser. + +### Why do we need Headless testing? + +Using the headless testing we can execute our tests without actually opening the web browser. +This is useful when we integrate our tests with the CI/CD pipelines where the test scripts run automatically without any human intervention. +In this scenario, it is not necessary that the web browser is launched because the CI/CD pipelines are run automatically at the night or in early +the morning when no one is there to see the execution. + +In the headless mode less resources are required to execute the tests. The tests are run normally as they would run on actual browser. + +### Technologies used:- +**Programming language** - Java + +**Assertion framework** - TestNG + +**Build tool** - Maven + +**Automation tool** - Selenium + +**For Headless execution** - htmlunit-driver + +**Logging** - Log4j2 + +**IDE** - Intellij + +###Dependencies used : +**TestNG** + +` +org.testng +testng +RELEASE +test +` + +**HTML unit driver** + +` +org.seleniumhq.selenium +htmlunit-driver +2.47.1 +test +` + +**Selenium chrome driver** + +` +org.seleniumhq.selenium +selenium-chrome-driver +4.0.0-alpha-4 +test +` + +**Log4j** + +` +org.apache.logging.log4j +log4j-core +2.14.0 +` + +### Steps for execution:- +Clone the repository on your local system. + +Let intellij resolve all the required dependencies. + +Go to the terminal the and execute the command **mvn test** to execute all the tests. + +In case of any issue you can also try **mvn clean test**. + + diff --git a/SeleniumHeadlessTestingUsingHTMLUnitDriver.iml b/SeleniumHeadlessTestingUsingHTMLUnitDriver.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/SeleniumHeadlessTestingUsingHTMLUnitDriver.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..789483b --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + org.example + SeleniumHeadlessTestingUsingHTMLUnitDriver + 1.0-SNAPSHOT + + + org.testng + testng + RELEASE + test + + + org.seleniumhq.selenium + htmlunit-driver + 2.47.1 + test + + + org.apache.logging.log4j + log4j-core + 2.14.0 + + + + 8 + 8 + + + \ No newline at end of file diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..0300f9e --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/SeleniumHeadlessTest.java b/src/test/java/SeleniumHeadlessTest.java new file mode 100644 index 0000000..47409f2 --- /dev/null +++ b/src/test/java/SeleniumHeadlessTest.java @@ -0,0 +1,33 @@ +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.testng.annotations.Test; +import java.util.concurrent.TimeUnit; +import org.apache.logging.log4j.*; + +public class SeleniumHeadlessTest { + private static Logger log = LogManager.getLogger(SeleniumHeadlessTest.class); + @Test + public void HeadlessTest() { + WebDriver driver = new HtmlUnitDriver(true); + //Initialize webdriver + driver.get("https://login.salesforce.com/?locale=in"); + //Enter the URL to hit. + log.debug(driver.getTitle()); + //Print the title of the webpage + log.debug(driver.getCurrentUrl()); + //Print the URL of the current webpage + driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); + //Add wait for uninterrupted test execution in case of long load times + driver.findElement(By.cssSelector("#username")).sendKeys("sample@gmail.com"); + //Find and enter text in username field + driver.findElement(By.cssSelector("#password")).sendKeys("password"); + //Find and enter text in password field + driver.findElement(By.cssSelector("#Login")).click(); + //Find and hit login button + log.debug(driver.findElement(By.id("error")).getText()); + //Print error message after unsuccessful login attempt + driver.close(); + //Close the web browser + } +}