-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.mjs
More file actions
95 lines (79 loc) · 3.13 KB
/
Copy pathtest.mjs
File metadata and controls
95 lines (79 loc) · 3.13 KB
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
import { Builder, By, Key, until } from 'selenium-webdriver';
import { assert } from 'chai';
describe('search engine', async function () {
// Increase timeout for Sauce Labs
this.timeout(120000);
let driver;
// Make sure all required variables are set
before(async function () {
if (!process.env.SAUCE_USERNAME || !process.env.SAUCE_ACCESS_KEY) {
throw new Error('Sauce Labs user name or access key not set')
}
});
// Before each test, initialize Selenium and launch browser
beforeEach(async function () {
// We connect to Sauce Labs's Selenium service
const server = process.env.SAUCE_URL ||
'https://ondemand.us-west-1.saucelabs.com/wd/hub';
// Sauce Labs authentication and options
const sauceOptions = {
username: process.env.SAUCE_USERNAME,
accessKey: process.env.SAUCE_ACCESS_KEY,
name: this.currentTest.fullTitle()
};
let browser = process.env.BROWSER || 'chrome';
// Microsoft uses a longer name for Edge
if (browser == 'edge') {
browser = 'MicrosoftEdge';
}
driver = await new Builder()
.usingServer(server)
.forBrowser(browser)
.setCapability('sauce:options', sauceOptions)
.build();
});
// After each test, submit the result and close the browser
afterEach(async function () {
if (driver) {
// Send test result to Sauce Labs
const result = this.currentTest.state == 'passed' ?
'passed' : 'failed';
await driver.executeScript(`sauce:job-result=${result}`);
// Close the browser & end session
await driver.quit();
}
});
// A helper function to start a web search
const search = async (term) => {
// Automate DuckDuckGo search
await driver.get('https://duckduckgo.com/');
const searchBox = await driver.findElement(
By.id('searchbox_input'));
await searchBox.sendKeys(term, Key.ENTER);
// Wait until the result page is loaded
await driver.wait(until.elementLocated(By.css('#more-results')));
// Return page content
return await driver.getPageSource();
};
// Our test definitions
it('should search for "Selenium"', async function () {
const content = await search('Selenium');
assert.isTrue(content.includes('www.selenium.dev'));
});
it('should search for "Appium"', async function () {
const content = await search('Appium');
assert.isTrue(content.includes('appium.io'));
});
it('should search for "Mozilla"', async function () {
const content = await search('Mozilla');
assert.isTrue(content.includes('mozilla.org'));
});
it('should search for "GitHub"', async function () {
const content = await search('GitHub');
assert.isTrue(content.includes('github.com'));
});
it('should search for "GitLab"', async function () {
const content = await search('GitLab');
assert.isTrue(content.includes('gitlab.com'));
});
});