Skip to content

Commit f982f63

Browse files
committed
initial
0 parents  commit f982f63

14 files changed

+565
-0
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
replay_pid*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample framework for https://www.saucedemo.com/

pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.borb</groupId>
8+
<artifactId>selenium-automation-example</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.seleniumhq.selenium</groupId>
19+
<artifactId>selenium-java</artifactId>
20+
<version>4.12.0</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.testng</groupId>
24+
<artifactId>testng</artifactId>
25+
<version>7.8.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>commons-io</groupId>
29+
<artifactId>commons-io</artifactId>
30+
<version>2.11.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>ch.qos.logback</groupId>
34+
<artifactId>logback-core</artifactId>
35+
<version>1.4.12</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-api</artifactId>
40+
<version>2.0.9</version>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.borb.driver;
2+
3+
import org.openqa.selenium.WebDriver;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.testng.ISuite;
7+
import org.testng.ISuiteListener;
8+
import org.testng.ITestContext;
9+
import org.testng.ITestListener;
10+
import org.testng.ITestResult;
11+
12+
public class DriverCreatorListener implements ITestListener, ISuiteListener {
13+
14+
private static final Logger LOGGER = LoggerFactory.getLogger(DriverCreatorListener.class);
15+
private long startTime;
16+
17+
/**
18+
* This method is invoked at the start of the test suite execution.
19+
*
20+
* @param suite {@link ISuite}
21+
*/
22+
@Override
23+
public void onStart(ISuite suite) {
24+
startTime = System.currentTimeMillis();
25+
}
26+
27+
/**
28+
* This method is invoked at the end of the test suite execution.
29+
*
30+
* @param suite {@link ISuite}
31+
*/
32+
@Override
33+
public void onFinish(ISuite suite) {
34+
double finishTime = (System.currentTimeMillis() - startTime);
35+
LOGGER.info("Suite execution took {} ms", finishTime);
36+
}
37+
38+
/**
39+
* This method is invoked at the start of the test class execution. It initializes the ThreadLocal
40+
* {@link WebDriver} instance.
41+
*
42+
* @param context {@link ITestContext}
43+
*/
44+
@Override
45+
public void onStart(ITestContext context) {
46+
// new PropertyReader();
47+
WebDriver driver = DriverFactory.getDriver();
48+
ThreadLocalDriverManager.setDriver(driver);
49+
}
50+
51+
/**
52+
* This method is invoked at the end of the test class execution. It closes the ThreadLocal {@link
53+
* WebDriver} instance.
54+
*
55+
* @param context {@link ITestContext}
56+
*/
57+
@Override
58+
public void onFinish(ITestContext context) {
59+
WebDriver driver = ThreadLocalDriverManager.getDriver();
60+
if (driver != null) {
61+
LOGGER.info("Closing the browser.");
62+
driver.quit();
63+
}
64+
}
65+
66+
@Override
67+
public void onTestStart(ITestResult result) {
68+
//no action required for test start
69+
}
70+
71+
@Override
72+
public void onTestSuccess(ITestResult result) {
73+
//no action required for test success
74+
}
75+
76+
@Override
77+
public void onTestFailure(ITestResult result) {
78+
//no action required for test failure
79+
}
80+
81+
@Override
82+
public void onTestSkipped(ITestResult result) {
83+
//no action required for test skipped
84+
}
85+
86+
@Override
87+
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
88+
//no action required for test failure
89+
}
90+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.borb.driver;
2+
3+
import org.openqa.selenium.WebDriver;
4+
import org.openqa.selenium.chrome.ChromeDriver;
5+
import org.openqa.selenium.chrome.ChromeDriverService;
6+
import org.openqa.selenium.edge.EdgeDriver;
7+
import org.openqa.selenium.edge.EdgeDriverService;
8+
import org.openqa.selenium.firefox.FirefoxDriver;
9+
import org.openqa.selenium.firefox.GeckoDriverService;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class DriverFactory {
14+
15+
private static final Logger LOGGER = LoggerFactory.getLogger(DriverFactory.class);
16+
17+
private static final String BROWSER_FROM_BUILD_ARG = System.getProperty("browser");
18+
private static final String CHROME_DRIVER_PATH = "src/main/resources/drivers/chromedriver.exe";
19+
private static final String GECKO_DRIVER_PATH = "src/main/resources/drivers/geckodriver.exe";
20+
private static final String EDGE_DRIVER_PATH = "src/main/resources/drivers/MicrosoftEdgeDriver.exe";
21+
22+
private DriverFactory() {
23+
//utility class
24+
}
25+
26+
/**
27+
* Create {@link WebDriver} instance according to the browser type parameter.
28+
*
29+
* @param browserType the browser to start
30+
* @return the specific {@link WebDriver} instance
31+
*/
32+
public static WebDriver getDriver(String browserType) {
33+
WebDriver driver;
34+
35+
switch (browserType) {
36+
case "chrome":
37+
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, CHROME_DRIVER_PATH);
38+
driver = new ChromeDriver();
39+
break;
40+
case "firefox":
41+
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY, GECKO_DRIVER_PATH);
42+
driver = new FirefoxDriver();
43+
break;
44+
case "edge":
45+
System.setProperty(EdgeDriverService.EDGE_DRIVER_EXE_PROPERTY, EDGE_DRIVER_PATH);
46+
driver = new EdgeDriver();
47+
break;
48+
default:
49+
String errorMessage = "Specify browser type";
50+
LOGGER.error(errorMessage);
51+
throw new IllegalArgumentException(errorMessage);
52+
}
53+
LOGGER.info("Initializing {} driver.", browserType);
54+
return driver;
55+
}
56+
57+
public static WebDriver getDriver() {
58+
return getDriver("chrome");
59+
}
60+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.borb.driver;
2+
3+
import java.time.Duration;
4+
import java.util.List;
5+
import java.util.concurrent.TimeUnit;
6+
7+
import org.openqa.selenium.NoSuchElementException;
8+
import org.openqa.selenium.StaleElementReferenceException;
9+
import org.openqa.selenium.WebDriver;
10+
import org.openqa.selenium.WebElement;
11+
import org.openqa.selenium.support.ui.ExpectedConditions;
12+
import org.openqa.selenium.support.ui.WebDriverWait;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
public final class ElementFinder {
17+
18+
private static final Logger LOGGER = LoggerFactory.getLogger(ElementFinder.class);
19+
private static final long DEFAULT_WAIT_TIME = 10;
20+
21+
private ElementFinder() {
22+
//utility class
23+
}
24+
25+
/**
26+
* A wrapper method to wait for the {@link WebElement} to be displayed.
27+
*
28+
* @param driver the {@link WebDriver} instance
29+
* @param element the {@link WebElement} to wait for
30+
* @return the {@link WebElement} if displayed
31+
* @see ExpectedConditions#visibilityOf(WebElement)
32+
*/
33+
public static WebElement waitForElementDisplayed(WebDriver driver, WebElement element) {
34+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500L));
35+
wait.until(ExpectedConditions.visibilityOf(element));
36+
return element;
37+
}
38+
39+
40+
/**
41+
* A wrapper method to wait for the {@link WebElement} to be displayed.
42+
*
43+
* @param driver the {@link WebDriver} instance
44+
* @param elements a {@link List} of {@link WebElement} to wait for
45+
* @return a {@link List} of {@link WebElement} if displayed
46+
* @see ExpectedConditions#visibilityOfAllElements(List)
47+
*/
48+
public static List<WebElement> waitForElementsDisplayed(WebDriver driver,
49+
List<WebElement> elements) {
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500L));
51+
wait.until(ExpectedConditions.visibilityOfAllElements(elements));
52+
return elements;
53+
}
54+
55+
/**
56+
* A wrapper method to wait for the {@link WebElement} to be clickable.
57+
*
58+
* @param driver the {@link WebDriver} instance
59+
* @param element the {@link WebElement} to wait for
60+
* @return the {@link WebElement} if clickable
61+
* @see ExpectedConditions#visibilityOf(WebElement)
62+
*/
63+
public static WebElement waitForElementClickable(WebDriver driver, WebElement element) {
64+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500L));
65+
wait.until(ExpectedConditions.elementToBeClickable(element));
66+
return element;
67+
}
68+
69+
/**
70+
* Checks if the {@link WebElement} is displayed and catches the {@link NoSuchElementException} or
71+
* the {@link StaleElementReferenceException} if the element not displayed.
72+
*
73+
* @param element the {@link WebElement} to check the presence of
74+
* @return true if the {@link WebElement} displayed
75+
*/
76+
public static boolean isElementDisplayed(WebElement element) {
77+
boolean isDisplayed = false;
78+
try {
79+
isDisplayed = element.isDisplayed();
80+
} catch (NoSuchElementException | StaleElementReferenceException e) {
81+
LOGGER.debug("Element is not displayed");
82+
}
83+
return isDisplayed;
84+
}
85+
86+
/**
87+
* Wait the specified time in seconds.
88+
*
89+
* @param timeInSec wait time
90+
*/
91+
public static void waitForTimeElapsed(int timeInSec) {
92+
try {
93+
Thread.sleep(TimeUnit.SECONDS.toMillis(timeInSec));
94+
} catch (InterruptedException e) {
95+
LOGGER.error(e.getMessage());
96+
Thread.currentThread().interrupt();
97+
}
98+
}
99+
100+
/**
101+
* Wait until the given element list is disappeared.
102+
*
103+
* @param element is the element list to be disappeared.
104+
* @return true if the element list is disappeared.
105+
*/
106+
public boolean areElementsDisappeared(WebDriver driver, List<WebElement> element, int timeInSec) {
107+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500L));
108+
return wait.until(ExpectedConditions.invisibilityOfAllElements(element));
109+
}
110+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.borb.driver;
2+
3+
import org.openqa.selenium.WebDriver;
4+
5+
public final class ThreadLocalDriverManager {
6+
7+
private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
8+
9+
private ThreadLocalDriverManager() {
10+
//utility class
11+
}
12+
13+
public static WebDriver getDriver() {
14+
return driver.get();
15+
}
16+
17+
static void setDriver(WebDriver webDriver) {
18+
driver.set(webDriver);
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.borb.pageobjects;
2+
3+
import org.openqa.selenium.WebDriver;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
public abstract class BasePage {
8+
9+
private static final Logger LOGGER = LoggerFactory.getLogger(BasePage.class);
10+
11+
private WebDriver driver;
12+
13+
BasePage(WebDriver driver) {
14+
this.driver = driver;
15+
}
16+
17+
/**
18+
* Get the {@link WebDriver} instance and maximize the browser window.
19+
* @return the {@link WebDriver} instance
20+
*/
21+
public WebDriver getDriver() {
22+
driver.manage().window().maximize();
23+
return driver;
24+
}
25+
26+
/**
27+
* Launch the specified page.
28+
* @param url the URL of the page to be opened.
29+
*/
30+
void launchPage(String url) {
31+
LOGGER.info("Starting browser.");
32+
LOGGER.info("Maximizing browser window.");
33+
LOGGER.info("Launching page: {}", url);
34+
getDriver().get(url);
35+
}
36+
}

0 commit comments

Comments
 (0)