Sau khi biết khai báo biến và viết điều kiện, tester cần học cách làm việc với nhiều kiểu dữ liệu. Test data, menu trên giao diện và response API thường được biểu diễn bằng array, object và JSON.
Array: danh sách nhiều phần tử
Array lưu nhiều giá trị theo thứ tự. Index bắt đầu từ 0, vì vậy phần tử đầu tiên có index 0.
const roles: string[] = ["admin", "editor", "student"];
console.log(roles[0]); // "admin"
console.log(roles.length); // 3
roles.push("guest"); //lệnh push đẩy thêm phần tử mới vào cuối array
console.log(roles); //["admin", "editor", "student", "guest"];roles: list[str] = ["admin", "editor", "student"]
print(roles[0]) # "admin"
print(len(roles)) # 3
roles.append("guest")
print(roles)Cách viết string[] cho biết đây là array chỉ chứa string. Tương tự, number[] là array chứa số.
| Thao tác | Mục đích | Kết quả |
|---|---|---|
length | Đếm số phần tử | Một number |
push() | Thêm phần tử vào cuối | Thay đổi array gốc |
includes() | Kiểm tra giá trị có tồn tại | Boolean |
const statusCodes = [200, 201, 400, 401, 404];
const hasUnauthorized = statusCodes.includes(401);
console.log(hasUnauthorized); // true
status_codes = [200, 201, 400, 401, 404]
has_unauthorized = 401 in status_codes
print(has_unauthorized) # True
Duyệt array bằng for…of
for...of là vòng lặp dễ đọc và phù hợp khi tester muốn thực hiện cùng một thao tác với từng phần tử.
const expectedMenus = ["Home", "Articles", "Profile"];
for (const menu of expectedMenus) { // khởi tạo 1 biến tạm "menu" trong vòng lặp
console.log(`Kiểm tra menu: ${menu}`);
}expected_menus = ["Home", "Articles", "Profile"]
for menu in expected_menus:
print(f"Kiểm tra menu: {menu}")Object: dữ liệu có thuộc tính
Object biểu diễn một đối tượng bằng các cặp key–value. Request body, response API, configuration và test data đều thường là object.
const user = {
email: "student@elitest.vn",
password: "Aa@123456",
approved: true,
};
console.log(user.email);
console.log(user["password"]);
user.approved = false;user = {
"email": "student@elitest.vn",
"password": "Aa@123456",
"approved": True,
}
print(user["email"])
print(user["password"])
user["approved"] = FalseCó thể đọc property bằng dấu chấm hoặc ngoặc vuông. Dấu chấm thường dễ đọc hơn; ngoặc vuông hữu ích khi tên property nằm trong một biến.
Object lồng nhau và array chứa object
const article = {
title: "API Testing",
author: {
username: "linh",
active: true,
},
tags: ["api", "testing"],
};
console.log(article.author.username); // "linh"
console.log(article.tags[0]); // "api"article = {
"title": "API Testing",
"author": {
"username": "linh",
"active": True,
},
"tags": ["api", "testing"],
}
print(article["author"]["username"])
print(article["tags"][0])Destructuring cơ bản
Destructuring lấy property từ object và tạo thành các biến ngắn gọn:
const user = {
id: 1,
name: "Linh",
email: "linh@example.com",
password: "supersecretpassword"
};
// set biến kiểu thông thường
const email = user.email;
const password = user.password;
// Set biến kiểu nhanh gọn, set đồng thời 2 biến email và password, lấy giá trị tương ứng trong object user
const { email, password } = user;
console.log(email);
console.log(password);# Ví dụ chúng ta có:
user = {
"id": 1,
"name": "Linh",
"email": "linh@example.com",
"password": "supersecretpassword",
}
# Truy cập từng giá trị bằng key:
email = user["email"]
password = user["password"]
print(email)
print(password)Function (quan trọng)
Function nhận dữ liệu đầu vào, thực hiện một công việc và có thể trả kết quả. Đây là kiến thức cốt lõi của bất kì ngôn ngữ lập trình nào
// Tạo 1 function lên là createEmail, đầu vào là biến name, đầu ra là 1 string
// với định dạng "name + đuôi @elitest.vn
function createEmail(name: string): string {
return `${name}@elitest.vn`;
}
const email = createEmail("student01");
console.log(email); // trả về student01@elitest.vndef create_email(name: str) -> str:
return f"{name}@elitest.vn"
email = create_email("student01")
print(email)| Khái niệm | Trong ví dụ |
|---|---|
| Function name | createEmail |
| Parameter và kiểu dữ liệu của nó | name: string |
| Kiểu dữ liệu mà function trả về | : string |
Default parameter
// function tạo user, trường name là 1 string phải truyển vào
// trường approved là 1 boolean, có giá trị mặc định là false nếu không truyền
function createUser(name: string, approved = false) {
return { name, approved };
}
createUser("student02", true); // truyền "student02" và "true" vào function
createUser("student01"); // truyền mỗi student, approved không truyền thì mặc định là falsedef create_user(name: str, approved: bool = False) -> dict:
return {"name": name, "approved": approved}
create_user("student01")
create_user("student02", True)
Tóm tắt phần 2
- Array lưu danh sách; object mô tả một đối tượng bằng property.
- Dùng
for...ofđể duyệt các phần tử theo cách dễ đọc. - Function đóng gói logic và trả kết quả để tái sử dụng.
Ở TypeScript cơ bản cho tester (P3), chúng ta sẽ chia code thành nhiều file, hiểu async/await, xử lý lỗi và học class ở mức đủ để bắt đầu Page Object Model.




