Commit 28a39a29 authored by Đỗ Gia Hưng's avatar Đỗ Gia Hưng

Add Lesson 11 (Assertions) and Lesson 12 (Actions) tests

parent 75c06269
......@@ -12,7 +12,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.version>4.21.0</selenium.version>
<selenium.version>4.24.0</selenium.version>
<testng.version>7.9.0</testng.version>
</properties>
......
package com.automation.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;
public class HandleActionsTest {
WebDriver driver;
@BeforeMethod
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test(priority = 1, description = "Test Mouse Hover - Di chuột qua menu")
public void testMouseHover() {
driver.get("https://anhtester.com");
// Khởi tạo Actions class
Actions actions = new Actions(driver);
// Tìm element menu "Khoá học" (Dùng 'oá' thay vì 'óa' cho đúng font của web)
WebElement menuCourses = driver.findElement(By.xpath("//a[contains(., 'Khoá học')]"));
// Thực hiện di chuột (Hover)
actions.moveToElement(menuCourses).perform();
System.out.println("Da thuc hien hover vao menu Khoa hoc");
// Kiểm tra xem menu con "Website Testing" có hiển thị không
WebElement websiteTesting = driver.findElement(By.xpath("//a[contains(., 'Website Testing')]"));
Assert.assertTrue(websiteTesting.isDisplayed(), "Menu con Website Testing khong hien thi sau khi hover!");
}
@Test(priority = 2, description = "Test Right Click & Double Click")
public void testClickActions() {
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
Actions actions = new Actions(driver);
// 1. Right Click (Chuột phải)
WebElement rightClickBtn = driver.findElement(By.xpath("//span[contains(text(),'right click me')]"));
actions.contextClick(rightClickBtn).perform();
// Kiểm tra xem menu chuột phải có hiện ra không
WebElement copyItem = driver.findElement(By.xpath("//span[contains(text(),'Copy')]"));
Assert.assertTrue(copyItem.isDisplayed(), "Menu chuot phai khong hien thi!");
// Click phím ESC để đóng menu chuột phải
actions.sendKeys(Keys.ESCAPE).perform();
// 2. Double Click (Click đúp)
WebElement doubleClickBtn = driver.findElement(By.xpath("//button[contains(text(),'Double-Click Me To See Alert')]"));
actions.doubleClick(doubleClickBtn).perform();
// Chờ và kiểm tra Alert xuất hiện
driver.switchTo().alert().accept();
System.out.println("Da thuc hien Double Click va dong Alert");
}
@Test(priority = 3, description = "Test Drag and Drop - Kéo và thả")
public void testDragAndDrop() {
driver.get("https://demo.guru99.com/test/drag_drop.html");
Actions actions = new Actions(driver);
// Tìm phần tử nguồn (Source) và đích (Target)
WebElement source = driver.findElement(By.xpath("//li[@id='fourth']//a[contains(text(),'5000')]"));
WebElement target = driver.findElement(By.xpath("//ol[@id='amt7']//li[@class='placeholder']"));
// Cách 1: Dùng hàm dragAndDrop()
actions.dragAndDrop(source, target).perform();
// Cách 2: Dùng clickAndHold -> moveToElement -> release
// actions.clickAndHold(source).moveToElement(target).release().build().perform();
System.out.println("Da thuc hien keo tha");
// Kiểm tra kết quả kéo thả
WebElement result = driver.findElement(By.xpath("//ol[@id='amt7']//li[contains(text(),'5000')]"));
Assert.assertTrue(result.isDisplayed(), "Keo tha khong thanh cong!");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
package com.automation.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import java.time.Duration;
public class TestNGAssertionsTest {
WebDriver driver;
@BeforeMethod
public void setUp() {
System.out.println("--- Khoi tao trinh duyet ---");
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
// Chạy ẩn để tiết kiệm RAM nếu cần
// options.addArguments("--headless");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test(priority = 1, description = "Test Hard Assert - Kiem tra title va header")
public void testHardAssert() {
System.out.println("Executing testHardAssert...");
driver.get("https://anhtester.com");
// 1. Kiểm tra Tiêu đề trang (Title) - Dùng Hard Assert
String expectedTitle = "Anh Tester";
String actualTitle = driver.getTitle();
System.out.println("Kiem tra Title cua trang...");
// Cú pháp: Assert.assertEquals(Thực tế, Mong muốn, Thông báo lỗi nếu sai)
// Hoặc Assert.assertTrue(Điều kiện đúng, Thông báo lỗi)
Assert.assertTrue(actualTitle.contains(expectedTitle), "Title trang web khong dung!");
System.out.println("=> Title dung. Kiem tra tiep Header...");
// 2. Kiểm tra Header có tồn tại không
WebElement headerLogo = driver.findElement(By.xpath("//a[@class='logo']"));
Assert.assertTrue(headerLogo.isDisplayed(), "Khong tim thay Logo o header!");
System.out.println("=> Header Logo ton tai. Hard Assert thanh cong!");
// Nếu dòng số 40 (Assert Title) bị sai (Fail), toàn bộ các dòng dưới (từ 41)
// sẽ KHÔNG ĐƯỢC CHẠY và test case dừng ngay lập tức.
}
@Test(priority = 2, description = "Test Soft Assert - Kiem tra nhieu URL va Text cung luc")
public void testSoftAssert() {
System.out.println("Executing testSoftAssert...");
driver.get("https://anhtester.com");
// Khởi tạo đối tượng SoftAssert
SoftAssert softAssert = new SoftAssert();
// 1. Cố tình kiểm tra sai Title
System.out.println("Kiem tra Title bang Soft Assert (Co tinh kiem tra sai)...");
String actualTitle = driver.getTitle();
// Chỉnh lại cho đúng: "Anh Tester" thay vì "anh tester"
softAssert.assertTrue(actualTitle.contains("Anh Tester"), "Title bi sai!");
System.out.println("=> Soft Assert tiep tuc kiem tra URL...");
// 2. Kiểm tra URL đúng
String currentUrl = driver.getCurrentUrl();
softAssert.assertTrue(currentUrl.contains("anhtester"), "URL khong chua anhtester!");
System.out.println("=> Kiem tra URL xong. Kiem tra tiep 1 element khac...");
// 3. Kiểm tra text của menu Blog
WebElement blogMenu = driver.findElement(By.xpath("//a[@href='/blogs']"));
String menuText = blogMenu.getText();
// DOM trả về "blog", trên giao diện hiển thị "BLOG" do CSS. Ta dùng equalsIgnoreCase cho chắc.
softAssert.assertTrue(menuText.equalsIgnoreCase("blog"), "Text cua menu Blog bi sai!");
System.out.println("=> Ket thuc viec kiem tra Soft Assert.");
// QUAN TRỌNG NHẤT: Phải có dòng này ở cuối để tổng hợp lỗi.
// Nếu không có, test case luôn báo PASS dù có bao nhiêu cái assert sai đi nữa.
softAssert.assertAll();
}
@AfterMethod
public void tearDown() {
System.out.println("--- Dong trinh duyet ---");
if (driver != null) {
driver.quit();
}
}
}
......@@ -6,6 +6,8 @@
<class name="com.automation.testcases.BasicWebDriverCommandsTest"/>
<class name="com.automation.testcases.WebElementCommandsTest"/>
<class name="com.automation.testcases.TestNGAnnotationsTest"/>
<class name="com.automation.testcases.TestNGAssertionsTest"/>
<class name="com.automation.testcases.HandleActionsTest"/>
</classes>
</test>
</suite>
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