-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
119 lines (103 loc) · 2.98 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import http from 'k6/http';
import { sleep, check } from 'k6';
import { randomItem, randomString, randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';
export const options = {
stages: [
{ duration: '20m', target: 10 },
],
};
const BASE_URL = __ENV.BASE_URL || 'http://localhost:8080';
const products = [
'0PUK6V6EV0',
'1YMWWN1N4O',
'2ZYFJ3GM2N',
'66VCHSJNUP',
'6E92ZMYYFZ',
'9SIQT8TOJO',
'L9ECAV7KIM',
'LS4PSXUNUM',
'OLJCESPC7Z'
];
const creditCards = [
'4086589772773199',
'4806685382089452',
'4622884557604268',
'4196762922218756',
'4602858310051573'
];
const zipCodes = [
'12345',
'23456',
'34567',
'45678',
'56789'
];
const currencies = ['EUR', 'USD', 'JPY', 'CAD', 'GBP', 'TRY'];
function checkResponse(res, expectedStatus = 200) {
let success = check(res, { [`status was ${expectedStatus}`]: (r) => r.status === expectedStatus });
if (!success) {
console.error(`Request failed. Status: ${res.status}, URL: ${res.url}, Response: ${res.body}`);
}
return success;
}
function index() {
let res = http.get(`${BASE_URL}/`);
checkResponse(res);
}
function setCurrency() {
let payload = { currency_code: randomItem(currencies) };
let res = http.post(`${BASE_URL}/setCurrency`, payload);
checkResponse(res);
}
function browseProduct() {
let product = randomItem(products);
let res = http.get(`${BASE_URL}/product/${product}`);
checkResponse(res);
}
function viewCart() {
let res = http.get(`${BASE_URL}/cart`);
checkResponse(res);
}
function addToCart() {
let product = randomItem(products);
http.get(`${BASE_URL}/product/${product}`); // Ensure the product page is viewed before adding to cart
let payload = { product_id: product, quantity: randomIntBetween(1, 10) };
let res = http.post(`${BASE_URL}/cart`, payload);
checkResponse(res);
}
function emptyCart() {
let res = http.post(`${BASE_URL}/cart/empty`);
checkResponse(res);
}
function checkout() {
addToCart();
let current_year = new Date().getFullYear() + 1;
let payload = {
email: `${randomString(10)}@example.com`,
street_address: `${randomIntBetween(100, 9999)} ${randomString(10)} St`,
zip_code: randomItem(zipCodes),
city: randomString(10),
state: randomString(2, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
country: 'USA',
credit_card_number: randomItem(creditCards),
credit_card_expiration_month: randomIntBetween(1, 12),
credit_card_expiration_year: randomIntBetween(current_year+1, current_year + 70),
credit_card_cvv: randomIntBetween(100, 999)
};
let res = http.post(`${BASE_URL}/cart/checkout`, payload);
checkResponse(res);
}
function logout() {
let res = http.get(`${BASE_URL}/logout`);
checkResponse(res);
}
export default function () {
index();
setCurrency();
browseProduct();
addToCart();
viewCart();
checkout();
logout();
sleep(randomIntBetween(1, 10));
}