Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
Automation_selenium
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Trình Nguyễn
Automation_selenium
Commits
28a39a29
Commit
28a39a29
authored
May 06, 2026
by
Đỗ Gia Hưng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Lesson 11 (Assertions) and Lesson 12 (Actions) tests
parent
75c06269
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
206 additions
and
1 deletion
+206
-1
pom.xml
pom.xml
+1
-1
HandleActionsTest.java
...test/java/com/automation/testcases/HandleActionsTest.java
+103
-0
TestNGAssertionsTest.java
...t/java/com/automation/testcases/TestNGAssertionsTest.java
+100
-0
testng.xml
testng.xml
+2
-0
No files found.
pom.xml
View file @
28a39a29
...
...
@@ -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.2
1
.0
</selenium.version>
<selenium.version>
4.2
4
.0
</selenium.version>
<testng.version>
7.9.0
</testng.version>
</properties>
...
...
src/test/java/com/automation/testcases/HandleActionsTest.java
0 → 100644
View file @
28a39a29
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
();
}
}
}
src/test/java/com/automation/testcases/TestNGAssertionsTest.java
0 → 100644
View file @
28a39a29
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
();
}
}
}
testng.xml
View file @
28a39a29
...
...
@@ -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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment