/
locator_pom.js
71 lines (61 loc) · 1.65 KB
/
locator_pom.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
import { chromium } from 'k6/x/browser';
export const options = {
thresholds: {
checks: ["rate==1.0"]
}
}
/*
Page Object Model is a well-known pattern to abstract a web page.
The Locator API enables using the Page Object Model pattern to organize
and simplify test code.
Note: For comparison, you can see another example that does not use
the Page Object Model pattern in locator.js.
*/
export class Bet {
constructor(page) {
this.page = page;
this.headsButton = page.locator("input[value='Bet on heads!']");
this.tailsButton = page.locator("input[value='Bet on tails!']");
this.currentBet = page.locator("//p[starts-with(text(),'Your bet: ')]");
}
goto() {
return this.page.goto("https://test.k6.io/flip_coin.php", { waitUntil: "networkidle" });
}
heads() {
return Promise.all([
this.page.waitForNavigation(),
this.headsButton.click(),
]);
}
tails() {
return Promise.all([
this.page.waitForNavigation(),
this.tailsButton.click(),
]);
}
current() {
return this.currentBet.innerText();
}
}
export default async function() {
const browser = chromium.launch({
headless: __ENV.XK6_HEADLESS ? true : false
});
const context = browser.newContext();
const page = context.newPage();
const bet = new Bet(page);
try {
await bet.goto()
await bet.tails();
console.log("Current bet:", bet.current());
await bet.heads();
console.log("Current bet:", bet.current());
await bet.tails();
console.log("Current bet:", bet.current());
await bet.heads();
console.log("Current bet:", bet.current());
} finally {
page.close();
browser.close();
}
}