Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,76 @@
# headless-testing-in-selenium
# 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**

`<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>`

**HTML unit driver**

`<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.47.1</version>
<scope>test</scope>
</dependency>`

**Selenium chrome driver**

`<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0-alpha-4</version>
<scope>test</scope>
</dependency>`

**Log4j**

`<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>`

### 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**.


2 changes: 2 additions & 0 deletions SeleniumHeadlessTestingUsingHTMLUnitDriver.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SeleniumHeadlessTestingUsingHTMLUnitDriver</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.47.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
13 changes: 13 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
33 changes: 33 additions & 0 deletions src/test/java/SeleniumHeadlessTest.java
Original file line number Diff line number Diff line change
@@ -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
}
}