Path: blob/trunk/javascript/selenium-webdriver/test/page_loading_test.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const error = require('selenium-webdriver/lib/error')21const test = require('../lib/test')22const { Browser, By, until } = require('selenium-webdriver')23const Pages = test.Pages2425test.suite(function (env) {26var driver27before(async function () {28driver = await env.builder().build()29})3031beforeEach(async function () {32if (!driver) {33driver = await env.builder().build()34}35})3637after(function () {38if (driver) {39return driver.quit()40}41})4243it('should wait for document to be loaded', async function () {44await driver.get(Pages.simpleTestPage)45assert.strictEqual(await driver.getTitle(), 'Hello WebDriver')46})4748it('should follow redirects sent in the http response headers', async function () {49await driver.get(Pages.redirectPage)50assert.strictEqual(await driver.getTitle(), 'We Arrive Here')51})5253it('should be able to get a fragment on the current page', async function () {54await driver.get(Pages.xhtmlTestPage)55await driver.get(Pages.xhtmlTestPage + '#text')56await driver.findElement(By.id('id1'))57})5859it('should wait for all frames to load in a frameset', async function () {60await driver.get(Pages.framesetPage)61await driver.switchTo().frame(0)6263let txt = await driver.findElement(By.css('span#pageNumber')).getText()64assert.strictEqual(txt.trim(), '1')6566await driver.switchTo().defaultContent()67await driver.switchTo().frame(1)68txt = await driver.findElement(By.css('span#pageNumber')).getText()6970assert.strictEqual(txt.trim(), '2')7172// For safari, need to make sure browser is focused on the main frame or73// subsequent tests will fail.74if (env.browser.name === Browser.SAFARI) {75await driver.switchTo().defaultContent()76}77})7879it('should be able to navigate back in browser history', async function () {80await driver.get(Pages.formPage)8182await driver.findElement(By.id('imageButton')).click()83await driver.wait(until.titleIs('We Arrive Here'), 2500)8485await driver.navigate().back()86await driver.wait(until.titleIs('We Leave From Here'), 2500)87})8889it('should be able to navigate back in presence of iframes', async function () {90await driver.get(Pages.xhtmlTestPage)9192await driver.findElement(By.name('sameWindow')).click()93await driver.wait(until.titleIs('This page has iframes'), 2500)9495await driver.navigate().back()96await driver.wait(until.titleIs('XHTML Test Page'), 2500)97})9899it('should be able to navigate forwards in browser history', async function () {100await driver.get(Pages.formPage)101102await driver.findElement(By.id('imageButton')).click()103await driver.wait(until.titleIs('We Arrive Here'), 5000)104105await driver.navigate().back()106await driver.wait(until.titleIs('We Leave From Here'), 5000)107108await driver.navigate().forward()109await driver.wait(until.titleIs('We Arrive Here'), 5000)110})111112it('should be able to refresh a page', async function () {113await driver.get(Pages.xhtmlTestPage)114115await driver.navigate().refresh()116117assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')118})119120it('should return title of page if set', async function () {121await driver.get(Pages.xhtmlTestPage)122assert.strictEqual(await driver.getTitle(), 'XHTML Test Page')123124await driver.get(Pages.simpleTestPage)125assert.strictEqual(await driver.getTitle(), 'Hello WebDriver')126})127128describe('timeouts', function () {129afterEach(function () {130let nullDriver = () => (driver = null)131if (driver) {132return driver.quit().then(nullDriver, nullDriver)133}134})135136it('should timeout if page load timeout is set', async function () {137await driver.manage().setTimeouts({ pageLoad: 1 })138return driver.get(Pages.sleepingPage + '?time=3').then(139function () {140throw Error('Should have timed out on page load')141},142function (e) {143if (!(e instanceof error.ScriptTimeoutError) && !(e instanceof error.TimeoutError)) {144throw Error('Unexpected error response: ' + e)145}146},147)148})149})150})151152153