Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/page_loading_test.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const error = require('selenium-webdriver/lib/error')
22
const test = require('../lib/test')
23
const { Browser, By, until } = require('selenium-webdriver')
24
const Pages = test.Pages
25
26
test.suite(function (env) {
27
var driver
28
before(async function () {
29
driver = await env.builder().build()
30
})
31
32
beforeEach(async function () {
33
if (!driver) {
34
driver = await env.builder().build()
35
}
36
})
37
38
after(function () {
39
if (driver) {
40
return driver.quit()
41
}
42
})
43
44
it('should wait for document to be loaded', async function () {
45
await driver.get(Pages.simpleTestPage)
46
assert.strictEqual(await driver.getTitle(), 'Hello WebDriver')
47
})
48
49
it('should follow redirects sent in the http response headers', async function () {
50
await driver.get(Pages.redirectPage)
51
assert.strictEqual(await driver.getTitle(), 'We Arrive Here')
52
})
53
54
it('should be able to get a fragment on the current page', async function () {
55
await driver.get(Pages.xhtmlTestPage)
56
await driver.get(Pages.xhtmlTestPage + '#text')
57
await driver.findElement(By.id('id1'))
58
})
59
60
it('should wait for all frames to load in a frameset', async function () {
61
await driver.get(Pages.framesetPage)
62
await driver.switchTo().frame(0)
63
64
let txt = await driver.findElement(By.css('span#pageNumber')).getText()
65
assert.strictEqual(txt.trim(), '1')
66
67
await driver.switchTo().defaultContent()
68
await driver.switchTo().frame(1)
69
txt = await driver.findElement(By.css('span#pageNumber')).getText()
70
71
assert.strictEqual(txt.trim(), '2')
72
73
// For safari, need to make sure browser is focused on the main frame or
74
// subsequent tests will fail.
75
if (env.browser.name === Browser.SAFARI) {
76
await driver.switchTo().defaultContent()
77
}
78
})
79
80
it('should be able to navigate back in browser history', async function () {
81
await driver.get(Pages.formPage)
82
83
await driver.findElement(By.id('imageButton')).click()
84
await driver.wait(until.titleIs('We Arrive Here'), 2500)
85
86
await driver.navigate().back()
87
await driver.wait(until.titleIs('We Leave From Here'), 2500)
88
})
89
90
it('should be able to navigate back in presence of iframes', async function () {
91
await driver.get(Pages.xhtmlTestPage)
92
93
await driver.findElement(By.name('sameWindow')).click()
94
await driver.wait(until.titleIs('This page has iframes'), 2500)
95
96
await driver.navigate().back()
97
await driver.wait(until.titleIs('XHTML Test Page'), 2500)
98
})
99
100
it('should be able to navigate forwards in browser history', async function () {
101
await driver.get(Pages.formPage)
102
103
await driver.findElement(By.id('imageButton')).click()
104
await driver.wait(until.titleIs('We Arrive Here'), 5000)
105
106
await driver.navigate().back()
107
await driver.wait(until.titleIs('We Leave From Here'), 5000)
108
109
await driver.navigate().forward()
110
await driver.wait(until.titleIs('We Arrive Here'), 5000)
111
})
112
113
it('should be able to refresh a page', async function () {
114
await driver.get(Pages.xhtmlTestPage)
115
116
await driver.navigate().refresh()
117
118
assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')
119
})
120
121
it('should return title of page if set', async function () {
122
await driver.get(Pages.xhtmlTestPage)
123
assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')
124
125
await driver.get(Pages.simpleTestPage)
126
assert.strictEqual(await driver.getTitle(), 'Hello WebDriver')
127
})
128
129
describe('timeouts', function () {
130
afterEach(function () {
131
let nullDriver = () => (driver = null)
132
if (driver) {
133
return driver.quit().then(nullDriver, nullDriver)
134
}
135
})
136
137
it('should timeout if page load timeout is set', async function () {
138
await driver.manage().setTimeouts({ pageLoad: 1 })
139
return driver.get(Pages.sleepingPage + '?time=3').then(
140
function () {
141
throw Error('Should have timed out on page load')
142
},
143
function (e) {
144
if (!(e instanceof error.ScriptTimeoutError) && !(e instanceof error.TimeoutError)) {
145
throw Error('Unexpected error response: ' + e)
146
}
147
},
148
)
149
})
150
})
151
})
152
153