-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathwindows.spec.js
118 lines (93 loc) · 4.18 KB
/
windows.spec.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
const {Builder, By} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const assert = require("node:assert");
let opts = new chrome.Options();
opts.addArguments('--headless');
let startIndex = 0
let endIndex = 5
let pdfMagicNumber = 'JVBER'
let imgMagicNumber = 'iVBOR'
let base64Code
describe('Interactions - Windows', function () {
let driver;
before(async function () {
driver = await new Builder().forBrowser('chrome').setChromeOptions(opts).build();
});
after(async () => await driver.quit());
it('Should be able to print page to pdf', async function () {
await driver.get('https://www.selenium.dev/selenium/web/alerts.html');
let base64 = await driver.printPage({pageRanges: ["1-2"]});
// page can be saved as a PDF as below
// await fs.writeFileSync('./test.pdf', base64, 'base64');
base64Code = base64.slice(startIndex, endIndex)
assert.strictEqual(base64Code, pdfMagicNumber)
});
it('Should be able to get text using executeScript', async function () {
await driver.get('https://www.selenium.dev/selenium/web/javascriptPage.html');
// Stores the header element
let header = await driver.findElement(By.css('h1'));
// Executing JavaScript to capture innerText of header element
let text = await driver.executeScript('return arguments[0].innerText', header);
assert.strictEqual(text, `Type Stuff`)
});
it('Should be able to take Element Screenshot', async function () {
await driver.get('https://www.selenium.dev/selenium/web/javascriptPage.html');
let header = await driver.findElement(By.css('h1'));
// Captures the element screenshot
let encodedString = await header.takeScreenshot(true);
// save screenshot as below
// await fs.writeFileSync('./image.png', encodedString, 'base64');
base64Code = encodedString.slice(startIndex, endIndex)
assert.strictEqual(base64Code, imgMagicNumber)
});
it('Should be able to takeScreenshot', async function () {
await driver.get('https://www.selenium.dev/selenium/web/javascriptPage.html');
// Captures the screenshot
let encodedString = await driver.takeScreenshot();
// save screenshot as below
// await fs.writeFileSync('./image.png', encodedString, 'base64');
base64Code = encodedString.slice(startIndex, endIndex)
assert.strictEqual(base64Code, imgMagicNumber)
});
it('Should be able to switch to newWindow and newTab and close', async function () {
await driver.get('https://www.selenium.dev/selenium/web/');
const initialWindow = await driver.getAllWindowHandles();
assert.strictEqual(initialWindow.length, 1)
// Opens a new tab and switches to new tab
await driver.switchTo().newWindow('tab');
const browserTabs = await driver.getAllWindowHandles();
assert.strictEqual(browserTabs.length, 2)
// Opens a new window and switches to new window
await driver.switchTo().newWindow('window');
const windows = await driver.getAllWindowHandles();
assert.strictEqual(windows.length, 3)
//Close the tab or window
await driver.close();
//Switch back to the old tab or window
await driver.switchTo().window(windows[1]);
const windowsAfterClose = await driver.getAllWindowHandles();
assert.strictEqual(windowsAfterClose.length, 2);
});
it('Should be able to getWindow Size', async function () {
await driver.get('https://www.selenium.dev/selenium/web/');
// Access each dimension individually
const { width, height } = await driver.manage().window().getRect();
// Or store the dimensions and query them later
const rect = await driver.manage().window().getRect();
const windowWidth = rect.width;
const windowHeight = rect.height;
assert.ok(windowWidth>0);
assert.ok(windowHeight>0);
});
it('Should be able to getWindow position', async function () {
await driver.get('https://www.selenium.dev/selenium/web/');
// Access each dimension individually
const { x, y } = await driver.manage().window().getRect();
// Or store the dimensions and query them later
const rect = await driver.manage().window().getRect();
const x1 = rect.x;
const y1 = rect.y;
assert.ok(x1>=0);
assert.ok(y1>=0);
});
});