blob: 96700d77f482897223b3df57fdcadc6568dc1281 [file] [log] [blame]
Dounia Berrada3f7480d2011-02-28 09:42:44 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit.webdriver;
18
19import com.android.internal.R;
20
Dounia Berrada77cdbc52011-04-11 11:36:04 -070021import java.util.List;
22
Dounia Berrada3f7480d2011-02-28 09:42:44 -080023/**
24 * Represents an HTML element. Typically most interactions with a web page
25 * will be performed through this class.
26 *
27 * @hide
28 */
29public class WebElement {
30 private final String mId;
31 private final WebDriver mDriver;
32
Dounia Berrada77cdbc52011-04-11 11:36:04 -070033 private static final String LOCATOR_ID = "id";
34 private static final String LOCATOR_LINK_TEXT = "linkText";
35 private static final String LOCATOR_PARTIAL_LINK_TEXT = "partialLinkText";
36 private static final String LOCATOR_NAME = "name";
37 private static final String LOCATOR_CLASS_NAME = "className";
38 private static final String LOCATOR_CSS = "css";
39 private static final String LOCATOR_TAG_NAME = "tagName";
40 private static final String LOCATOR_XPATH = "xpath";
41
Dounia Berrada3f7480d2011-02-28 09:42:44 -080042 /**
43 * Package constructor to prevent clients from creating a new WebElement
44 * instance.
45 *
46 * <p> A WebElement represents an HTML element on the page.
47 * The corresponding HTML element is stored in a JS cache in the page
48 * that can be accessed through JavaScript using "bot.inject.cache".
49 *
50 * @param driver The WebDriver instance to use.
Dounia Berrada77cdbc52011-04-11 11:36:04 -070051 * @param id The index of the HTML element in the JavaSctipt cache.
Dounia Berrada3f7480d2011-02-28 09:42:44 -080052 * document.documentElement object.
53 */
Dounia Berrada77cdbc52011-04-11 11:36:04 -070054 /* package */ WebElement(final WebDriver driver, final String id) {
Dounia Berrada3f7480d2011-02-28 09:42:44 -080055 this.mId = id;
56 this.mDriver = driver;
57 }
58
59 /**
60 * Finds the first {@link android.webkit.webdriver.WebElement} using the
61 * given method.
62 *
63 * @param by The locating mechanism to use.
64 * @return The first matching element on the current context.
65 */
66 public WebElement findElement(final By by) {
67 return by.findElement(this);
68 }
69
Dounia Berrada77cdbc52011-04-11 11:36:04 -070070 /**
71 * Finds all {@link android.webkit.webdriver.WebElement} within the page
72 * using the given method.
73 *
74 * @param by The locating mechanism to use.
75 * @return A list of all {@link android.webkit.webdriver.WebElement} found,
76 * or an empty list if nothing matches.
77 */
78 public List<WebElement> findElements(final By by) {
79 return by.findElements(this);
80 }
81
Dounia Berrada3f7480d2011-02-28 09:42:44 -080082 /**
83 * Gets the visisble (i.e. not hidden by CSS) innerText of this element,
84 * inlcuding sub-elements.
85 *
86 * @return the innerText of this element.
87 * @throws {@link android.webkit.webdriver.WebElementStaleException} if this
88 * element is stale, i.e. not on the current DOM.
89 */
90 public String getText() {
91 String getText = mDriver.getResourceAsString(R.raw.get_text_android);
Dounia Berrada3f7480d2011-02-28 09:42:44 -080092 return (String) executeAtom(getText, this);
93 }
94
Dounia Berrada77cdbc52011-04-11 11:36:04 -070095 /**
96 * Gets the value of an HTML attribute for this element or the value of the
97 * property with the same name if the attribute is not present. If neither
98 * is set, null is returned.
99 *
100 * @param attribute the HTML attribute.
101 * @return the value of that attribute or the value of the property with the
102 * same name if the attribute is not set, or null if neither are set. For
103 * boolean attribute values this will return the string "true" or "false".
104 */
105 public String getAttribute(String attribute) {
106 String getAttribute = mDriver.getResourceAsString(
107 R.raw.get_attribute_value_android);
108 return (String) executeAtom(getAttribute, this, attribute);
109 }
110
111 /**
112 * @return the tag name of this element.
113 */
114 public String getTagName() {
115 return (String) mDriver.executeScript("return arguments[0].tagName;",
116 this);
117 }
118
119 /**
120 * @return true if this element is enabled, false otherwise.
121 */
122 public boolean isEnabled() {
123 String isEnabled = mDriver.getResourceAsString(
124 R.raw.is_enabled_android);
125 return (Boolean) executeAtom(isEnabled, this);
126 }
127
128 /**
129 * Determines whether this element is selected or not. This applies to input
130 * elements such as checkboxes, options in a select, and radio buttons.
131 *
132 * @return True if this element is selected, false otherwise.
133 */
134 public boolean isSelected() {
135 String isSelected = mDriver.getResourceAsString(
136 R.raw.is_selected_android);
137 return (Boolean) executeAtom(isSelected, this);
138 }
139
140 /**
141 * Selects an element on the page. This works for selecting checkboxes,
142 * options in a select, and radio buttons.
143 */
144 public void setSelected() {
145 String setSelected = mDriver.getResourceAsString(
146 R.raw.set_selected_android);
147 executeAtom(setSelected, this);
148 }
149
150 /**
151 * This toggles the checkboxe state from selected to not selected, or
152 * from not selected to selected.
153 *
154 * @return True if the toggled element is selected, false otherwise.
155 */
156 public boolean toggle() {
157 String toggle = mDriver.getResourceAsString(R.raw.toggle_android);
158 return (Boolean) executeAtom(toggle, this);
159 }
160
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800161 /*package*/ String getId() {
162 return mId;
163 }
164
165 /* package */ WebElement findElementById(final String locator) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700166 return findElement(LOCATOR_ID, locator);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800167 }
168
169 /* package */ WebElement findElementByLinkText(final String linkText) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700170 return findElement(LOCATOR_LINK_TEXT, linkText);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800171 }
172
173 /* package */ WebElement findElementByPartialLinkText(
174 final String linkText) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700175 return findElement(LOCATOR_PARTIAL_LINK_TEXT, linkText);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800176 }
177
178 /* package */ WebElement findElementByName(final String name) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700179 return findElement(LOCATOR_NAME, name);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800180 }
181
182 /* package */ WebElement findElementByClassName(final String className) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700183 return findElement(LOCATOR_CLASS_NAME, className);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800184 }
185
186 /* package */ WebElement findElementByCss(final String css) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700187 return findElement(LOCATOR_CSS, css);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800188 }
189
190 /* package */ WebElement findElementByTagName(final String tagName) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700191 return findElement(LOCATOR_TAG_NAME, tagName);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800192 }
193
194 /* package */ WebElement findElementByXPath(final String xpath) {
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700195 return findElement(LOCATOR_XPATH, xpath);
196 }
197
198 /* package */ List<WebElement> findElementsById(final String locator) {
199 return findElements(LOCATOR_ID, locator);
200 }
201
202 /* package */ List<WebElement> findElementsByLinkText(final String linkText) {
203 return findElements(LOCATOR_LINK_TEXT, linkText);
204 }
205
206 /* package */ List<WebElement> findElementsByPartialLinkText(
207 final String linkText) {
208 return findElements(LOCATOR_PARTIAL_LINK_TEXT, linkText);
209 }
210
211 /* package */ List<WebElement> findElementsByName(final String name) {
212 return findElements(LOCATOR_NAME, name);
213 }
214
215 /* package */ List<WebElement> findElementsByClassName(final String className) {
216 return findElements(LOCATOR_CLASS_NAME, className);
217 }
218
219 /* package */ List<WebElement> findElementsByCss(final String css) {
220 return findElements(LOCATOR_CSS, css);
221 }
222
223 /* package */ List<WebElement> findElementsByTagName(final String tagName) {
224 return findElements(LOCATOR_TAG_NAME, tagName);
225 }
226
227 /* package */ List<WebElement> findElementsByXPath(final String xpath) {
228 return findElements(LOCATOR_XPATH, xpath);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800229 }
230
231 private Object executeAtom(final String atom, final Object... args) {
232 String scriptArgs = mDriver.convertToJsArgs(args);
233 return mDriver.executeRawJavascript("(" +
234 atom + ")(" + scriptArgs + ")");
235 }
236
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700237 private List<WebElement> findElements(String strategy, String locator) {
238 String findElements = mDriver.getResourceAsString(
239 R.raw.find_elements_android);
240 return (List<WebElement>) executeAtom(findElements,
241 strategy, locator, this);
242 }
243
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800244 private WebElement findElement(String strategy, String locator) {
245 String findElement = mDriver.getResourceAsString(
246 R.raw.find_element_android);
Dounia Berrada77cdbc52011-04-11 11:36:04 -0700247 WebElement el = (WebElement) executeAtom(findElement,
248 strategy, locator, this);
Dounia Berrada3f7480d2011-02-28 09:42:44 -0800249 if (el == null) {
250 throw new WebElementNotFoundException("Could not find element "
251 + "with " + strategy + ": " + locator);
252 }
253 return el;
254 }
255}