-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathExpectedConditions.cs
More file actions
643 lines (607 loc) · 25.1 KB
/
ExpectedConditions.cs
File metadata and controls
643 lines (607 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// <copyright file="ExpectedConditions.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using OpenQA.Selenium;
namespace SeleniumExtras.WaitHelpers
{
/// <summary>
/// Supplies a set of common conditions that can be waited for using <see cref="WebDriverWait"/>.
/// </summary>
/// <example>
/// <code>
/// IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3))
/// IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("foo")));
/// </code>
/// </example>
public sealed class ExpectedConditions
{
/// <summary>
/// Prevents a default instance of the <see cref="ExpectedConditions"/> class from being created.
/// </summary>
private ExpectedConditions()
{
}
/// <summary>
/// An expectation for checking the title of a page.
/// </summary>
/// <param name="title">The expected title, which must be an exact match.</param>
/// <returns><see langword="true"/> when the title matches; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TitleIs(string title)
{
return (driver) => { return title == driver.Title; };
}
/// <summary>
/// An expectation for checking that the title of a page contains a case-sensitive substring.
/// </summary>
/// <param name="title">The fragment of title expected.</param>
/// <returns><see langword="true"/> when the title matches; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TitleContains(string title)
{
return (driver) => { return driver.Title.Contains(title); };
}
/// <summary>
/// An expectation for the URL of the current page to be a specific URL.
/// </summary>
/// <param name="url">The URL that the page should be on</param>
/// <returns><see langword="true"/> when the URL is what it should be; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> UrlToBe(string url)
{
return (driver) => { return driver.Url.ToLowerInvariant().Equals(url.ToLowerInvariant()); };
}
/// <summary>
/// An expectation for the URL of the current page to be a specific URL.
/// </summary>
/// <param name="fraction">The fraction of the url that the page should be on</param>
/// <returns><see langword="true"/> when the URL contains the text; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> UrlContains(string fraction)
{
return (driver) => { return driver.Url.ToLowerInvariant().Contains(fraction.ToLowerInvariant()); };
}
/// <summary>
/// An expectation for the URL of the current page to be a specific URL.
/// </summary>
/// <param name="regex">The regular expression that the URL should match</param>
/// <returns><see langword="true"/> if the URL matches the specified regular expression; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> UrlMatches(string regex)
{
return (driver) =>
{
var currentUrl = driver.Url;
var pattern = new Regex(regex, RegexOptions.IgnoreCase);
var match = pattern.Match(currentUrl);
return match.Success;
};
}
/// <summary>
/// An expectation for checking that an element is present on the DOM of a
/// page. This does not necessarily mean that the element is visible.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located.</returns>
public static Func<IWebDriver, IWebElement> ElementExists(By locator)
{
return (driver) => { return driver.FindElement(locator); };
}
/// <summary>
/// An expectation for checking that an element is present on the DOM of a page
/// and visible. Visibility means that the element is not only displayed but
/// also has a height and width that is greater than 0.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located and visible.</returns>
public static Func<IWebDriver, IWebElement> ElementIsVisible(By locator)
{
return (driver) =>
{
try
{
return ElementIfVisible(driver.FindElement(locator));
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking that all elements present on the web page that
/// match the locator are visible. Visibility means that the elements are not
/// only displayed but also have a height and width that is greater than 0.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The list of <see cref="IWebElement"/> once it is located and visible.</returns>
public static Func<IWebDriver, IWebElement> ElementIsVisible(IWebElement element)
{
return (driver) =>
{
try
{
return ElementIfVisible(element);
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking that all elements present on the web page that
/// match the element are visible. Visibility means that the elements are not
/// only displayed but also have a height and width that is greater than 0.
/// </summary>
/// <returns>The list of <see cref="IWebElement"/> once the element is visible.</returns>
public static Func<IWebDriver, ReadOnlyCollection<IWebElement>> VisibilityOfAllElementsLocatedBy(By locator)
{
return (driver) =>
{
try
{
var elements = driver.FindElements(locator);
if (elements.Any(element => !element.Displayed))
{
return null;
}
return elements.Any() ? elements : null;
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking that all elements present on the web page that
/// match the locator are visible. Visibility means that the elements are not
/// only displayed but also have a height and width that is greater than 0.
/// </summary>
/// <param name="elements">list of WebElements</param>
/// <returns>The list of <see cref="IWebElement"/> once it is located and visible.</returns>
public static Func<IWebDriver, ReadOnlyCollection<IWebElement>> VisibilityOfAllElementsLocatedBy(ReadOnlyCollection<IWebElement> elements)
{
return (driver) =>
{
try
{
if (elements.Any(element => !element.Displayed))
{
return null;
}
return elements.Any() ? elements : null;
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking that all elements present on the web page that
/// match the locator.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The list of <see cref="IWebElement"/> once it is located.</returns>
public static Func<IWebDriver, ReadOnlyCollection<IWebElement>> PresenceOfAllElementsLocatedBy(By locator)
{
return (driver) =>
{
try
{
var elements = driver.FindElements(locator);
return elements.Any() ? elements : null;
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking if the given text is present in the specified element.
/// </summary>
/// <param name="element">The WebElement</param>
/// <param name="text">Text to be present in the element</param>
/// <returns><see langword="true"/> once the element contains the given text; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TextToBePresentInElement(IWebElement element, string text)
{
return (driver) =>
{
try
{
var elementText = element.Text;
return elementText.Contains(text);
}
catch (StaleElementReferenceException)
{
return false;
}
};
}
/// <summary>
/// An expectation for checking if the given text is present in the element that matches the given locator.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <param name="text">Text to be present in the element</param>
/// <returns><see langword="true"/> once the element contains the given text; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TextToBePresentInElementLocated(By locator, string text)
{
return (driver) =>
{
try
{
var element = driver.FindElement(locator);
var elementText = element.Text;
return elementText.Contains(text);
}
catch (StaleElementReferenceException)
{
return false;
}
};
}
/// <summary>
/// An expectation for checking if the given text is present in the specified elements value attribute.
/// </summary>
/// <param name="element">The WebElement</param>
/// <param name="text">Text to be present in the element</param>
/// <returns><see langword="true"/> once the element contains the given text; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TextToBePresentInElementValue(IWebElement element, string text)
{
return (driver) =>
{
try
{
var elementValue = element.GetAttribute("value");
if (elementValue != null)
{
return elementValue.Contains(text);
}
else
{
return false;
}
}
catch (StaleElementReferenceException)
{
return false;
}
};
}
/// <summary>
/// An expectation for checking if the given text is present in the specified elements value attribute.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <param name="text">Text to be present in the element</param>
/// <returns><see langword="true"/> once the element contains the given text; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TextToBePresentInElementValue(By locator, string text)
{
return (driver) =>
{
try
{
var element = driver.FindElement(locator);
var elementValue = element.GetAttribute("value");
if (elementValue != null)
{
return elementValue.Contains(text);
}
else
{
return false;
}
}
catch (StaleElementReferenceException)
{
return false;
}
};
}
/// <summary>
/// An expectation for checking whether the given frame is available to switch
/// to. If the frame is available it switches the given driver to the
/// specified frame.
/// </summary>
/// <param name="frameLocator">Used to find the frame (id or name)</param>
/// <returns><see cref="IWebDriver"/></returns>
public static Func<IWebDriver, IWebDriver> FrameToBeAvailableAndSwitchToIt(string frameLocator)
{
return (driver) =>
{
try
{
return driver.SwitchTo().Frame(frameLocator);
}
catch (NoSuchFrameException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking whether the given frame is available to switch
/// to. If the frame is available it switches the given driver to the
/// specified frame.
/// </summary>
/// <param name="locator">Locator for the Frame</param>
/// <returns><see cref="IWebDriver"/></returns>
public static Func<IWebDriver, IWebDriver> FrameToBeAvailableAndSwitchToIt(By locator)
{
return (driver) =>
{
try
{
var frameElement = driver.FindElement(locator);
return driver.SwitchTo().Frame(frameElement);
}
catch (NoSuchFrameException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking that an element is either invisible or not present on the DOM.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns><see langword="true"/> if the element is not displayed; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> InvisibilityOfElementLocated(By locator)
{
return (driver) =>
{
try
{
var element = driver.FindElement(locator);
return !element.Displayed;
}
catch (NoSuchElementException)
{
// Returns true because the element is not present in DOM. The
// try block checks if the element is present but is invisible.
return true;
}
catch (StaleElementReferenceException)
{
// Returns true because stale element reference implies that element
// is no longer visible.
return true;
}
};
}
/// <summary>
/// An expectation for checking that an element with text is either invisible or not present on the DOM.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <param name="text">Text of the element</param>
/// <returns><see langword="true"/> if the element is not displayed; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> InvisibilityOfElementWithText(By locator, string text)
{
return (driver) =>
{
try
{
var element = driver.FindElement(locator);
var elementText = element.Text;
if (string.IsNullOrEmpty(elementText))
{
return true;
}
return !elementText.Equals(text);
}
catch (NoSuchElementException)
{
// Returns true because the element with text is not present in DOM. The
// try block checks if the element is present but is invisible.
return true;
}
catch (StaleElementReferenceException)
{
// Returns true because stale element reference implies that element
// is no longer visible.
return true;
}
};
}
/// <summary>
/// An expectation for checking an element is visible and enabled such that you
/// can click it.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located and clickable (visible and enabled).</returns>
public static Func<IWebDriver, IWebElement> ElementToBeClickable(By locator)
{
return (driver) =>
{
var element = ElementIfVisible(driver.FindElement(locator));
try
{
if (element != null && element.Enabled)
{
return element;
}
else
{
return null;
}
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking an element is visible and enabled such that you
/// can click it.
/// </summary>
/// <param name="element">The element.</param>
/// <returns>The <see cref="IWebElement"/> once it is clickable (visible and enabled).</returns>
public static Func<IWebDriver, IWebElement> ElementToBeClickable(IWebElement element)
{
return (driver) =>
{
try
{
if (element != null && element.Displayed && element.Enabled)
{
return element;
}
else
{
return null;
}
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
/// <summary>
/// Wait until an element is no longer attached to the DOM.
/// </summary>
/// <param name="element">The element.</param>
/// <returns><see langword="false"/> is the element is still attached to the DOM; otherwise, <see langword="true"/>.</returns>
public static Func<IWebDriver, bool> StalenessOf(IWebElement element)
{
return (driver) =>
{
try
{
// Calling any method forces a staleness check
return element == null || !element.Enabled;
}
catch (StaleElementReferenceException)
{
return true;
}
};
}
/// <summary>
/// An expectation for checking if the given element is selected.
/// </summary>
/// <param name="element">The element.</param>
/// <returns><see langword="true"/> given element is selected.; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> ElementToBeSelected(IWebElement element)
{
return ElementSelectionStateToBe(element, true);
}
/// <summary>
/// An expectation for checking if the given element is in correct state.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="selected">selected or not selected</param>
/// <returns><see langword="true"/> given element is in correct state.; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> ElementToBeSelected(IWebElement element, bool selected)
{
return (driver) =>
{
return element.Selected == selected;
};
}
/// <summary>
/// An expectation for checking if the given element is in correct state.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="selected">selected or not selected</param>
/// <returns><see langword="true"/> given element is in correct state.; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> ElementSelectionStateToBe(IWebElement element, bool selected)
{
return (driver) =>
{
return element.Selected == selected;
};
}
/// <summary>
/// An expectation for checking if the given element is selected.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns><see langword="true"/> given element is selected.; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> ElementToBeSelected(By locator)
{
return ElementSelectionStateToBe(locator, true);
}
/// <summary>
/// An expectation for checking if the given element is in correct state.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <param name="selected">selected or not selected</param>
/// <returns><see langword="true"/> given element is in correct state.; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> ElementSelectionStateToBe(By locator, bool selected)
{
return (driver) =>
{
try
{
var element = driver.FindElement(locator);
return element.Selected == selected;
}
catch (StaleElementReferenceException)
{
return false;
}
};
}
/// <summary>
/// An expectation for checking the AlterIsPresent
/// </summary>
/// <returns>Alert </returns>
public static Func<IWebDriver, IAlert> AlertIsPresent()
{
return (driver) =>
{
try
{
return driver.SwitchTo().Alert();
}
catch (NoAlertPresentException)
{
return null;
}
};
}
/// <summary>
/// An expectation for checking the Alert State
/// </summary>
/// <param name="state">A value indicating whether or not an alert should be displayed in order to meet this condition.</param>
/// <returns><see langword="true"/> alert is in correct state present or not present; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> AlertState(bool state)
{
return (driver) =>
{
var alertState = false;
try
{
driver.SwitchTo().Alert();
alertState = true;
return alertState == state;
}
catch (NoAlertPresentException)
{
alertState = false;
return alertState == state;
}
};
}
private static IWebElement ElementIfVisible(IWebElement element)
{
return element.Displayed ? element : null;
}
}
}