UNPKG

7.27 kBTypeScriptView Raw
1import { Locator, WebDriver, WebElement } from "../";
2
3/**
4 * Typings for lib/by.js
5 * Describes a mechanism for locating an element on the page.
6 * @final
7 */
8export class By {
9 /**
10 * @param {string} using the name of the location strategy to use.
11 * @param {string} value the value to search for.
12 */
13 constructor(using: string, value: string);
14
15 using: string;
16 value: string;
17
18 /**
19 * Locates elements that have a specific class name.
20 *
21 * @param {string} name The class name to search for.
22 * @return {!By} The new locator.
23 * @see http://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes
24 * @see http://www.w3.org/TR/CSS2/selector.html#class-html
25 */
26 static className(name: string): By;
27
28 /**
29 * Locates elements using a CSS selector.
30 *
31 * @param {string} selector The CSS selector to use.
32 * @return {!By} The new locator.
33 * @see http://www.w3.org/TR/CSS2/selector.html
34 */
35 static css(selector: string): By;
36
37 /**
38 * Locates eleemnts by the ID attribute. This locator uses the CSS selector
39 * `*[id='$ID']`, _not_ `document.getElementById`.
40 *
41 * @param {string} id The ID to search for.
42 * @return {!By} The new locator.
43 */
44 static id(id: string): By;
45
46 /**
47 * Locates link elements whose
48 * {@linkplain WebElement#getText visible text} matches the given
49 * string.
50 *
51 * @param {string} text The link text to search for.
52 * @return {!By} The new locator.
53 */
54 static linkText(text: string): By;
55
56 /**
57 * Locates an elements by evaluating a
58 * {@linkplain WebDriver#executeScript JavaScript expression}.
59 * The result of this expression must be an element or list of elements.
60 *
61 * @param {!(string|Function)} script The script to execute.
62 * @param {...*} var_args The arguments to pass to the script.
63 * @return {function(!./WebDriver): !./Promise}
64 * A new JavaScript-based locator function.
65 */
66 static js(script: string | Function, ...var_args: any[]): (webdriver: WebDriver) => Promise<any>;
67
68 /**
69 * Locates elements whose `name` attribute has the given value.
70 *
71 * @param {string} name The name attribute to search for.
72 * @return {!By} The new locator.
73 */
74 static name(name: string): By;
75
76 /**
77 * Locates link elements whose
78 * {@linkplain WebElement#getText visible text} contains the given
79 * substring.
80 *
81 * @param {string} text The substring to check for in a link's visible text.
82 * @return {!By} The new locator.
83 */
84 static partialLinkText(text: string): By;
85
86 /**
87 * Locates elements with a given tag name.
88 *
89 * @param {string} name The tag name to search for.
90 * @return {!By} The new locator.
91 * @deprecated Use {@link By.css() By.css(tagName)} instead.
92 */
93 static tagName(name: string): By;
94
95 /**
96 * Locates elements matching a XPath selector. Care should be taken when
97 * using an XPath selector with a {@link WebElement} as WebDriver
98 * will respect the context in the specified in the selector. For example,
99 * given the selector `//div`, WebDriver will search from the document root
100 * regardless of whether the locator was used with a WebElement.
101 *
102 * @param {string} xpath The XPath selector to use.
103 * @return {!By} The new locator.
104 * @see http://www.w3.org/TR/xpath/
105 */
106 static xpath(xpath: string): By;
107
108 toObject(): Object;
109
110 /** @override */
111 toString(): string;
112}
113
114/**
115 * Describes a mechanism for locating an element relative to others
116 * on the page.
117 * @final
118 */
119export class RelativeBy {
120 /**
121 * @param {By} findDetails
122 * @param {Array<Object>} filters
123 */
124 constructor(findDetails: By, filters: Object[]);
125
126 /**
127 * Look for elements above the root element passed in
128 * @param {string|WebElement} locatorOrElement
129 * @return {!RelativeBy} Return this object
130 */
131 above(locatorOrElement: Locator | WebElement): RelativeBy;
132
133 /**
134 * Look for elements below the root element passed in
135 * @param {Locator|WebElement} locatorOrElement
136 * @return {!RelativeBy} Return this object
137 */
138 below(locatorOrElement: Locator | WebElement): RelativeBy;
139
140 /**
141 * Look for elements left the root element passed in
142 * @param {Locator|WebElement} locatorOrElement
143 * @return {!RelativeBy} Return this object
144 */
145 toLeftOf(locatorOrElement: Locator | WebElement): RelativeBy;
146
147 /**
148 * Look for elements right the root element passed in
149 * @param {Locator|WebElement} locatorOrElement
150 * @return {!RelativeBy} Return this object
151 */
152 toRightOf(locatorOrElement: Locator | WebElement): RelativeBy;
153
154 /**
155 * Look for elements near the root element passed in
156 * @param {Locator|WebElement} locatorOrElement
157 * @return {!RelativeBy} Return this object
158 */
159 near(locatorOrElement: Locator | WebElement): RelativeBy;
160
161 /**
162 * Returns a marshalled version of the {@link RelativeBy}
163 * @return {!Object} Object representation of a {@link WebElement}
164 * that will be used in {@link #findElements}.
165 */
166 marshall(): Object;
167
168 /** @override */
169 toString(): string;
170}
171
172/**
173 * Short-hand expressions for the primary element locator strategies.
174 * For example the following two statements are equivalent:
175 *
176 * var e1 = driver.findElement(By.id('foo'));
177 * var e2 = driver.findElement({id: 'foo'});
178 *
179 * Care should be taken when using JavaScript minifiers (such as the
180 * Closure compiler), as locator hashes will always be parsed using
181 * the un-obfuscated properties listed.
182 */
183export type ByHash =
184 | { className: string }
185 | { css: string }
186 | { id: string }
187 | { js: string }
188 | { linkText: string }
189 | { name: string }
190 | { partialLinkText: string }
191 | { tagName: string }
192 | { xpath: string };
193
194/**
195 * Checks if a value is a valid locator.
196 * @param {!(By|Function|ByHash)} locator The value to check.
197 * @return {!(By|Function)} The valid locator.
198 * @throws {TypeError} If the given value does not define a valid locator
199 * strategy.
200 */
201export function checkedLocator(locator: Locator): By | Function;
202
203/**
204 * Start searching for relative objects using search criteria with By.
205 * @param {Locator} by A By map that shows how to find the initial element
206 * @returns {RelativeBy} RelativeBy instance
207 */
208export function locateWith(by: Locator): RelativeBy;
209
210/**
211 * Start Searching for relative objects using the value returned from
212 * `By.tagName()`.
213 *
214 * Note: this method will likely be removed in the future please use
215 * `locateWith`.
216 * @param {By} tagName The value returned from calling By.tagName()
217 * @returns {RelativeBy} RelativeBy instance
218 */
219export function withTagName(tagName: By): RelativeBy;
220
221/**
222 * Escapes a CSS string.
223 * @param {string} css the string to escape.
224 * @return {string} the escaped string.
225 * @throws {TypeError} if the input value is not a string.
226 * @throws {InvalidCharacterError} if the string contains an invalid character.
227 * @see https://drafts.csswg.org/cssom/#serialize-an-identifier
228 */
229export function escapeCss(css: string): string;