Commit 4c3b9bc8 authored by Đỗ Gia Hưng's avatar Đỗ Gia Hưng

Hoan thien Bai 7, 9, 10 va fix loi hang driver

parent 53c51bee
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.version>4.19.1</selenium.version> <selenium.version>4.21.0</selenium.version>
<testng.version>7.9.0</testng.version> <testng.version>7.9.0</testng.version>
</properties> </properties>
...@@ -32,5 +32,19 @@ ...@@ -32,5 +32,19 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>
...@@ -2,6 +2,7 @@ package com.automation.testcases; ...@@ -2,6 +2,7 @@ package com.automation.testcases;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
...@@ -15,7 +16,11 @@ public class BasicWebDriverCommandsTest { ...@@ -15,7 +16,11 @@ public class BasicWebDriverCommandsTest {
@BeforeMethod @BeforeMethod
public void setUp() { public void setUp() {
// Khởi tạo WebDriver // Khởi tạo WebDriver
driver = new ChromeDriver(); ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(options);
// --- 4. Thiết lập thời gian chờ (Wait Configurations) --- // --- 4. Thiết lập thời gian chờ (Wait Configurations) ---
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
......
package com.automation.testcases; package com.automation.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.*; import org.testng.annotations.*;
import java.time.Duration;
public class TestNGAnnotationsTest { public class TestNGAnnotationsTest {
WebDriver driver;
@BeforeSuite @BeforeSuite
public void beforeSuite() { public void beforeSuite() {
System.out.println(">>> @BeforeSuite: Chay 1 lan duy nhat truoc toan bo Suite"); System.out.println(">>> @BeforeSuite: Chay 1 lan duy nhat truoc toan bo Suite");
...@@ -22,35 +29,47 @@ public class TestNGAnnotationsTest { ...@@ -22,35 +29,47 @@ public class TestNGAnnotationsTest {
@BeforeMethod @BeforeMethod
public void beforeMethod() { public void beforeMethod() {
System.out.println("- @BeforeMethod: Chay truoc moi phuong thuc @Test"); System.out.println("- @BeforeMethod: Khoi tao trinh duyet truoc moi test case");
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
// Neu bi loi memory, hay bo comment dong duoi day de chay an (headless)
// options.addArguments("--headless");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
} }
// Bai 10: Su dung thuoc tinh priority de quan ly thu tu chay (so nho chay truoc) // Bai 10: Su dung thuoc tinh priority de quan ly thu tu chay (so nho chay
// description dung de mo ta test case giup bao cao de doc hon // truoc)
@Test(priority = 1, description = "Kiem tra tieu de trang web Anh Tester") @Test(priority = 1, description = "Kiem tra tieu de trang web Anh Tester")
public void testCase1() { public void testCase1() {
System.out.println("Executing Test Case 1: Kiem tra tieu de"); System.out.println("Executing Test Case 1: Mo trang chu va kiem tra tieu de");
driver.get("https://anhtester.com");
String expectedTitle = "Anh Tester"; String expectedTitle = "Anh Tester";
String actualTitle = "Anh Tester"; String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle, "Tieu de khong khop!"); Assert.assertTrue(actualTitle.contains(expectedTitle), "Tieu de khong chua tu khoa mong muon!");
} }
@Test(priority = 2, description = "Kiem tra trang thai hien thi cua phan tu") @Test(priority = 2, description = "Kiem tra URL trang Blog")
public void testCase2() { public void testCase2() {
System.out.println("Executing Test Case 2: Kiem tra hien thi"); System.out.println("Executing Test Case 2: Mo trang Blog va kiem tra URL");
boolean isDisplayed = true; driver.get("https://anhtester.com/blog");
Assert.assertTrue(isDisplayed, "Phan tu khong hien thi!"); String currentUrl = driver.getCurrentUrl();
Assert.assertTrue(currentUrl.contains("blog"), "URL khong chua tu khoa 'blog'!");
} }
// Bai 10: Su dung enabled = false de bo qua (skip) mot test case khong muon chay @Test(priority = 3, enabled = false, description = "Test case nay bi disable nen se khong chay")
@Test(priority = 3, enabled = false, description = "Test case nay dang bi loi nen tam thoi bo qua")
public void testCase3() { public void testCase3() {
System.out.println("Test case nay se khong bao gio duoc chay"); System.out.println("Test case nay se khong bao gio duoc chay");
} }
@AfterMethod @AfterMethod
public void afterMethod() { public void afterMethod() {
System.out.println("- @AfterMethod: Chay sau moi phuong thuc @Test"); System.out.println("- @AfterMethod: Dong trinh duyet sau moi test case");
if (driver != null) {
driver.quit();
}
} }
@AfterClass @AfterClass
......
...@@ -5,6 +5,7 @@ import org.openqa.selenium.JavascriptExecutor; ...@@ -5,6 +5,7 @@ import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
...@@ -19,7 +20,11 @@ public class WebElementCommandsTest { ...@@ -19,7 +20,11 @@ public class WebElementCommandsTest {
@BeforeMethod @BeforeMethod
public void setUp() { public void setUp() {
driver = new ChromeDriver(); ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(options);
driver.manage().window().maximize(); driver.manage().window().maximize();
// Cài đặt chờ ngầm định 10s cho việc tìm kiếm element // Cài đặt chờ ngầm định 10s cho việc tìm kiếm element
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
...@@ -47,7 +52,8 @@ public class WebElementCommandsTest { ...@@ -47,7 +52,8 @@ public class WebElementCommandsTest {
// 2. NÂNG CAO: DÙNG EXPLICIT WAIT (Chờ đợi thông minh) // 2. NÂNG CAO: DÙNG EXPLICIT WAIT (Chờ đợi thông minh)
// Đợi cho đến khi nút Login sẵn sàng để click (tối đa đợi 10s) // Đợi cho đến khi nút Login sẵn sàng để click (tối đa đợi 10s)
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement buttonLogin = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='submit']"))); WebElement buttonLogin = wait
.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='submit']")));
// 3. NÂNG CAO: DÙNG JAVASCRIPT ĐỂ CLICK NGẦM (Xử lý khi bị che khuất) // 3. NÂNG CAO: DÙNG JAVASCRIPT ĐỂ CLICK NGẦM (Xử lý khi bị che khuất)
// Thông thường bạn chỉ cần gõ: buttonLogin.click(); // Thông thường bạn chỉ cần gõ: buttonLogin.click();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment