Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/by_test.js
2885 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 by = require('selenium-webdriver/lib/by')
22
23
describe('by', function () {
24
describe('By', function () {
25
describe('className', function () {
26
it('delegates to By.css', function () {
27
let locator = by.By.className('foo')
28
assert.strictEqual('css selector', locator.using)
29
assert.strictEqual('.foo', locator.value)
30
})
31
32
it('escapes class name', function () {
33
let locator = by.By.className('foo#bar')
34
assert.strictEqual('css selector', locator.using)
35
assert.strictEqual('.foo\\#bar', locator.value)
36
})
37
38
it('translates compound class names', function () {
39
let locator = by.By.className('a b')
40
assert.strictEqual('css selector', locator.using)
41
assert.strictEqual('.a.b', locator.value)
42
43
locator = by.By.className(' x y z-1 "g" ')
44
assert.strictEqual('css selector', locator.using)
45
assert.strictEqual('.x.y.z-1.\\"g\\"', locator.value)
46
})
47
})
48
49
describe('id', function () {
50
it('delegates to By.css', function () {
51
let locator = by.By.id('foo')
52
assert.strictEqual('css selector', locator.using)
53
assert.strictEqual('*[id="foo"]', locator.value)
54
})
55
56
it('escapes the ID', function () {
57
let locator = by.By.id('foo#bar')
58
assert.strictEqual('css selector', locator.using)
59
assert.strictEqual('*[id="foo\\#bar"]', locator.value)
60
})
61
})
62
63
describe('name', function () {
64
it('delegates to By.css', function () {
65
let locator = by.By.name('foo')
66
assert.strictEqual('css selector', locator.using)
67
assert.strictEqual('*[name="foo"]', locator.value)
68
})
69
70
it('escapes the name', function () {
71
let locator = by.By.name('foo"bar"')
72
assert.strictEqual('css selector', locator.using)
73
assert.strictEqual('*[name="foo\\"bar\\""]', locator.value)
74
})
75
76
it('escapes the name when it starts with a number', function () {
77
let locator = by.By.name('123foo"bar"')
78
assert.strictEqual('css selector', locator.using)
79
assert.strictEqual('*[name="\\31 23foo\\"bar\\""]', locator.value)
80
})
81
82
it('escapes the name when it starts with a negative number', function () {
83
let locator = by.By.name('-123foo"bar"')
84
assert.strictEqual('css selector', locator.using)
85
assert.strictEqual('*[name="-\\31 23foo\\"bar\\""]', locator.value)
86
})
87
})
88
})
89
90
describe('RelativeBy', function () {
91
it('marshalls the RelativeBy object', function () {
92
let relative = by.locateWith(by.By.tagName('p')).above(by.By.name('foobar'))
93
94
let expected = {
95
relative: {
96
root: { 'tag name': 'p' },
97
filters: [{ kind: 'above', args: [{ 'css selector': '*[name="foobar"]' }] }],
98
},
99
}
100
assert.deepStrictEqual(relative.marshall(), expected)
101
})
102
})
103
104
describe('checkedLocator', function () {
105
it('accepts a By instance', function () {
106
let original = by.By.name('foo')
107
let locator = by.checkedLocator(original)
108
assert.strictEqual(locator, original)
109
})
110
111
it('accepts custom locator functions', function () {
112
let original = function () {}
113
let locator = by.checkedLocator(original)
114
assert.strictEqual(locator, original)
115
})
116
117
// See https://github.com/SeleniumHQ/selenium/issues/3056
118
it('accepts By-like objects', function () {
119
let fakeBy = { using: 'id', value: 'foo' }
120
let locator = by.checkedLocator(fakeBy)
121
assert.strictEqual(locator.constructor, by.By)
122
assert.strictEqual(locator.using, 'id')
123
assert.strictEqual(locator.value, 'foo')
124
})
125
126
it('accepts class name', function () {
127
let locator = by.checkedLocator({ className: 'foo' })
128
assert.strictEqual('css selector', locator.using)
129
assert.strictEqual('.foo', locator.value)
130
})
131
132
it('accepts css', function () {
133
let locator = by.checkedLocator({ css: 'a > b' })
134
assert.strictEqual('css selector', locator.using)
135
assert.strictEqual('a > b', locator.value)
136
})
137
138
it('accepts id', function () {
139
let locator = by.checkedLocator({ id: 'foobar' })
140
assert.strictEqual('css selector', locator.using)
141
assert.strictEqual('*[id="foobar"]', locator.value)
142
})
143
144
it('accepts linkText', function () {
145
let locator = by.checkedLocator({ linkText: 'hello' })
146
assert.strictEqual('link text', locator.using)
147
assert.strictEqual('hello', locator.value)
148
})
149
150
it('accepts name', function () {
151
let locator = by.checkedLocator({ name: 'foobar' })
152
assert.strictEqual('css selector', locator.using)
153
assert.strictEqual('*[name="foobar"]', locator.value)
154
})
155
156
it('accepts partialLinkText', function () {
157
let locator = by.checkedLocator({ partialLinkText: 'hello' })
158
assert.strictEqual('partial link text', locator.using)
159
assert.strictEqual('hello', locator.value)
160
})
161
162
it('accepts tagName', function () {
163
let locator = by.checkedLocator({ tagName: 'div' })
164
assert.strictEqual('tag name', locator.using)
165
assert.strictEqual('div', locator.value)
166
})
167
168
it('accepts xpath', function () {
169
let locator = by.checkedLocator({ xpath: '//div[1]' })
170
assert.strictEqual('xpath', locator.using)
171
assert.strictEqual('//div[1]', locator.value)
172
})
173
})
174
})
175
176