-
Notifications
You must be signed in to change notification settings - Fork 8
/
challenge.js
209 lines (187 loc) · 6.32 KB
/
challenge.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import http from "k6/http";
import { parseHTML } from "k6/html";
import { sleep, check } from "k6";
let debug = 1;
const domain = "https://challenge.flood.io";
let stepIDVal = "NOT_FOUND";
let authTokenVal = "NOT_FOUND";
let largestOrderSelectedVal = "NOT_FOUND";
let largestOrderVal = 0;
let challengerOrders = [];
let challengerValues = [];
let oneTimeToken = '';
export default function () {
Home();
Step1_ClickStart();
Step2_SubmitAge();
Step3_SubmitHighestOrderValue();
Step4_ClickNext();
Step5_SubmitOTT();
}
export function Home() {
/*************************
* 01_HomePage
* Visit the homepage.
*************************/
let res = http.get(domain, { tags: { name: "01_HomePage" } });
check(res, {
"Home_text verification": (r) => r.body.includes("Ready to take the test?"),
});
//Extract dynamic variables
let doc = parseHTML(res.body);
stepIDVal = doc.find('input[name="challenger[step_id]"]').val();
authTokenVal = doc.find('input[name="authenticity_token"]').val();
// console.log('stepIDVal = ', stepIDVal);
// console.log('authTokenVal = ', encodeURIComponent(authTokenVal));
if (debug == 0) {
sleep(Math.random() * 5);
}
}
export function Step1_ClickStart() {
/*************************
* 02_Step1_ClickStart
* Click the start button.
*************************/
let data = {
authenticity_token: authTokenVal,
"challenger[step_id]": stepIDVal,
"challenger[step_number]": "1",
commit: "Start",
};
let res = http.post(domain + "/start", data);
check(res, {
"Step1_text verification": (r) => r.body.includes("Step 2"),
});
//Extract dynamic variables
let doc = parseHTML(res.body);
stepIDVal = doc.find('input[name="challenger[step_id]"]').val();
authTokenVal = doc.find('input[name="authenticity_token"]').val();
}
export function Step2_SubmitAge() {
/*************************
* 03_Step2_SubmitAge
* From the dropdown menu, select 34 as the age and then click Next.
*************************/
let data = {
authenticity_token: authTokenVal,
"challenger[step_id]": stepIDVal,
"challenger[step_number]": "2",
"challenger[age]": "34",
commit: "Next",
};
let res = http.post(domain + "/start", data);
check(res, {
"Step2_text verification": (r) => r.body.includes("Step 3"),
});
//Extract dynamic variables
let doc = parseHTML(res.body);
stepIDVal = doc.find('input[name="challenger[step_id]"]').val();
authTokenVal = doc.find('input[name="authenticity_token"]').val();
let orderValues = doc
.find('label[class="collection_radio_buttons"]')
.toArray();
let orderSelectedValues = doc
.find('input[name="challenger[order_selected]"]')
.toArray();
//Determine which among the extracted numbers is the largest and save that into a variable.
let i = 0;
let largestOrdinal = 0; //counter variable to get the ordinal for the largest order value
orderValues.forEach(function (element) {
// console.log(element.text());
if (parseInt(element.text()) > largestOrderVal) {
largestOrderVal = parseInt(element.text());
// console.log("largestOrderVal = ", largestOrderVal);
largestOrdinal = i;
// console.log("largestOrdinal = ", largestOrdinal);
}
i++;
});
//Get the order selected value string corresponding to the largest number.
largestOrderSelectedVal = orderSelectedValues[largestOrdinal].val();
// console.log("largest selected order value is", largestOrderSelectedVal);
}
export function Step3_SubmitHighestOrderValue() {
/*************************
* 04_Step3_SubmitHighestOrderValue
* Select the radio buton with the highest number.
* Enter the highest number as a string in the text box.
* Click Next.
*************************/
let data = {
authenticity_token: authTokenVal,
"challenger[step_id]": stepIDVal,
"challenger[step_number]": "3",
"challenger[largest_order]": largestOrderVal,
"challenger[order_selected]": largestOrderSelectedVal,
commit: "Next",
};
let res = http.post(domain + "/start", data);
check(res, {
"Step3_text verification": (r) => r.body.includes("Step 4"),
});
// Extract dynamic variables
let doc = parseHTML(res.body);
stepIDVal = doc.find('input[name="challenger[step_id]"]').val();
authTokenVal = doc.find('input[name="authenticity_token"]').val();
// Extract dynamic challenger orders and values
let challengerOrderRegex = /name="(challenger\[order_[0-9]+\])" type="hidden" value="([0-9]+)"/g;
let challengerMatch;
while ((challengerMatch = challengerOrderRegex.exec(res.body))) {
// console.log('challengerMatch: ', challengerMatch);
challengerOrders.push(challengerMatch[1]); // challenger[order_1]
challengerValues.push(challengerMatch[2]); // 1612534938
}
// console.log('challengerOrders: ', challengerOrders);
// console.log('challengerValues: ', challengerValues);
}
export function Step4_ClickNext() {
/*************************
* 05_Step4_ClickNext
* Click Next.
*************************/
// console.log(challengerOrders[0], ":", challengerValues[0]);
let i = 0;
let data = {
authenticity_token: authTokenVal,
"challenger[step_id]": stepIDVal,
"challenger[step_number]": "4",
commit: "Next",
};
challengerOrders.forEach(function(item){
data[item] = challengerValues[i];
i++
});
let res = http.post(domain + "/start", data);
check(res, {
"Step4_text verification": (r) => r.body.includes("Step 5"),
});
// Extract dynamic variables
let doc = parseHTML(res.body);
stepIDVal = doc.find('input[name="challenger[step_id]"]').val();
authTokenVal = doc.find('input[name="authenticity_token"]').val();
// Get code
res = http.get(domain + "/code");
doc = JSON.parse(res.body);
oneTimeToken = doc.code;
// console.log('oneTimeToken =', oneTimeToken);
}
export function Step5_SubmitOTT() {
/*************************
* 06_Step5_SubmitOTT
* Copy the token.
* Paste the token in the text field.
* Click Next.
*************************/
let data = {
authenticity_token: authTokenVal,
"challenger[step_id]": stepIDVal,
"challenger[step_number]": "5",
"challenger[one_time_token]": oneTimeToken,
commit: "Next",
};
let res = http.post(domain + "/start", data);
check(res, {
"Step5_text verification": (r) => r.body.includes("You're Done"),
});
console.log(res.body);
}