Path: blob/trunk/javascript/selenium-webdriver/test/bidi/locate_nodes_test.js
2885 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 { Browser } = require('selenium-webdriver')21const { Pages, suite, ignore } = require('../../lib/test')22const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')23const { Locator } = require('selenium-webdriver/bidi/browsingContext')24const { ScriptManager } = require('selenium-webdriver/index')25const { EvaluateResultType } = require('selenium-webdriver/bidi/evaluateResult')26const { LocalValue, ReferenceValue } = require('selenium-webdriver/bidi/protocolValue')2728suite(29function (env) {30let driver3132beforeEach(async function () {33driver = await env.builder().build()34})3536afterEach(async function () {37await driver.quit()38})3940describe('Locate Nodes', function () {41it('can locate nodes', async function () {42const id = await driver.getWindowHandle()43const browsingContext = await BrowsingContext(driver, {44browsingContextId: id,45})4647await driver.get(Pages.xhtmlTestPage)4849const element = await browsingContext.locateNodes(Locator.css('div'))50assert.strictEqual(element.length, 13)51})5253it('can locate node', async function () {54const id = await driver.getWindowHandle()55const browsingContext = await BrowsingContext(driver, {56browsingContextId: id,57})5859await driver.get(Pages.xhtmlTestPage)6061const element = await browsingContext.locateNode(Locator.css('div'))62assert.strictEqual(element.type, 'node')63})6465it('can locate node with css locator', async function () {66const id = await driver.getWindowHandle()67const browsingContext = await BrowsingContext(driver, {68browsingContextId: id,69})7071await driver.get(Pages.xhtmlTestPage)7273const elements = await browsingContext.locateNodes(Locator.css('div.extraDiv, div.content'), 1)74const element = elements[0]75assert.strictEqual(element.type, 'node')76assert.notEqual(element.value, undefined)77assert.strictEqual(element.value.localName, 'div')78assert.strictEqual(element.value.attributes.class, 'content')79assert.notEqual(element.sharedId, undefined)80})8182it('can locate node with xpath locator', async function () {83const id = await driver.getWindowHandle()84const browsingContext = await BrowsingContext(driver, {85browsingContextId: id,86})8788await driver.get(Pages.xhtmlTestPage)89const elements = await browsingContext.locateNodes(Locator.xpath('/html/body/div[2]'), 1)9091const element = elements[0]92assert.strictEqual(element.type, 'node')93assert.notEqual(element.value, undefined)94assert.strictEqual(element.value.localName, 'div')95assert.strictEqual(element.value.attributes.class, 'content')96assert.notEqual(element.sharedId, undefined)97})9899ignore(env.browsers(Browser.FIREFOX)).it('can locate node with inner test locator', async function () {100const id = await driver.getWindowHandle()101const browsingContext = await BrowsingContext(driver, {102browsingContextId: id,103})104105await driver.get(Pages.xhtmlTestPage)106const elements = await browsingContext.locateNodes(Locator.innerText('Spaced out'), 1)107108const element = elements[0]109assert.strictEqual(element.type, 'node')110assert.notEqual(element.value, undefined)111})112113it('can locate node with max node count', async function () {114const id = await driver.getWindowHandle()115const browsingContext = await BrowsingContext(driver, {116browsingContextId: id,117})118119await driver.get(Pages.xhtmlTestPage)120121const elements = await browsingContext.locateNodes(Locator.css('div'), 4)122assert.strictEqual(elements.length, 4)123})124125it('can locate node with given start nodes', async function () {126const id = await driver.getWindowHandle()127const browsingContext = await BrowsingContext(driver, {128browsingContextId: id,129})130131await driver.get(Pages.formPage)132133const script = await ScriptManager(id, driver)134135const result = await script.evaluateFunctionInBrowsingContext(136id,137"document.querySelectorAll('form')",138false,139'root',140)141142assert.equal(result.resultType, EvaluateResultType.SUCCESS)143assert.notEqual(result.realmId, null)144assert.equal(result.result.type, 'nodelist')145146const value = result.result.value147148const startNodes = []149150value.forEach((node) => {151startNodes.push(new ReferenceValue(node.handle, node.sharedId))152})153154const elements = await browsingContext.locateNodes(Locator.css('input'), 50, 'none', undefined, startNodes)155156assert.strictEqual(elements.length, 35)157})158159it('can locate nodes in a given sandbox', async function () {160const sandbox = 'sandbox'161const id = await driver.getWindowHandle()162const browsingContext = await BrowsingContext(driver, {163browsingContextId: id,164})165166await browsingContext.navigate(Pages.xhtmlTestPage, 'complete')167168const elements = await browsingContext.locateNodes(Locator.css('div'), 1, sandbox)169170assert.strictEqual(elements.length, 1)171172const nodeId = elements[0].sharedId173174const script = await ScriptManager(id, driver)175176let argumentValues = []177let mapValue = { sharedId: LocalValue.createStringValue(nodeId) }178argumentValues.push(LocalValue.createMapValue(mapValue))179180const response = await script.callFunctionInBrowsingContext(181id,182'function(){ return arguments[0]; }',183false,184argumentValues,185undefined,186undefined,187sandbox,188)189190assert.equal(response.resultType, EvaluateResultType.SUCCESS)191assert.equal(response.result.type, 'map')192193const value = response.result.value[0]194195assert.strictEqual(value[1].type, 'string')196assert.strictEqual(value[1].value, nodeId)197})198199it('can find element', async function () {200const id = await driver.getWindowHandle()201const browsingContext = await BrowsingContext(driver, {202browsingContextId: id,203})204205await driver.get(Pages.xhtmlTestPage)206207const element = await browsingContext.locateElement(Locator.css('p'))208const elementText = await element.getText()209assert.strictEqual(elementText, 'Open new window')210})211212it('can find elements', async function () {213const id = await driver.getWindowHandle()214const browsingContext = await BrowsingContext(driver, {215browsingContextId: id,216})217218await driver.get(Pages.xhtmlTestPage)219220const elements = await browsingContext.locateElements(Locator.css('div'))221assert.strictEqual(elements.length, 13)222223const elementText = await elements[0].getText()224assert.strictEqual(elementText.includes('Open new window'), true)225})226})227},228{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },229)230231232