Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/webdriver/atoms/inject/dom.js
2868 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 Ready to inject atoms for querying the DOM.
20
*/
21
22
goog.provide('webdriver.atoms.inject.dom');
23
24
goog.require('bot.dom');
25
goog.require('bot.inject');
26
goog.require('bot.userAgent');
27
goog.require('goog.json');
28
goog.require('webdriver.atoms.element');
29
goog.require('webdriver.atoms.inject');
30
31
32
/**
33
* Gets the visible text for the given element.
34
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
35
* @param {{WINDOW: string}=} opt_window The optional window
36
* containing the element.
37
* @return {string} The visible text wrapped in a JSON string as defined by the
38
* WebDriver wire protocol.
39
*/
40
webdriver.atoms.inject.dom.getText = function(element, opt_window) {
41
return webdriver.atoms.inject.dom.executeDomFunction_(
42
bot.dom.getVisibleText, [element], opt_window);
43
};
44
45
46
/**
47
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
48
* @param {{WINDOW: string}=} opt_window The optional window
49
* containing the element.
50
* @return {string} A boolean describing whether the element is
51
* checked or selected wrapped in a JSON string as defined by
52
* the wire protocol.
53
*/
54
webdriver.atoms.inject.dom.isSelected = function(element, opt_window) {
55
return webdriver.atoms.inject.dom.executeDomFunction_(
56
bot.dom.isSelected, [element], opt_window);
57
};
58
59
60
/**
61
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
62
* @param {{WINDOW: string}=} opt_window The optional window
63
* containing the element.
64
* @return {string} The coordinates of the top left corner in a JSON
65
* string as defined by the wire protocol.
66
*/
67
webdriver.atoms.inject.dom.getTopLeftCoordinates =
68
function(element, opt_window) {
69
return webdriver.atoms.inject.dom.executeDomFunction_(
70
webdriver.atoms.element.getLocationInView, [element], opt_window);
71
};
72
73
74
/**
75
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
76
* @param {string} attribute The attribute to look up.
77
* @param {{WINDOW: string}=} opt_window The optional window
78
* containing the element.
79
* @return {string} The requested attribute value in a JSON string
80
* as defined by the wire protocol.
81
*/
82
webdriver.atoms.inject.dom.getAttributeValue =
83
function(element, attribute, opt_window) {
84
return webdriver.atoms.inject.dom.executeDomFunction_(
85
webdriver.atoms.element.getAttribute, [element, attribute], opt_window);
86
};
87
88
89
/**
90
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
91
* @param {{WINDOW: string}=} opt_window The optional window
92
* containing the element.
93
* @return {string} The element size in a JSON string as
94
* defined by the wire protocol.
95
*/
96
webdriver.atoms.inject.dom.getSize = function(element, opt_window) {
97
return webdriver.atoms.inject.dom.executeDomFunction_(
98
getSize, [element], opt_window);
99
100
function getSize(e) {
101
var rect = bot.dom.getClientRect(e);
102
var height = rect.height;
103
var width = rect.width;
104
if (!bot.userAgent.IE_DOC_PRE10) {
105
// On IE10, getBoundingClientRect returns floating point values.
106
width = Math.floor(width);
107
height = Math.floor(height);
108
}
109
return { 'width': width, 'height': height };
110
}
111
};
112
113
114
/**
115
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
116
* @param {string} property The property to look up.
117
* @param {{WINDOW: string}=} opt_window The optional window
118
* containing the element.
119
* @return {string} The value of the requested CSS property in a JSON
120
* string as defined by the wire protocol.
121
*/
122
webdriver.atoms.inject.dom.getValueOfCssProperty =
123
function(element, property, opt_window) {
124
return webdriver.atoms.inject.dom.executeDomFunction_(
125
bot.dom.getEffectiveStyle, [element, property], opt_window);
126
};
127
128
129
/**
130
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.
131
* @param {{WINDOW: string}=} opt_window The optional window
132
* containing the element.
133
* @return {string} A boolean describing whether the element is enabled
134
* in a JSON string as defined by the wire protocol.
135
*/
136
webdriver.atoms.inject.dom.isEnabled = function(element, opt_window) {
137
return webdriver.atoms.inject.dom.executeDomFunction_(
138
bot.dom.isEnabled, [element], opt_window);
139
};
140
141
142
/**
143
* @param {{bot.inject.ELEMENT_KEY: string}} element The element to check.
144
* @param {{WINDOW: string}=} opt_window The optional window
145
* containing the element.
146
* @return {string} true if the element is visible, false otherwise.
147
* The result is wrapped in a JSON string as defined by the wire
148
* protocol.
149
*/
150
webdriver.atoms.inject.dom.isDisplayed = function(element, opt_window) {
151
return webdriver.atoms.inject.dom.executeDomFunction_(
152
bot.dom.isShown, [element, /*ignoreOpacity=*/true], opt_window);
153
};
154
155
156
/**
157
* @param {Function} fn The function to call.
158
* @param {Array.<*>} args An array of function arguments for the function.
159
* @param {{WINDOW: string}=} opt_window The window context for
160
* the execution of the function.
161
* @return {string} The serialized JSON wire protocol result of the function.
162
* @private
163
*/
164
webdriver.atoms.inject.dom.executeDomFunction_ =
165
function(fn, args, opt_window) {
166
var response;
167
try {
168
var targetWindow = webdriver.atoms.inject.getWindow(opt_window);
169
var unwrappedArgs = /**@type {Object}*/(bot.inject.unwrapValue(args,
170
targetWindow.document));
171
var functionResult = fn.apply(null, unwrappedArgs);
172
response = bot.inject.wrapResponse(functionResult);
173
} catch (ex) {
174
response = bot.inject.wrapError(ex);
175
}
176
return goog.json.serialize(response);
177
};
178
179