Ready-to-use UI Test Automation Architecture using Java and Selenium WebDriver.
In order to use the framework:
- Fork the repository.
- Clone, i.e, download your copy of the repository to your local machine using
git clone https://github.com/[your_username]/selenium-java-test-automation-architecture.git
- Import the project in IntelliJ IDEA.
- Make your desired changes.
- Use IntelliJ IDEA to run your desired tests. Alternatively, you can use the terminal to run the tests, for example
./gradlew test -Dbrowser=firefox -Dheadless=false
to run all the tests using the firefox browser in headful mode. - To see the report, go to the
testoutput
folder in the project root and then go to thereport
folder.
The project uses the following:
- Java 11 as the programming language.
- Selenium WebDriver as the web browser automation framework using the Java binding.
- WebDriverManager as the browser driver management library.
- Univocity Parsers to parse and handle CSV files.
- TestNG as the testing framework.
- AssertJ as the assertion library.
- Lombok to generate getters.
- Owner to minimize the code to handle properties file.
- Extent Reports as the test reporting strategy.
- Selenium Shutterbug for capturing screenshots.
- Gradle as the Java build tool.
- IntelliJ IDEA as the IDE.
The project is structured as follows:
📦 selenium-java-test-automation-architecture
├─ .github
│ ├─ FUNDING.yml
│ └─ workflows
│ └─ test-execution.yml
├─ .gitignore
├─ LICENSE
├─ README.md
├─ build.gradle
├─ gradle
│ └─ wrapper
│ ├─ gradle-wrapper.jar
│ └─ gradle-wrapper.properties
├─ gradlew
├─ gradlew.bat
├─ script
│ └─ install_chrome.sh
├─ settings.gradle
└─ src
├─ main
│ └─ java
│ └─ io
│ └─ github
│ └─ tahanima
│ ├─ config
│ │ ├─ Configuration.java
│ │ └─ ConfigurationManager.java
│ ├─ data
│ │ ├─ BaseData.java
│ │ ├─ LoginData.java
│ │ └─ ProductsData.java
│ ├─ factory
│ │ ├─ BasePageFactory.java
│ │ └─ BrowserFactory.java
│ ├─ report
│ │ └─ ExtentReportManager.java
│ └─ ui
│ ├─ component
│ │ ├─ BaseComponent.java
│ │ ├─ Header.java
│ │ └─ SideNavMenu.java
│ └─ page
│ ├─ BasePage.java
│ ├─ LoginPage.java
│ └─ ProductsPage.java
└─ test
├─ java
│ └─ io
│ └─ github
│ └─ tahanima
│ ├─ e2e
│ │ ├─ BaseE2ETest.java
│ │ ├─ LoginE2ETest.java
│ │ └─ ProductsE2ETest.java
│ └─ util
│ ├─ DataProviderUtil.java
│ ├─ TestListener.java
│ └─ TestRetry.java
└─ resources
├─ config.properties
└─ testdata
├─ login.csv
└─ products.csv
-
The project uses a config.properties file to manage global configurations such as browser type and base url.
-
To add a new property, register a new entry in this file.
key=value
Then, add a method in the Configuration interface in the below format.
@Key("key") dataType key();
For example, let's say I want to add a new property named
context
with the valuedev
. In theconfig.properties
file, I'll add:context=dev
In the
Configuration
interface, I'll add:@Key("context") String context();
To use your newly created property, you need to use the below import statement.
import static io.github.tahanima.config.ConfigurationManager.config;
Then, you can call
config().key()
to retrieve the value of your newly created property. For the example I've provided, I need to callconfig().context()
. -
You can supply the properties present in the
config.properties
file as system properties in your test via gradle../gradlew test -Dkey1=value1 -Dkey2=value2
-
-
The project uses csv file to store test data and univocity-parsers to retrieve the data and map it to a Java bean.
To add configurations for new test data, add a new Java bean in the data package. For example, let's say I want to add test data for a
User
with the attributesFirst Name
andLast Name
. The code for this is as follows:package io.github.tahanima.data; import com.univocity.parsers.annotations.Parsed; import lombok.Getter; import lombok.ToString; @Getter @ToString(callSuper = true) public class UserData extends BaseData { @Parsed(field = "First Name", defaultNullRead = "") private String firstName; @Parsed(field = "Last Name", defaultNullRead = "") private String lastName; }
Note that the class extends from BaseData and thus, inherits the attributes
Test Case ID
andTest Case Description
.Now, in the testdata folder you can add a csv file
user.csv
forUser
with the below contents and use it in your tests.Test Case ID,Test Case Description,First Name,Last Name TC-1,Successful user creation,Tahanima,Chowdhury
-
The project contains the implementation of the Chrome and Firefox browsers. If you want to include an implementation of a new browser type, add the relevant codes in the BrowserFactory enum. Note that the project uses WebDriverManager for auto-handling the download, setup, and maintenance of browser drivers so the setup code should be in the below format.
WebDriverManager.yourdesireddriver().setup();
For example, let's say I want to add the
Edge
browser to theBrowserFactory
enum. The code for this is:EDGE { @Override public WebDriver getDriver() { WebDriverManager.edgedriver().setup(); WebDriver driver = new EdgeDriver(getOptions()); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(config().timeout())); driver.manage().window().maximize(); return driver; } private EdgeOptions getOptions() { EdgeOptions options = new EdgeOptions(); options.setAcceptInsecureCerts(true); if (Boolean.TRUE.equals(config().headless())) { options.addArguments("--headless=new"); } return options; } }
Now, you can launch all your tests in the
Edge
browser by either setting the propertybrowser
toedge
in the config.properties file or as a system property via gradle. -
The project uses Page Objects and Page Component Objects to capture the relevant behaviors of a web page. Check the ui package for reference.
-
The project uses TestNG as the test runner. Check this implementation for reference.