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
4c3b9bc8
Commit
4c3b9bc8
authored
May 05, 2026
by
Đỗ Gia Hưng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hoan thien Bai 7, 9, 10 va fix loi hang driver
parent
53c51bee
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
34 deletions
+78
-34
pom.xml
pom.xml
+17
-3
BasicWebDriverCommandsTest.java
.../com/automation/testcases/BasicWebDriverCommandsTest.java
+15
-10
TestNGAnnotationsTest.java
.../java/com/automation/testcases/TestNGAnnotationsTest.java
+33
-14
WebElementCommandsTest.java
...java/com/automation/testcases/WebElementCommandsTest.java
+13
-7
No files found.
pom.xml
View file @
4c3b9bc8
...
...
@@ -9,10 +9,10 @@
<version>
1.0-SNAPSHOT
</version>
<properties>
<maven.compiler.source>
1
1
</maven.compiler.source>
<maven.compiler.target>
1
1
</maven.compiler.target>
<maven.compiler.source>
1
7
</maven.compiler.source>
<maven.compiler.target>
1
7
</maven.compiler.target>
<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>
</properties>
...
...
@@ -32,5 +32,19 @@
<scope>
test
</scope>
</dependency>
</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>
src/test/java/com/automation/testcases/BasicWebDriverCommandsTest.java
View file @
4c3b9bc8
...
...
@@ -2,6 +2,7 @@ package com.automation.testcases;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.chrome.ChromeDriver
;
import
org.openqa.selenium.chrome.ChromeOptions
;
import
org.testng.annotations.AfterMethod
;
import
org.testng.annotations.BeforeMethod
;
import
org.testng.annotations.Test
;
...
...
@@ -9,18 +10,22 @@ import org.testng.annotations.Test;
import
java.time.Duration
;
public
class
BasicWebDriverCommandsTest
{
WebDriver
driver
;
@BeforeMethod
public
void
setUp
()
{
// 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) ---
driver
.
manage
().
timeouts
().
implicitlyWait
(
Duration
.
ofSeconds
(
10
));
driver
.
manage
().
timeouts
().
pageLoadTimeout
(
Duration
.
ofSeconds
(
30
));
// --- 3. Quản lý cửa sổ (Window Management) ---
driver
.
manage
().
window
().
maximize
();
// Phóng to toàn màn hình
}
...
...
@@ -30,10 +35,10 @@ public class BasicWebDriverCommandsTest {
// --- 1. Các hàm cơ bản (Basic Browser Operations) ---
System
.
out
.
println
(
"Mở trang web Anh Tester..."
);
driver
.
get
(
"https://anhtester.com"
);
System
.
out
.
println
(
"Tiêu đề trang: "
+
driver
.
getTitle
());
System
.
out
.
println
(
"URL hiện tại: "
+
driver
.
getCurrentUrl
());
System
.
out
.
println
(
"Độ dài mã nguồn: "
+
driver
.
getPageSource
().
length
());
Thread
.
sleep
(
2000
);
// Demo mục đích quan sát
...
...
@@ -42,18 +47,18 @@ public class BasicWebDriverCommandsTest {
System
.
out
.
println
(
"\nChuyển hướng sang Google..."
);
driver
.
navigate
().
to
(
"https://google.com"
);
System
.
out
.
println
(
"Đã chuyển sang: "
+
driver
.
getTitle
());
Thread
.
sleep
(
2000
);
System
.
out
.
println
(
"Quay lại (Back)..."
);
driver
.
navigate
().
back
();
System
.
out
.
println
(
"Trang hiện tại: "
+
driver
.
getTitle
());
Thread
.
sleep
(
2000
);
System
.
out
.
println
(
"Đi tới (Forward)..."
);
driver
.
navigate
().
forward
();
System
.
out
.
println
(
"Tải lại trang (Refresh)..."
);
driver
.
navigate
().
refresh
();
}
...
...
src/test/java/com/automation/testcases/TestNGAnnotationsTest.java
View file @
4c3b9bc8
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.annotations.*
;
import
java.time.Duration
;
public
class
TestNGAnnotationsTest
{
WebDriver
driver
;
@BeforeSuite
public
void
beforeSuite
()
{
System
.
out
.
println
(
">>> @BeforeSuite: Chay 1 lan duy nhat truoc toan bo Suite"
);
...
...
@@ -22,35 +29,47 @@ public class TestNGAnnotationsTest {
@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)
//
description dung de mo ta test case giup bao cao de doc hon
// Bai 10: Su dung thuoc tinh priority de quan ly thu tu chay (so nho chay
//
truoc)
@Test
(
priority
=
1
,
description
=
"Kiem tra tieu de trang web Anh Tester"
)
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
actualTitle
=
"Anh Tester"
;
Assert
.
assert
Equals
(
actualTitle
,
expectedTitle
,
"Tieu de khong khop
!"
);
String
actualTitle
=
driver
.
getTitle
()
;
Assert
.
assert
True
(
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
()
{
System
.
out
.
println
(
"Executing Test Case 2: Kiem tra hien thi"
);
boolean
isDisplayed
=
true
;
Assert
.
assertTrue
(
isDisplayed
,
"Phan tu khong hien thi!"
);
System
.
out
.
println
(
"Executing Test Case 2: Mo trang Blog va kiem tra URL"
);
driver
.
get
(
"https://anhtester.com/blog"
);
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 dang bi loi nen tam thoi bo qua"
)
@Test
(
priority
=
3
,
enabled
=
false
,
description
=
"Test case nay bi disable nen se khong chay"
)
public
void
testCase3
()
{
System
.
out
.
println
(
"Test case nay se khong bao gio duoc chay"
);
}
@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
...
...
src/test/java/com/automation/testcases/WebElementCommandsTest.java
View file @
4c3b9bc8
...
...
@@ -5,6 +5,7 @@ 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.chrome.ChromeOptions
;
import
org.openqa.selenium.support.ui.ExpectedConditions
;
import
org.openqa.selenium.support.ui.WebDriverWait
;
import
org.testng.annotations.AfterMethod
;
...
...
@@ -19,7 +20,11 @@ public class WebElementCommandsTest {
@BeforeMethod
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
();
// Cài đặt chờ ngầm định 10s cho việc tìm kiếm element
driver
.
manage
().
timeouts
().
implicitlyWait
(
Duration
.
ofSeconds
(
10
));
...
...
@@ -36,28 +41,29 @@ public class WebElementCommandsTest {
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']"
)));
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!"
);
}
...
...
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