Commit 6ea400e6 authored by Đỗ Gia Hưng's avatar Đỗ Gia Hưng

Add separate test files for Lesson 14 (JS Executor) and Lesson 15 (Wait)

parent 72447216
package com.automation.testcases;
import com.automation.base.BaseSetup;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
public class HandleJavascriptExecutorTest extends BaseSetup {
@Test(priority = 1, description = "Bài 14: Sử dụng JS để Click và Scroll")
public void testJavascriptExecutorActions() throws InterruptedException {
driver.get("https://anhtester.com/");
JavascriptExecutor js = (JavascriptExecutor) driver;
// 1. Lấy Title của trang bằng JS
String title = (String) js.executeScript("return document.title;");
System.out.println("Title via JS: " + title);
// 2. Cuộn chuột xuống cuối trang
System.out.println(">>> Đang cuộn xuống cuối trang...");
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep(2000);
// 3. Cuộn chuột lên đầu trang
System.out.println(">>> Đang cuộn lên đầu trang...");
js.executeScript("window.scrollTo(0, 0)");
Thread.sleep(2000);
// 4. Tìm và cuộn đến một element cụ thể
WebElement btnAllCourses = driver.findElement(By.xpath("//h2[contains(text(),'Các Khóa Học Mới Nhất')]"));
System.out.println(">>> Đang cuộn đến section khóa học...");
js.executeScript("arguments[0].scrollIntoView(true);", btnAllCourses);
highlightElement(btnAllCourses);
Thread.sleep(2000);
// 5. Click vào element bằng JS (Hữu ích khi element bị che khuất)
WebElement blogMenu = driver.findElement(By.xpath("//a[contains(text(),'Blog')]"));
System.out.println(">>> Đang Click vào menu Blog bằng JS...");
js.executeScript("arguments[0].click();", blogMenu);
Thread.sleep(2000);
}
}
package com.automation.testcases;
import com.automation.base.BaseSetup;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.time.Duration;
public class HandleWaitTest extends BaseSetup {
@Test(priority = 1, description = "Bài 15: Sử dụng Explicit Wait (WebDriverWait)")
public void testExplicitWait() {
driver.get("https://anhtester.com/login");
// Khởi tạo WebDriverWait (Chờ tối đa 10 giây)
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// 1. Chờ cho tiêu đề trang chứa từ khóa cụ thể
wait.until(ExpectedConditions.titleContains("Đăng nhập"));
System.out.println(">>> Title đã sẵn sàng.");
// 2. Chờ cho element Login button hiển thị và có thể click được
WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("btn-login")));
highlightElement(loginBtn);
System.out.println(">>> Nút Login đã sẵn sàng để Click (không dùng Thread.sleep).");
Assert.assertTrue(loginBtn.isDisplayed());
}
@Test(priority = 2, description = "Bài 15: Chờ một element biến mất hoặc xuất hiện")
public void testWaitVisibility() {
driver.get("https://anhtester.com/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Chờ cho Logo xuất hiện
WebElement logo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='logo']//img")));
System.out.println(">>> Logo đã hiển thị: " + logo.getAttribute("src"));
Assert.assertTrue(logo.isDisplayed());
}
}
......@@ -11,6 +11,8 @@
<class name="com.automation.testcases.HandleRobotClassTest"/>
<class name="com.automation.testcases.HandleAlertTest"/>
<class name="com.automation.testcases.HandleWindowIFrameTest"/>
<class name="com.automation.testcases.HandleJavascriptExecutorTest"/>
<class name="com.automation.testcases.HandleWaitTest"/>
</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