Path: blob/trunk/javascript/selenium-webdriver/test/lib/by_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 by = require('selenium-webdriver/lib/by')2122describe('by', function () {23describe('By', function () {24describe('className', function () {25it('delegates to By.css', function () {26let locator = by.By.className('foo')27assert.strictEqual('css selector', locator.using)28assert.strictEqual('.foo', locator.value)29})3031it('escapes class name', function () {32let locator = by.By.className('foo#bar')33assert.strictEqual('css selector', locator.using)34assert.strictEqual('.foo\\#bar', locator.value)35})3637it('translates compound class names', function () {38let locator = by.By.className('a b')39assert.strictEqual('css selector', locator.using)40assert.strictEqual('.a.b', locator.value)4142locator = by.By.className(' x y z-1 "g" ')43assert.strictEqual('css selector', locator.using)44assert.strictEqual('.x.y.z-1.\\"g\\"', locator.value)45})46})4748describe('id', function () {49it('delegates to By.css', function () {50let locator = by.By.id('foo')51assert.strictEqual('css selector', locator.using)52assert.strictEqual('*[id="foo"]', locator.value)53})5455it('escapes the ID', function () {56let locator = by.By.id('foo#bar')57assert.strictEqual('css selector', locator.using)58assert.strictEqual('*[id="foo\\#bar"]', locator.value)59})60})6162describe('name', function () {63it('delegates to By.css', function () {64let locator = by.By.name('foo')65assert.strictEqual('css selector', locator.using)66assert.strictEqual('*[name="foo"]', locator.value)67})6869it('escapes the name', function () {70let locator = by.By.name('foo"bar"')71assert.strictEqual('css selector', locator.using)72assert.strictEqual('*[name="foo\\"bar\\""]', locator.value)73})7475it('escapes the name when it starts with a number', function () {76let locator = by.By.name('123foo"bar"')77assert.strictEqual('css selector', locator.using)78assert.strictEqual('*[name="\\31 23foo\\"bar\\""]', locator.value)79})8081it('escapes the name when it starts with a negative number', function () {82let locator = by.By.name('-123foo"bar"')83assert.strictEqual('css selector', locator.using)84assert.strictEqual('*[name="-\\31 23foo\\"bar\\""]', locator.value)85})86})87})8889describe('RelativeBy', function () {90it('marshalls the RelativeBy object', function () {91let relative = by.locateWith(by.By.tagName('p')).above(by.By.name('foobar'))9293let expected = {94relative: {95root: { 'tag name': 'p' },96filters: [{ kind: 'above', args: [{ 'css selector': '*[name="foobar"]' }] }],97},98}99assert.deepStrictEqual(relative.marshall(), expected)100})101})102103describe('checkedLocator', function () {104it('accepts a By instance', function () {105let original = by.By.name('foo')106let locator = by.checkedLocator(original)107assert.strictEqual(locator, original)108})109110it('accepts custom locator functions', function () {111let original = function () {}112let locator = by.checkedLocator(original)113assert.strictEqual(locator, original)114})115116// See https://github.com/SeleniumHQ/selenium/issues/3056117it('accepts By-like objects', function () {118let fakeBy = { using: 'id', value: 'foo' }119let locator = by.checkedLocator(fakeBy)120assert.strictEqual(locator.constructor, by.By)121assert.strictEqual(locator.using, 'id')122assert.strictEqual(locator.value, 'foo')123})124125it('accepts class name', function () {126let locator = by.checkedLocator({ className: 'foo' })127assert.strictEqual('css selector', locator.using)128assert.strictEqual('.foo', locator.value)129})130131it('accepts css', function () {132let locator = by.checkedLocator({ css: 'a > b' })133assert.strictEqual('css selector', locator.using)134assert.strictEqual('a > b', locator.value)135})136137it('accepts id', function () {138let locator = by.checkedLocator({ id: 'foobar' })139assert.strictEqual('css selector', locator.using)140assert.strictEqual('*[id="foobar"]', locator.value)141})142143it('accepts linkText', function () {144let locator = by.checkedLocator({ linkText: 'hello' })145assert.strictEqual('link text', locator.using)146assert.strictEqual('hello', locator.value)147})148149it('accepts name', function () {150let locator = by.checkedLocator({ name: 'foobar' })151assert.strictEqual('css selector', locator.using)152assert.strictEqual('*[name="foobar"]', locator.value)153})154155it('accepts partialLinkText', function () {156let locator = by.checkedLocator({ partialLinkText: 'hello' })157assert.strictEqual('partial link text', locator.using)158assert.strictEqual('hello', locator.value)159})160161it('accepts tagName', function () {162let locator = by.checkedLocator({ tagName: 'div' })163assert.strictEqual('tag name', locator.using)164assert.strictEqual('div', locator.value)165})166167it('accepts xpath', function () {168let locator = by.checkedLocator({ xpath: '//div[1]' })169assert.strictEqual('xpath', locator.using)170assert.strictEqual('//div[1]', locator.value)171})172})173})174175176