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
7e3780a9
Commit
7e3780a9
authored
May 04, 2026
by
Đỗ Gia Hưng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Thuc hanh xu ly Element va Javascript
parent
69c5e582
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
WebElementCommandsTest.java
...java/com/automation/testcases/WebElementCommandsTest.java
+70
-0
No files found.
src/test/java/com/automation/testcases/WebElementCommandsTest.java
0 → 100644
View file @
7e3780a9
package
com
.
automation
.
testcases
;
import
org.openqa.selenium.By
;
import
org.openqa.selenium.JavascriptExecutor
;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.chrome.ChromeDriver
;
import
org.openqa.selenium.support.ui.ExpectedConditions
;
import
org.openqa.selenium.support.ui.WebDriverWait
;
import
org.testng.annotations.AfterMethod
;
import
org.testng.annotations.BeforeMethod
;
import
org.testng.annotations.Test
;
import
java.time.Duration
;
public
class
WebElementCommandsTest
{
WebDriver
driver
;
@BeforeMethod
public
void
setUp
()
{
driver
=
new
ChromeDriver
();
driver
.
manage
().
window
().
maximize
();
// Cài đặt chờ ngầm định 10s cho việc tìm kiếm element
driver
.
manage
().
timeouts
().
implicitlyWait
(
Duration
.
ofSeconds
(
10
));
}
@Test
public
void
testFindAndInteractWithElements
()
throws
InterruptedException
{
// Mở trang CRM demo của Anh Tester
driver
.
get
(
"https://crm.anhtester.com/admin/authentication"
);
System
.
out
.
println
(
"Đã mở trang đăng nhập CRM"
);
// 1. CHUẨN MỰC: TÌM ELEMENT VÀ TƯƠNG TÁC (Check Locator)
// Tìm ô Email qua ID và điền chữ
WebElement
inputEmail
=
driver
.
findElement
(
By
.
id
(
"email"
));
inputEmail
.
clear
();
inputEmail
.
sendKeys
(
"admin@example.com"
);
// Tìm ô Password qua ID và điền chữ
WebElement
inputPassword
=
driver
.
findElement
(
By
.
id
(
"password"
));
inputPassword
.
clear
();
inputPassword
.
sendKeys
(
"123456"
);
Thread
.
sleep
(
2000
);
// Tạm dừng 2s để bạn nhìn thấy chữ được điền vào
// 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)
WebDriverWait
wait
=
new
WebDriverWait
(
driver
,
Duration
.
ofSeconds
(
10
));
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)
// Thông thường bạn chỉ cần gõ: buttonLogin.click();
// Nhưng ở đây ta dùng thử JS để click xuyên qua mọi chướng ngại vật
System
.
out
.
println
(
"Thực hiện click nút Login bằng JavascriptExecutor..."
);
JavascriptExecutor
js
=
(
JavascriptExecutor
)
driver
;
js
.
executeScript
(
"arguments[0].click();"
,
buttonLogin
);
Thread
.
sleep
(
3000
);
// Chờ 3s để bạn quan sát kết quả trang sau khi bấm Đăng nhập
System
.
out
.
println
(
"Thực thi kịch bản thành công!"
);
}
@AfterMethod
public
void
tearDown
()
{
if
(
driver
!=
null
)
{
driver
.
quit
();
// Đóng trình duyệt dọn dẹp RAM
}
}
}
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