- Navigate to folder with maven project.
- Run command:
- run all tests from all java classes
mvn test
- run all test from one java class
mvn test -Dtest=LoginTest
- run one test from one java class
mvn test -Dtest=LoginTest#success_login_test
programming space
mvn test
mvn test -Dtest=LoginTest
mvn test -Dtest=LoginTest#success_login_test
Here is example of POM.xml file
<?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>com.example</groupId>
<artifactId>java-selenium-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>25</maven.compiler.source>
<maven.compiler.target>25</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.14.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.38.0</version>
</dependency>
</dependencies>
</project>
This is example of TestBase.java
//TestBase.java
package base;
import java.time.Duration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class TestBase {
public static WebDriver driver;
public static FirefoxOptions options;
@BeforeAll
public static void setup() {
options = new FirefoxOptions();
options.addArguments("--headless");
driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void teardown() {
if (driver != null) {
driver.quit();
}
}
}
//LoginTest.java
package tests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import base.TestBase;
import pages.LoginPage;
public class LoginTest extends TestBase {
@Test
public void success_login_test() {
// arrange
LoginPage loginPage = new LoginPage(driver);
InventoryPage inventory = new InventoryPage(driver);
// act
loginPage.login("standard_user", "secret_sauce");
// assert
assertEquals(true, inventory.inventory_container_is_displayed());
}
}
Here is example of LoginPage.java
//LoginPage.java
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
private WebDriver driver;
private By usernameField = By.id("username");
private By passwordField = By.id("password");
private By loginBtn = By.id("loginBtn");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user, String pass) {
driver.findElement(usernameField).sendKeys(user);
driver.findElement(passwordField).sendKeys(pass);
driver.findElement(loginBtn).click();
}
}
Here is folder structure for Page Object Model
project-root/
├── pom.xml
├── src/
│ ├── main/
│ │ └── java/
│ │ └── pages/
│ │ ├── LoginPage.java
│ │ ├── HomePage.java
│ │ └── BasePage.java
│ └── test/
│ ├── java/
│ │ ├── tests/
│ │ │ ├── LoginTest.java
│ │ │ └── HomeTest.java
│ │ ├── base/
│ │ │ └── TestBase.java
│ │ └── utils/
│ │ └── TestData.java
│ └── resources/
│ ├── config.properties
│ └── testdata.json
└── target/