Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/lib/webelement.js
2884 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
const { isObject } = require('./util')
20
21
/**
22
* @fileoverview Defines some common methods used for WebElements.
23
*/
24
25
const LEGACY_ELEMENT_ID_KEY = 'ELEMENT'
26
const ELEMENT_ID_KEY = 'element-6066-11e4-a52e-4f735466cecf'
27
28
/**
29
* Contains logic about WebElements.
30
*/
31
/**
32
* @param {?} obj the object to test.
33
* @return {boolean} whether the object is a valid encoded WebElement ID.
34
*/
35
function isId(obj) {
36
return isObject(obj) && (typeof obj[ELEMENT_ID_KEY] === 'string' || typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string')
37
}
38
39
/**
40
* Extracts the encoded WebElement ID from the object.
41
*
42
* @param {?} obj The object to extract the ID from.
43
* @return {string} the extracted ID.
44
* @throws {TypeError} if the object is not a valid encoded ID.
45
*/
46
function extractId(obj) {
47
if (isObject(obj)) {
48
if (typeof obj[ELEMENT_ID_KEY] === 'string') {
49
return obj[ELEMENT_ID_KEY]
50
} else if (typeof obj[LEGACY_ELEMENT_ID_KEY] === 'string') {
51
return obj[LEGACY_ELEMENT_ID_KEY]
52
}
53
}
54
throw new TypeError('object is not a WebElement ID')
55
}
56
57
// PUBLIC API
58
59
module.exports = {
60
isId,
61
extractId,
62
}
63
64