Path: blob/trunk/javascript/selenium-webdriver/test/element_finding_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 promise = require('selenium-webdriver/lib/promise')21const { Browser, By, RelativeBy, error, withTagName, until } = require('selenium-webdriver')22const { Pages, ignore, suite, whereIs } = require('../lib/test')23const { locateWith } = require('selenium-webdriver/lib/by')2425suite(function (env) {26const browsers = (...args) => env.browsers(...args)2728let driver2930before(async function () {31driver = await env.builder().build()32})3334after(function () {35return driver.quit()36})3738describe('finding elements', function () {39it('should work after loading multiple pages in a row', async function () {40await driver.get(Pages.formPage)41await driver.get(Pages.xhtmlTestPage)42await driver.findElement(By.linkText('click me')).click()43await driver.wait(until.titleIs('We Arrive Here'), 5000)44})4546describe('By.id()', function () {47it('should work', async function () {48await driver.get(Pages.xhtmlTestPage)49await driver.findElement(By.id('linkId')).click()50await driver.wait(until.titleIs('We Arrive Here'), 5000)51})5253it('should fail if ID not present on page', async function () {54await driver.get(Pages.formPage)55return driver.findElement(By.id('nonExistentButton')).then(assert.fail, function (e) {56assert.ok(e instanceof error.NoSuchElementError)57})58})5960it('should find multiple elements by ID even though that is ' + 'malformed HTML', async function () {61await driver.get(Pages.nestedPage)6263let elements = await driver.findElements(By.id('2'))64assert.strictEqual(elements.length, 8)65})66})6768describe('By.linkText()', function () {69it('should be able to click on link identified by text', async function () {70await driver.get(Pages.xhtmlTestPage)71await driver.findElement(By.linkText('click me')).click()72await driver.wait(until.titleIs('We Arrive Here'), 5000)73})7475it('should be able to find elements by partial link text', async function () {76await driver.get(Pages.xhtmlTestPage)77await driver.findElement(By.partialLinkText('ick me')).click()78await driver.wait(until.titleIs('We Arrive Here'), 5000)79})8081it('should work when link text contains equals sign', async function () {82await driver.get(Pages.xhtmlTestPage)83let el = await driver.findElement(By.linkText('Link=equalssign'))8485let id = await el.getAttribute('id')86assert.strictEqual(id, 'linkWithEqualsSign')87})8889it('matches by partial text when containing equals sign', async function () {90await driver.get(Pages.xhtmlTestPage)91let link = await driver.findElement(By.partialLinkText('Link='))9293let id = await link.getAttribute('id')94assert.strictEqual(id, 'linkWithEqualsSign')95})9697it('works when searching for multiple and text contains =', async function () {98await driver.get(Pages.xhtmlTestPage)99let elements = await driver.findElements(By.linkText('Link=equalssign'))100101assert.strictEqual(elements.length, 1)102103let id = await elements[0].getAttribute('id')104assert.strictEqual(id, 'linkWithEqualsSign')105})106107it('works when searching for multiple with partial text containing =', async function () {108await driver.get(Pages.xhtmlTestPage)109let elements = await driver.findElements(By.partialLinkText('Link='))110111assert.strictEqual(elements.length, 1)112113let id = await elements[0].getAttribute('id')114assert.strictEqual(id, 'linkWithEqualsSign')115})116117it('should be able to find multiple exact matches', async function () {118await driver.get(Pages.xhtmlTestPage)119let elements = await driver.findElements(By.linkText('click me'))120assert.strictEqual(elements.length, 2)121})122123it('should be able to find multiple partial matches', async function () {124await driver.get(Pages.xhtmlTestPage)125let elements = await driver.findElements(By.partialLinkText('ick me'))126assert.strictEqual(elements.length, 2)127})128129ignore(browsers(Browser.SAFARI)).it('works on XHTML pages', async function () {130await driver.get(whereIs('actualXhtmlPage.xhtml'))131132let el = await driver.findElement(By.linkText('Foo'))133assert.strictEqual(await el.getText(), 'Foo')134})135})136137describe('By.name()', function () {138it('should work', async function () {139await driver.get(Pages.formPage)140141let el = await driver.findElement(By.name('checky'))142assert.strictEqual(await el.getAttribute('value'), 'furrfu')143})144145it('should find multiple elements with same name', async function () {146await driver.get(Pages.nestedPage)147148let elements = await driver.findElements(By.name('checky'))149assert.ok(elements.length > 1)150})151152it('should be able to find elements that do not support name property', async function () {153await driver.get(Pages.nestedPage)154await driver.findElement(By.name('div1'))155// Pass if this does not return an error.156})157158it('should be able to find hidden elements by name', async function () {159await driver.get(Pages.formPage)160await driver.findElement(By.name('hidden'))161// Pass if this does not return an error.162})163})164165describe('By.className()', function () {166it('should work', async function () {167await driver.get(Pages.xhtmlTestPage)168169let el = await driver.findElement(By.className('extraDiv'))170let text = await el.getText()171assert.ok(text.startsWith('Another div starts here.'), `Unexpected text: "${text}"`)172})173174it('should work when name is first name among many', async function () {175await driver.get(Pages.xhtmlTestPage)176177let el = await driver.findElement(By.className('nameA'))178assert.strictEqual(await el.getText(), 'An H2 title')179})180181it('should work when name is last name among many', async function () {182await driver.get(Pages.xhtmlTestPage)183184let el = await driver.findElement(By.className('nameC'))185assert.strictEqual(await el.getText(), 'An H2 title')186})187188it('should work when name is middle of many', async function () {189await driver.get(Pages.xhtmlTestPage)190191let el = await driver.findElement(By.className('nameBnoise'))192assert.strictEqual(await el.getText(), 'An H2 title')193})194195it('should work when name surrounded by whitespace', async function () {196await driver.get(Pages.xhtmlTestPage)197198let el = await driver.findElement(By.className('spaceAround'))199assert.strictEqual(await el.getText(), 'Spaced out')200})201202it('should fail if queried name only partially matches', async function () {203await driver.get(Pages.xhtmlTestPage)204return driver.findElement(By.className('nameB')).then(assert.fail, function (e) {205assert.ok(e instanceof error.NoSuchElementError)206})207})208209it('should implicitly wait', async function () {210const TIMEOUT_IN_MS = 1000211const EPSILON = TIMEOUT_IN_MS / 2212213await driver.manage().setTimeouts({ implicit: TIMEOUT_IN_MS })214await driver.get(Pages.formPage)215216let start = new Date()217return driver.findElement(By.id('nonExistentButton')).then(assert.fail, function (e) {218let end = new Date()219assert.ok(e instanceof error.NoSuchElementError)220221let elapsed = end - start222let diff = Math.abs(elapsed - TIMEOUT_IN_MS)223assert.ok(diff < EPSILON, `Expected ${TIMEOUT_IN_MS} \u00b1 ${EPSILON} but got ${elapsed}`)224})225})226227it('should be able to find multiple matches', async function () {228await driver.get(Pages.xhtmlTestPage)229230let elements = await driver.findElements(By.className('nameC'))231assert.ok(elements.length > 1)232})233234it('permits compound class names', function () {235return driver236.get(Pages.xhtmlTestPage)237.then(() => driver.findElement(By.className('nameA nameC')))238.then((el) => el.getText())239.then((text) => assert.strictEqual(text, 'An H2 title'))240})241})242243describe('By.xpath()', function () {244it('should work with multiple matches', async function () {245await driver.get(Pages.xhtmlTestPage)246let elements = await driver.findElements(By.xpath('//div'))247assert.ok(elements.length > 1)248})249250it('should work for selectors using contains keyword', async function () {251await driver.get(Pages.nestedPage)252await driver.findElement(By.xpath('//a[contains(., "hello world")]'))253// Pass if no error.254})255})256257describe('By.tagName()', function () {258it('works', async function () {259await driver.get(Pages.formPage)260261let el = await driver.findElement(By.tagName('input'))262assert.strictEqual((await el.getTagName()).toLowerCase(), 'input')263})264265it('can find multiple elements', async function () {266await driver.get(Pages.formPage)267268let elements = await driver.findElements(By.tagName('input'))269assert.ok(elements.length > 1)270})271})272273describe('By.css()', function () {274it('works', async function () {275await driver.get(Pages.xhtmlTestPage)276await driver.findElement(By.css('div.content'))277// Pass if no error.278})279280it('can find multiple elements', async function () {281await driver.get(Pages.xhtmlTestPage)282283let elements = await driver.findElements(By.css('p'))284assert.ok(elements.length > 1)285// Pass if no error.286})287288it('should find first matching element when searching by ' + 'compound CSS selector', async function () {289await driver.get(Pages.xhtmlTestPage)290291let el = await driver.findElement(By.css('div.extraDiv, div.content'))292assert.strictEqual(await el.getAttribute('class'), 'content')293})294295it('should be able to find multiple elements by compound selector', async function () {296await driver.get(Pages.xhtmlTestPage)297let elements = await driver.findElements(By.css('div.extraDiv, div.content'))298299return Promise.all([assertClassIs(elements[0], 'content'), assertClassIs(elements[1], 'extraDiv')])300301async function assertClassIs(el, expected) {302let clazz = await el.getAttribute('class')303assert.strictEqual(clazz, expected)304}305})306307// IE only supports short version option[selected].308ignore(browsers(Browser.INTERNET_EXPLORER)).it(309'should be able to find element by boolean attribute',310async function () {311await driver.get(whereIs('locators_tests/boolean_attribute_selected.html'))312313let el = await driver.findElement(By.css('option[selected="selected"]'))314assert.strictEqual(await el.getAttribute('value'), 'two')315},316)317318it('should be able to find element with short ' + 'boolean attribute selector', async function () {319await driver.get(whereIs('locators_tests/boolean_attribute_selected.html'))320321let el = await driver.findElement(By.css('option[selected]'))322assert.strictEqual(await el.getAttribute('value'), 'two')323})324325it('should be able to find element with short boolean attribute ' + 'selector on HTML4 page', async function () {326await driver.get(whereIs('locators_tests/boolean_attribute_selected_html4.html'))327328let el = await driver.findElement(By.css('option[selected]'))329assert.strictEqual(await el.getAttribute('value'), 'two')330})331})332333describe('by custom locator', function () {334it('handles single element result', async function () {335await driver.get(Pages.javascriptPage)336337let link = await driver.findElement(function (driver) {338let links = driver.findElements(By.tagName('a'))339return promise340.filter(links, function (link) {341return link.getAttribute('id').then((id) => id === 'updatediv')342})343.then((links) => links[0])344})345346let text = await link.getText()347let regex = /Update\s+a\s+div/348assert.ok(regex.test(text), `"${text}" does not match ${regex}`)349})350351it('uses first element if locator resolves to list', async function () {352await driver.get(Pages.javascriptPage)353354let link = await driver.findElement(function () {355return driver.findElements(By.tagName('a'))356})357358assert.strictEqual(await link.getText(), 'Change the page title!')359})360361it('fails if locator returns non-webelement value', async function () {362await driver.get(Pages.javascriptPage)363364let link = driver.findElement(function () {365return driver.getTitle()366})367368return link.then(369() => assert.fail('Should have failed'),370(e) => assert.ok(e instanceof TypeError),371)372})373})374375describe('RelativeBy to find element', function () {376it('finds an element above', async function () {377await driver.get(Pages.relativeLocators)378let below = await driver.findElement(By.id('below'))379let elements = await driver.findElements(withTagName('p').above(below))380let ids = []381assert.strictEqual(elements.length, 2)382for (let i = 0; i < elements.length; i++) {383ids.push(await elements[i].getAttribute('id'))384}385assert.deepStrictEqual(ids, ['mid', 'above'])386})387388it('should combine filters', async function () {389await driver.get(Pages.relativeLocators)390391let elements = await driver.findElements(withTagName('td').above(By.id('center')).toRightOf(By.id('top')))392let ids = []393for (let i = 0; i < elements.length; i++) {394ids.push(await elements[i].getAttribute('id'))395}396assert.notDeepStrictEqual(ids.indexOf('topRight'), -1, `Elements are ${ids}`)397})398})399400describe('RelativeBy with findElement', function () {401it('finds an element above', async function () {402await driver.get(Pages.relativeLocators)403let below = await driver.findElement(By.id('below'))404let element = await driver.findElement(withTagName('p').above(below))405assert.deepStrictEqual(await element.getAttribute('id'), `mid`)406})407408it('should combine filters', async function () {409await driver.get(Pages.relativeLocators)410let element = await driver.findElement(withTagName('td').above(By.id('center')).toRightOf(By.id('top')))411assert.deepStrictEqual(await element.getAttribute('id'), `topRight`)412})413414it('should search by passing in a by object', async function () {415await driver.get(Pages.relativeLocators)416let relativeLocator = locateWith(By.css('p')).above(await driver.findElement(By.id('below')))417assert.ok(relativeLocator instanceof RelativeBy)418419let element = await driver.findElement(relativeLocator)420assert.deepStrictEqual(await element.getAttribute('id'), 'mid')421})422})423424describe('switchTo().activeElement()', function () {425// SAFARI's new session response does not identify it as a W3C browser,426// so the command is sent in the unsupported wire protocol format.427ignore(browsers(Browser.SAFARI)).it('returns document.activeElement', async function () {428await driver.get(Pages.formPage)429430let email = await driver.findElement(By.css('#email'))431await driver.executeScript('arguments[0].focus()', email)432433let ae = await driver.switchTo().activeElement()434let equal = await driver.executeScript('return arguments[0] === arguments[1]', email, ae)435assert.ok(equal)436})437})438})439})440441442