JMeter Series – Hướng dẫn sử dụng JMeter Assertions

Lượt đọc: 1711

5/5 - (3 bình chọn)

Tiếp theo trong JMeter Series, ở bài này mình sẽ hướng dẫn cho các bạn cách sử dụng Jmeter Assertions để xác thực các phản hồi được trả về từ server.

Assertions là gì ? Có bao nhiêu loại Assertions trong JMeter ?

type-of-jmeter-assertion
Các loại Assertion trong JMeter

JMeter Assertions được sử dụng để xác thực phản hồi của yêu cầu mà bạn đã gửi đến server, nếu kết quả mong đợi như kết quả thực tế thì xác thực thành công. Bạn có thể sử dụng Assertions để xác thực cho Sampler hoặc 1 phần nằm trong Sampler.

Có các loại Assertions sau đây:

1. Response Assertion: là assertion thường được dùng nhất, dùng để kiểm tra kết quả trả về từ text/body/code/message/header có chứa, khớp hay bằng một mẫu được chỉ định hay không.
2. JSON Assertion: thực thi các biểu thức JsonPath và xác thực tài liệu Json.
3. Size Assertion: kiểm tra xem mỗi phản hồi từ server chứa số byte dự kiến trong đó. Bạn có thể chỉ định kích thước bằng, lớn hơn, nhỏ hơn hoặc không bằng một số byte nhất định.
4. JSR233 Assertion: chạy logic code của riêng bạn bằng cách sử dụng script JSR223.
5. Xpath2 Assertion: kiểm tra tài liệu xml có well-formed (đúng cú pháp) hay không. Sử dụng “/” sẽ phù hợp với mọi tài liệu well-formed và là biểu thức XPath2 mặc định. Assertion cũng hỗ trợ các biểu thức boolean, chẳng hạn như “count (// * error) = 2”.
6. Binary Response Assertion: kiểm tra nhận dạng nhị phân của phản hồi.
7. Compare Assertion: được sử dụng để so sánh văn bản hoặc chuỗi biểu thức chính quy đã cho với nội dung phản hồi.
8. Duration Assertion: kiểm tra xem mỗi phản hồi từ server đã trôi qua trong khoảng thời gian nhất định. Bất kỳ phản hồi nào kéo dài hơn sẽ được đánh dấu là không thành công.
9. HTML Assertion: cho phép người dùng kiểm tra dữ liệu phản hồi có đáp ứng theo cú pháp HTML hay không.
10. JSON JMESPath Assertion: updating …
11. MD5Hex Assertion: cho phép kiểm tra băm MD5 của dữ liệu phản hồi.
12. SMIME Assertion: xác nhận SMIME kiểm tra xem phản hồi trả về từ Mail Reader Sampler đã được ký hay chưa. Một điều khoản thay thế (message chưa được ký) cũng có thể được chỉ định, bất kể assertion có thể xác minh chứng chỉ người ký hay không.
13. XML Assertion: Kiểm tra xem dữ liệu phản hồi từ server có chứa một tài liệu XML hợp lệ hay không.
14. XML Schema Assertion: cho phép xác thực một phản hồi XML dựa trên một lược đồ XML.
15. Xpath Assertion: kiểm tra phản hồi của một yêu cầu cụ thể sẽ xác thực xpath như đã định nghĩa, trong trường hợp Xpath tồn tại trong phản hồi thì Assertion đó là true, nếu Xpath không tồn tại thì Assertion đó là false.
16. BeanShell Assertion: Thực thi logic của riêng bạn bằng cách sử dụng tập lệnh Beanshell

Dưỡng Trắng Da Labio

1. Response Assertion

Test plan cần thực hiện:
Vào trang https://phptravels.com/demo và sử dụng Response Assertion xác nhận kết quả trả về sử dụng các field:
Text Response, Response Code, Response Message, Response Headers, Request Headers, URL Sampled, Document (text), Ignore Status, Request Data

1.1. Test Response:

Step 1: Tạo test plan như sau

Test plan
_____Thread Group
__________HTTP Request
_______________Response Assertion
_____View Results Tree
_____Summary Report

JMeter Assertions - Response Assertion
Response Assertions – Test plan

Step 2: Tại Field to Test chọn Text Response, tại Patterns to Test nhấn Add và nhập “Application Test Drive“, “Application Test Drive” là text có hiển thị trên trang https://phptravels.com/demo, ở đây mình sử dụng Assertions để xác nhận text đã nhập tại Patterns to Test có được hiển thị trên trang web hay không.

JMeter Assertions - Text Response
Response Assertions – Text Response
JMeter Assertions - phptravels.com demo
phptravels.com demo

Step 3: Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Response data – Response Body

HTTP Request hiển thị trạng thái Passed tức là server có trả về đúng kết quả cần xác nhận

JMeter Assertion - Text Response - Passed
Response Assertion – Text Response – Response data (Request Body)

Bây giờ thử trường hợp nhập text là “Docgihocgi.com” nhé

JMeter Response Assertion - Text Response
Response Assertion – Text Response
JMeter Response Assertion - Text Response - Failed
Response Assertion – Text Response – Response data (Request Body)

HTTP Request hiển thị trạng thái Failed tức là server không trả về đúng kết quả cần xác nhận.

OK! Tới đây thì các bạn đã hiểu phương thức hoạt động của Response Assertion rồi phải không nào.
Tương tự ta thực hiện test Assertions với các loại Field to Test khác.

1.2. Response Code:

Field to Test: Response Code
Patterns to Test: 200

Assertions - Response Code
JMeter Assertions – Response Assertion – Response Code

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Sampler result cho thấy Response code là 200 nên trạng thái là Passed.

Assertions - Response Code - Sampler result
Response Assertion – Response Code – Sampler result

1.3. Response Message:

Field to Test: Response Message
Patterns to Test: OK

Response Assertion – Response Message

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Sampler result cho thấy Response message là OK nên trạng thái là Passed.

Assertion - Response Message - Sampler result
Response Assertion – Response Message – Sampler result

1.4. Response Headers:

Field to Test: Response Headers
Patterns to Test: Server: cloudflare

Response Assertions - Response Headers
Response Assertions – Response Headers

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Response data (Response headers ) cho thấy có hiển thị mục Server: cloudflare nên trạng thái là Passed.

Response Assertions - Response Headers - data
Response Assertions – Response Headers – Ressponse data

1.5. Request Headers:

Field to Test: Request Headers
Patterns to Test: Host: phptravels.com

Response Assertions - Response Headers - Patterns to Test
Response Assertions – Response Headers – Patterns to Test

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Request (Request Headers ) cho thấy có hiển thị mục Host: phptravels.com nên trạng thái là Passed.

Response Assertions - Response Headers - Results
Response Assertions – Response Headers – Results

1.6. URL Sampled

Field to Test: URL Sampled
Patterns to Test: phptravels.com/demo

Response Assertions - URL Sampled
Response Assertions – URL Sampled

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Request (Request Body) cho thấy có hiển thị url chứa https://phptravels.com/demo nên trạng thái là Passed.

Response Assertions - URL Sampled - Results
Response Assertions – URL Sampled – Results

1.7. Document (text)

Field to Test: Document (text)
Patterns to Test: <title>Demo Script Test drive – PHPTRAVELS</title>

Response Assertions - Document (text)
Response Assertions – Document (text)

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Kiểm tra kết quả trả về từ server tại Response data (Response Body) cho thấy có hiển thị đoạn text <title>Demo Script Test drive – PHPTRAVELS</title> nên trạng thái là Passed.

Response Assertions - Document (text) - Results
Response Assertions – Document (text) – Results

2. JSON Assertion

Test plan cần thực hiện:
Xác thực kết quả trả về từ json document https://jsonplaceholder.typicode.com/users có user có name là “Leanne Graham” hay không.

Step 1: Tạo test plan như sau

Test plan
_____Thread Group
__________HTTP Request
_______________JSON Assertion
_____View Results Tree
_____Summary Report

JSON Assertions - HTTP Request
JMeter Assertions – JSON Assertions – HTTP Request
JSON Assertions - Assertion
JSON Assertions – Assertion

Step 2: Kiểm tra JSON Path của name trong file Json

Nhấn Start > Chọn View Results Tree > Chọn JSON Path Tester > Chọn HTTP Request

Tại JSON Path Expression nhập “$.name” > Nhấn Test
Kết quả bên dưới là NO MATCH do JSON Path chưa đúng.

Tại JSON Path Expression nhập “$..name” > Nhấn Test
Kết quả bên dưới là MATCH và hiển thị list name có trong file.

Do đó JSON Path cần lấy là “$..name

JSON Assertions - JSON Path Tester - No match
JSON Assertions – JSON Path Tester – No match
JSON Assertions - JSON Path Tester - Match
JSON Assertions – JSON Path Tester – Match

Step 3: Setting JSON Assertion

Assert JSON Path exists: $..name
Additionally assert value: Checked
Expected Value: “Leanne Graham

JSON Assertions - Setting
JSON Assertions – Setting

Step 4: Start test

Nhấn Start > Chọn View Results Tree > Kết quả trả về từ file Json có chứa name “Leanne Graham” nên trạng thái của HTTP RequestPassed.

JSON Assertions - Result
JSON Assertions – Result

3. Size Assertion

Test plan cần thực hiện:
Xác thực kết quả trả về khi request từ file json https://jsonplaceholder.typicode.com/users có dung lượng bytes dự kiến là bao nhiêu

Step 1: Tạo test plan như sau

Test plan
_____Thread Group
__________HTTP Request
_______________Size Assertion
_____View Results Tree
_____Summary Report

JMeter Assertions: Size Assertions - HTTP Request
JMeter Assertions: Size Assertions – HTTP Request
Size Assertions -  Assertions
Size Assertions – Assertion

Step 2: Setting tại mục Size to Assert, kết quả trả về có Size in bytes = 6000

Size in bytes: 6000
Type of Comparison: =

Size Assertions -  Size to Assert
Size Assertions – Size to Assert

Step 3: Start test

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Chọn Sampler result > Kết quả trả về Size in bytes:6776 không thỏa mãn nên trạng thái của HTTP RequestFailed.

Size Assertions -  Test - Failed
Size Assertions – Test – Failed

Step 4: Setting tại mục Size to Assert, kết quả trả về có Size in bytes > 6000

Size in bytes: 6000
Type of Comparison: >

Size Assertions -  Size to Assert
Size Assertions – Size to Assert
Size Assertions -  Test - Passed
Size Assertions – Test – Passed

Step 5: Start test

Nhấn Start > Chọn View Results Tree > Chọn HTTP Request > Chọn Sampler result > Kết quả trả về Size in bytes:6774 thỏa mãn nên trạng thái của HTTP RequestPassed.

4. Xpath Assertion

Test plan cần thực hiện:
Xác thực kết quả trả về từ website https://jsonplaceholder.typicode.com/users có chứa đường dẫn Xpath //*[@id=”btnLogin”] hay không ?

Step 1: Tạo test plan như sau

Test plan
_____Thread Group
__________HTTP Request
_______________Xpath Assertion
_____View Results Tree
_____Summary Report

Xpath Assertion - HTTP Request
Xpath Assertion – HTTP Request

Step 2: Setting Xpath Assertion

Apply to: Main sample only
XML Parsing Options: Use Tidy (tolerant parser)
Xpath Assertion: //*[@id=”btnLogin”]

Xpath Assertion - Config
Xpath Assertion – Config

Cách lấy Xpath bất kì phần tử nào trên website:
Tại button LOGIN click phải chọn Inspec hoặc nhấn F12 để tìm ra đường dẫn Xpath của Element cần xác nhận, sau đó chọn Copy Xpath.

Xpath Assertion - website Inspec
Xpath Assertion – website Inspec
Xpath Assertion Copy Xpath
Xpath Assertion – Copy Xpath

Bạn cũng có thể xác thực đường dẫn đã lấy bằng cách sử dụng Xpath Tester trên JMeter.

Xpath Assertion - Xpath Tester
Xpath Assertion – Xpath Tester

Step 3: Start test

Nhấn Start > Chọn View Results Tree > trạng thái của HTTP RequestPassed.

Xpath Assertions -  Test - Passed
Xpath Assertions – Test – Passed

Lời kết

Vậy là qua bài viết bạn đã có thể sử dụng Assertion để kiểm tra kết quả trả về từ server có thỏa mãn với tiêu chí đề ra.
Hiện tại các hướng dẫn bao gồm Response Assertion, JSON Assertion, Size Assertion Xpath Assertion, các loại assertion còn lại mình sẽ cập nhật trong thời gian sớm nhất.

Hẹn gặp lại các bạn trong các bài viết tiếp theo của JMeter Series.
Nếu có thắc mắc hoặc trao đổi vui lòng để lại bình luận bên dưới nhé !

Đồng Hồ Thông Minh Mocato Stark P9-W

Bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

RSS
Follow by Email
LinkedIn
Share
Instagram
Scroll to Top
Scroll to Top