Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/ie-driver/atoms.js
2867 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
/**
19
* @fileoverview IE specific atoms.
20
*/
21
22
goog.provide('webdriver.ie');
23
24
goog.require('bot.ErrorCode');
25
goog.require('bot.dom');
26
goog.require('bot.locators');
27
goog.require('bot.userAgent');
28
goog.require('goog.math.Coordinate');
29
goog.require('goog.style');
30
31
32
/**
33
* Find the first element in the DOM matching the mechanism and criteria.
34
*
35
* @param {!string} mechanism The mechanism to search by.
36
* @param {!string} criteria The criteria to search for.
37
* @param {?(Document|Element)=} opt_root The node from which to start the
38
* search. If not specified, will use `document` as the root.
39
* @return {!Object} An object containing the status of the find and
40
* the result (success will be the first matching element found in
41
* the DOM, failure will be the associated error message).
42
*/
43
webdriver.ie.findElement = function(mechanism, criteria, opt_root) {
44
var locator = {};
45
var retval = {};
46
locator[mechanism] = criteria;
47
try {
48
retval = bot.locators.findElement(locator, opt_root);
49
} catch (e) {
50
// The normal error case throws bot.Error, which has a 'code'
51
// property. In the case where the locator mechanism is unknown,
52
// findElement throws a plain JavaScript Error, which doesn't.
53
return {
54
'status': e.code || bot.ErrorCode.JAVASCRIPT_ERROR,
55
'value': e.message
56
};
57
}
58
if (retval == null) {
59
return { 'status': bot.ErrorCode.NO_SUCH_ELEMENT, 'value': retval };
60
}
61
return { 'status': bot.ErrorCode.SUCCESS, 'value': retval };
62
};
63
64
65
/**
66
* Find all elements in the DOM matching the mechanism and criteria.
67
*
68
* @param {!string} mechanism The mechanism to search by.
69
* @param {!string} criteria The criteria to search for.
70
* @param {?(Document|Element)=} opt_root The node from which to start the
71
* search. If not specified, will use `document` as the root.
72
* @return {!Object} An object containing the status of the find and
73
* the result (success will be the elements, failure will be the
74
* associated error message).
75
*/
76
webdriver.ie.findElements = function(mechanism, criteria, opt_root) {
77
// For finding multiple elements by class name in IE, if the document
78
// mode is below 8 (which includes quirks mode), the findElements atom
79
// drops into a branch of the Closure library that doesn't validate
80
// that the class name is valid, and therefore just returns an empty
81
// array. Thus, we pre-screen the class name in this special case.
82
// Note: the regex used herein may not be entirely correct; judging
83
// this to be an acceptable risk due to the (hopefully) limited nature
84
// of the bug.
85
if (mechanism == 'className' && bot.userAgent.IE_DOC_PRE8) {
86
var invalidTokenRegex = /[~!@\$%\^&\*\(\)_\+=,\.\/';:"\?><\[\]\\\{\}\|`#]+/;
87
if (invalidTokenRegex.test(criteria)) {
88
return { 'status': bot.ErrorCode.INVALID_SELECTOR_ERROR,
89
'value': 'Invalid character in class name.' };
90
}
91
}
92
var retval = {};
93
var locator = {};
94
locator[mechanism] = criteria;
95
try {
96
retval = bot.locators.findElements(locator, opt_root);
97
} catch (e) {
98
// The normal error case throws bot.Error, which has a 'code'
99
// property. In the case where the locator mechanism is unknown,
100
// findElement throws a plain JavaScript Error, which doesn't.
101
return {
102
'status': e.code || bot.ErrorCode.JAVASCRIPT_ERROR,
103
'value': e.message
104
};
105
}
106
return {'status': bot.ErrorCode.SUCCESS, 'value': retval};
107
};
108
109
110
/**
111
* Checks whether the element is currently scrolled into the parent's overflow
112
* region, such that the offset given, relative to the top-left corner of the
113
* element, is currently in the overflow region.
114
*
115
* @param {!Element} element The element to check.
116
* @param {number} x The horizontal offset from the top-left corner.
117
* @param {number} y The vertical offset from the top-left corner.
118
* @return {bot.dom.OverflowState} Whether the coordinates specified, relative to the element,
119
* are scrolled in the parent overflow.
120
*/
121
webdriver.ie.isOffsetInParentOverflow = function (element, x, y) {
122
var offsetPoint = new goog.math.Coordinate(x, y);
123
return bot.dom.getOverflowState(element, offsetPoint);
124
};
125
126
/**
127
* Checks whether the element is entirely scrolled into the parent's overflow
128
* region.
129
*
130
* @param {!Element} element The element to check.
131
* @return {bot.dom.OverflowState} Whether the coordinates specified, relative to the element,
132
* are scrolled in the parent overflow.
133
*/
134
webdriver.ie.isElementInParentOverflow = function (element) {
135
return bot.dom.getOverflowState(element);
136
};
137
138
/**
139
* Gets the size and location of an element.
140
*
141
* @param {!Element} element The element to get the rect of.
142
* @return {!Object} An object containing the element rect.
143
*/
144
webdriver.ie.getElementRect = function (element) {
145
// For now, we'll return a custom object. If we need
146
// more functionality later provided by goog.math.Rect,
147
// we can upgrade to that when needed.
148
var size = goog.style.getTransformedSize(element);
149
var location = goog.style.getPageOffset(element);
150
return {
151
'x': location.x, 'y': location.y,
152
'width': size.width, 'height': size.height
153
};
154
};
155
156