-
Notifications
You must be signed in to change notification settings - Fork 12
/
navigateHomepage.js
79 lines (70 loc) · 2.45 KB
/
navigateHomepage.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
import { sleep, group } from "k6";
import http from "k6/http";
import { checkStatus } from "./utils.js";
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.1.0/index.js";
export function navigateHomepage() {
group("Navigate to Homepage", function () {
let response = http.get("http://ecommerce.test.k6.io/", {
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate",
"accept-language": "en-US,en;q=0.9",
"cache-control": "max-age=0",
connection: "keep-alive",
host: "ecommerce.test.k6.io",
"upgrade-insecure-requests": "1",
},
});
checkStatus({
response: response,
expectedStatus: 200,
failOnError: true,
printOnError: true
});
// extract all of the available products using their "Add to Cart" buttons
const addToCartButtons = response
.html()
.find("li[class*=product]")
.find('a:contains("Add to Cart")')
.toArray();
const products = addToCartButtons.map(i => {
return {
id: i.get(0).getAttribute("data-product_id"),
sku: i.get(0).getAttribute("data-product_sku")
};
});
products.forEach(i => {
console.debug(`Product ID: '${i.id}' SKU: '${i.sku}'`);
});
// select a random product and store in vars:
globalThis.vars["selectedProduct"] = products[Math.floor(Math.random() * products.length)];
console.debug(`Selected Product with ID: '${globalThis.vars["selectedProduct"].id}' and SKU: '${globalThis.vars["selectedProduct"].sku}'`);
response = http.post(
"http://ecommerce.test.k6.io/?wc-ajax=get_refreshed_fragments",
{
time: Date.now(),
},
{
headers: {
accept: "*/*",
"accept-encoding": "gzip, deflate",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
"content-type":
"application/x-www-form-urlencoded;type=content-type;mimeType=application/x-www-form-urlencoded",
host: "ecommerce.test.k6.io",
origin: "http://ecommerce.test.k6.io",
"x-requested-with": "XMLHttpRequest",
},
}
);
checkStatus({
response: response,
expectedStatus: 200,
failOnError: true,
printOnError: true
});
});
sleep(randomIntBetween(pauseMin, pauseMax));
}