Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selenium template init #1

Merged
merged 26 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

# Idea files
*.iml
.idea/
target/

# Allure files
.allure/
allure-results/
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# jdn-template-selenium
Template project for JDN with Selenium
# Selenium Testng empty template

Empty template for Test Automation project with Selenium

# Instruction:

1. Download template and unpack in appropriate folder

2. Open project in IDE (for example IntelliJIdea)

3. Reporting: Allure is enabled, after running tests just run **allure:serve** in maven plugins (allure should be
installed locally)

4. Parameters that can be changed:

- File src/test/resources/config.properties There are settings to define base url of your site under tests, browser to
define chrome/safari/ie/firefox/edge which driver to use

5. TestNg Retry and before after listeners: You can also modify rules of retry tests (now it is 1 retry for each test)
and actions before/after all tests (now it prints test name and result) in **testng** folder

6. pages generated by JDN should be places in src/main/java/pages package

7. Each page object class must be inherited from BasePage, including one of the mandatory paremeters is PATH describing
the URI of the page e.g. in http://example.com/login PATH = "/login".
126 changes: 126 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<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.epam.jdi</groupId>
<artifactId>jdn-template-selenium</artifactId>
<version>1.0.0</version>
<name>Template: Selenium TestNG Testing project</name>

<properties>
<testng.version>7.8.0</testng.version>
<commons.config.version>2.7</commons.config.version>
<log4j.core.version>2.22.0</log4j.core.version>
<selenium.java.version>3.141.59</selenium.java.version>
<lombok.version>1.18.22</lombok.version>
<webdrivermanager.version>5.6.3</webdrivermanager.version>
<allure.testng.version>2.13.6</allure.testng.version>
<allure.maven.version>2.12.0</allure.maven.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<maven.surefire.plugin.version>2.22.2</maven.surefire.plugin.version>
</properties>

<dependencies>
<!-- Apache Commons Configuration -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>${commons.config.version}</version>
</dependency>
<!-- Log4j2 Dependency -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.core.version}</version>
</dependency>
<!-- Selenium Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.java.version}</version>
</dependency>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<!-- Lombok Dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>test</scope>
</dependency>
<!-- WebDriverManager Dependency -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
<!-- Allure Reporting Dependency -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>${allure.testng.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>2.12.0</version>
<configuration>
<reportVersion>2.13.6</reportVersion>
<properties>
<property>
<name>allure.results.directory</name>
<value>target</value>
</property>
</properties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<systemPropertyVariables>
<allure.results.directory>${project.build.directory}/allure-results</allure.results.directory>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>listener</name>
<value>io.qameta.allure.testng.AllureTestNg</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

62 changes: 62 additions & 0 deletions src/main/java/pages/BasePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pages;

import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import static utilities.ConfigReader.getConfigProperty;

public abstract class BasePage {

public final WebDriver driver;
public final int DEFAULT_TIMEOUT = 30;
protected String baseUrl;
protected String path;

public BasePage(WebDriver driver, String path) {
this.driver = driver;
this.baseUrl = getConfigProperty("baseUrl");
this.path = path;
PageFactory.initElements(driver, this);
}

public void waitUntilElementVisible(WebElement webElement) {
WebDriverWait wait = new WebDriverWait(driver, DEFAULT_TIMEOUT);
wait.until(ExpectedConditions.visibilityOf(webElement));
}

public void waitUntilElementClickable(WebElement webElement) {
WebDriverWait wait = new WebDriverWait(driver, DEFAULT_TIMEOUT);
wait.until(ExpectedConditions.elementToBeClickable(webElement));
}

public void click(WebElement webElement) {
waitUntilElementClickable(webElement);
webElement.click();
}

public WebElement getElement(WebElement webElement) {
waitUntilElementVisible(webElement);
return webElement;
}

public List<WebElement> getElements(List<WebElement> webElements) {
for (WebElement element : webElements) {
waitUntilElementVisible(element);
}
return webElements;
}

public void sendKeys(WebElement webElement, String text) {
waitUntilElementVisible(webElement);
webElement.sendKeys(text);
}

public void open() {
driver.get(baseUrl + this.path);
}
}
16 changes: 16 additions & 0 deletions src/main/java/pages/HomePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

// This is an example of Page Object definition. To be removed
public class HomePage extends BasePage {
private static final String PATH = "/";
@FindBy(css = "p > a")
public WebElement link;

public HomePage(final WebDriver driver) {
super(driver, PATH);
}
}
26 changes: 26 additions & 0 deletions src/main/java/utilities/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utilities;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigReader {

public static String getConfigProperty(String propertyName) {
return getPropertyValue("config.properties", propertyName);
}

private static String getPropertyValue(String propertyFileName, String propertyName) {
String propertyValue = null;

try (InputStream input = new FileInputStream("./src/test/resources/" + propertyFileName)) {
AlexeyGirin marked this conversation as resolved.
Show resolved Hide resolved
Properties prop = new Properties();
prop.load(input);
propertyValue = prop.getProperty(propertyName);
} catch (IOException ex) {
ex.printStackTrace();
}
return propertyValue;
}
}
59 changes: 59 additions & 0 deletions src/main/java/utilities/DriverFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package utilities;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.safari.SafariDriver;

public class DriverFactory {

public static final String CHROME = "chrome";
public static final String FIREFOX = "firefox";
public static final String IE = "ie";
public static final String EDGE = "edge";
public static final String SAFARI = "safari";

public static WebDriver getDriver(String browserName) {
if (browserName == null) {
WebDriverManager.chromedriver()
.setup();
return new ChromeDriver();
}
switch (browserName.toLowerCase()) {
case CHROME:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--window-size=1920x1080");
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-dev-shm-usage");
WebDriverManager.chromedriver()
.setup();
return new ChromeDriver(chromeOptions);
case FIREFOX:
WebDriverManager.firefoxdriver()
.setup();
return new FirefoxDriver();
case IE:
WebDriverManager.iedriver()
.setup();
return new InternetExplorerDriver();
case EDGE:
WebDriverManager.edgedriver()
.setup();
return new EdgeDriver();
case SAFARI:
return new SafariDriver();
default:
WebDriverManager.chromedriver()
.setup();
return new ChromeDriver();
}
}
}
35 changes: 35 additions & 0 deletions src/test/java/test/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import utilities.DriverFactory;

import static utilities.ConfigReader.getConfigProperty;

@Listeners()
public abstract class BaseTest {

protected WebDriver driver;
protected String baseUrl;

@BeforeMethod
public void setUp() {
baseUrl = getConfigProperty("baseUrl");
driver = DriverFactory.getDriver(getConfigProperty("browser"));
driver.manage()
.window()
.maximize();
driver.get(baseUrl);
}

@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}

21 changes: 21 additions & 0 deletions src/test/java/test/ExampleTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test;

import io.qameta.allure.Description;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import pages.HomePage;

public class ExampleTests extends BaseTest {
private HomePage homePage;

@BeforeMethod
public void setUpPage() {
homePage = new HomePage(driver);
}

@Test(description = "Example test")
public void testMethod() {
homePage.open();
homePage.link.click();
}
}
Loading