Skip to content

Commit

Permalink
Update lesson 26 design driver in POM and WebUI keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Feb 22, 2023
1 parent 523ec7c commit adc5ce3
Show file tree
Hide file tree
Showing 24 changed files with 1,148 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
.idea/
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# SeleniumTestNG092022Parallel
Source code khoá học Selenium Java khoá 09/2022 nội dung Parallel Execution từ bài 25 đến hết.
## ♻️ Selenium TestNG 09/2022 POM + Parallel Execution
Source code khoá học Selenium Java khoá 09/2022 nội dung Page Object Model + Parallel Execution từ bài 25 đến hết.

Source code Selenium Maven từ bài 5 đến bài 8: https://github.com/anhtester/SeleniumMaven092022
Source code Selenium Maven từ bài 9 đến bài 16: https://github.com/anhtester/SeleniumTestNG092022
Source code Selenium Maven từ bài 17 đến bài 25: https://github.com/anhtester/SeleniumTestNG092022POM

Source code hướng dẫn bài tập: https://github.com/anhtester/HuongDanBaiTap092022

🔅 Khoá học Selenium WebDriver with Java Basic to Advanced | Anh Tester

https://anhtester.com/course/selenium-webdriver-with-java-basic-to-advanced-c4.html

![alt text](https://anhtester.com/uploads/logo/logo_anh_tester_github_v3.jpg)
58 changes: 58 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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>anhtester.com</groupId>
<artifactId>anhtester-selenium-java-092022</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.6</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
24 changes: 24 additions & 0 deletions src/main/java/anhtester/com/drivers/DriverManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package anhtester.com.drivers;

import org.openqa.selenium.WebDriver;

public class DriverManager {

private static final ThreadLocal<WebDriver> driver = new ThreadLocal<>();

private DriverManager() {
}

public static WebDriver getDriver() {
return driver.get();
}

public static void setDriver(WebDriver driver) {
DriverManager.driver.set(driver);
}

public static void quit() {
DriverManager.driver.get().quit();
driver.remove();
}
}
231 changes: 231 additions & 0 deletions src/main/java/anhtester/com/keywords/WebUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package anhtester.com.keywords;

import anhtester.com.drivers.DriverManager;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import java.time.Duration;
import java.util.List;

//import static anhtester.com.drivers.DriverManager.getDriver;
import static anhtester.com.drivers.DriverManager.*;

public class WebUI {

private static int EXPLICIT_WAIT_TIMEOUT = 10;
private static int WAIT_PAGE_LEADED_TIMEOUT = 30;

public static WebElement getWebElement(By by) {
return getDriver().findElement(by);
}

public static void logConsole(String message) {
System.out.println(message);
}

public static void hoverOnElement(By by) {
waitForElementVisible(by);
Actions action = new Actions(getDriver());
action.moveToElement(getWebElement(by));
logConsole("Hover on element " + by);
}

public static WebElement highLightElement(By by) {
waitForElementVisible(by);
// Tô màu border ngoài chính element chỉ định - màu đỏ (có thể đổi màu khác)
if (getDriver() instanceof JavascriptExecutor) {
((JavascriptExecutor) getDriver()).executeScript("arguments[0].style.border='3px solid red'", getWebElement(by));
sleep(1);
}
return getWebElement(by);
}

public static void rightClickElement(By by) {
waitForElementVisible(by);
Actions action = new Actions(getDriver());
action.contextClick(getWebElement(by));
logConsole("Right click on element " + by);
}

public static void openURL(String URL) {
getDriver().get(URL);
waitForPageLoaded();
logConsole("Open URL: " + URL);
}

public static String getCurrentUrl() {
waitForPageLoaded();
logConsole("Get Current URL: " + getDriver().getCurrentUrl());
return getDriver().getCurrentUrl();
}

public static void clickElement(By by) {
waitForElementVisible(by);
highLightElement(by);
getWebElement(by).click();
logConsole("Click on element " + by);
//Report
}

public static void setText(By by, String value) {
waitForElementVisible(by);
getWebElement(by).sendKeys(value);
logConsole("Set text " + value + " on element " + by);
}

public static String getTextElement(By by) {
waitForElementVisible(by);
logConsole("Get text of element " + by);
logConsole("==> Text: " + getWebElement(by).getText());
return getWebElement(by).getText();
}

public static String getAttributeElement(By by, String attributeName) {
waitForElementVisible(by);
logConsole("Get attribute value of element " + by);
logConsole("==> Attribute value: " + getWebElement(by).getAttribute(attributeName));
return getWebElement(by).getAttribute(attributeName);
}

public static void scrollToElementWithJS(By by) {
waitForElementPresent(by);
//Dùng Actions class
//Robot class
//Dùng JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("arguments[0].scrollIntoView(true);", getWebElement(by));
logConsole("Scroll to element " + by);
}

public static void scrollToElement(By by) {
//Dùng Actions class

}

public static void scrollToElementWithRobot(By by) {
//Dùng Robot class

}

public static void sleep(double second) {
try {
Thread.sleep((long) (1000 * second));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

public static void waitForElementVisible(By by, int second) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(second), Duration.ofMillis(500));

wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}

public static void waitForElementVisible(By by) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(EXPLICIT_WAIT_TIMEOUT), Duration.ofMillis(500));

wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}

public static void waitForElementPresent(By by, int second) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(second));

wait.until(ExpectedConditions.presenceOfElementLocated(by));
}

public static void waitForElementPresent(By by) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(EXPLICIT_WAIT_TIMEOUT));

wait.until(ExpectedConditions.presenceOfElementLocated(by));
}

public static void waitForElementClickable(By by, int second) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(second));

wait.until(ExpectedConditions.elementToBeClickable(by));
}

public static boolean verifyElementVisible(By by, int second) {
try {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(second), Duration.ofMillis(500));
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
return true;
} catch (TimeoutException e) {
e.printStackTrace();
return false;
}

}

public static boolean verifyElementNotVisible(By by, int second) {
try {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(second), Duration.ofMillis(500));
wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
return true;
} catch (TimeoutException e) {
e.printStackTrace();
return false;
}
}

public static boolean checkElementExist(By by) {
List<WebElement> listElement = getDriver().findElements(by);

if (listElement.size() > 0) {
System.out.println("Element " + by + " existing.");
return true;
} else {
System.out.println("Element " + by + " NOT exist.");
return false;
}
}

public static Boolean checkElementExist(String xpath) {
List<WebElement> listElement = getDriver().findElements(By.xpath(xpath));

if (listElement.size() > 0) {
System.out.println("Element " + xpath + " existing.");
return true;
} else {
System.out.println("Element " + xpath + " NOT exist.");
return false;
}
}

/**
* Wait for Page loaded
* Chờ đợi trang tải xong (Javascript tải xong)
*/
public static void waitForPageLoaded() {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(WAIT_PAGE_LEADED_TIMEOUT), Duration.ofMillis(500));
JavascriptExecutor js = (JavascriptExecutor) getDriver();

//Wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return js.executeScript("return document.readyState").toString().equals("complete");
}
};

//Check JS is Ready
boolean jsReady = js.executeScript("return document.readyState").toString().equals("complete");

//Wait Javascript until it is Ready!
if (!jsReady) {
System.out.println("Javascript is NOT Ready.");
//Wait for Javascript to load
try {
wait.until(jsLoad);
} catch (Throwable error) {
error.printStackTrace();
Assert.fail("FAILED. Timeout waiting for page load.");
}
}
}

}
Loading

0 comments on commit adc5ce3

Please sign in to comment.