-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserContext.cs
539 lines (476 loc) · 18.9 KB
/
BrowserContext.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using FluentAssert;
using FluentBrowserAutomation.Controls;
using FluentBrowserAutomation.Declarative;
using FluentBrowserAutomation.Framework;
using JetBrains.Annotations;
using OpenQA.Selenium;
namespace FluentBrowserAutomation
{
public interface IBrowserContext
{
string BaseUrl { get; }
IWebDriver Browser { get; }
ButtonWrapper ButtonWithId([NotNull] string id);
ButtonWrapper ButtonWithText([NotNull] string text);
IEnumerable<ButtonWrapper> Buttons();
CheckBoxWrapper CheckBoxWithId([NotNull] string id);
CheckBoxWrapper CheckBoxWithLabel([NotNull] string id);
CheckBoxWrapper CheckBoxWithIdAndValue([NotNull] string id, [NotNull] string value);
CheckBoxWrapper CheckBoxWithNameAndValue([NotNull] string name, [NotNull] string value);
IEnumerable<CheckBoxWrapper> CheckBoxes();
IEnumerable<CheckBoxWrapper> CheckBoxesWithName([NotNull] string name);
void CloseBrowser();
ContainerWrapper ContainerWithId([NotNull] string id);
DialogHandlerWrapper Dialog([NotNull] Action action);
DivWrapper DivWithId([NotNull] string id);
IEnumerable<IWebElement> GetWebElementsWithClassName([NotNull] string className);
IEnumerable<DivWrapper> Divs();
DropDownListWrapper DropDownListWithId([NotNull] string idOfList);
DropDownListWrapper DropDownListWithLabel([NotNull] string label);
IEnumerable<DropDownListWrapper> DropDownLists();
void GoToUrl([NotNull] string url);
TextBoxWrapper HiddenWithId([NotNull] string id);
IEnumerable<TextBoxWrapper> Hiddens();
BrowserContext IdOfFieldWithFocusShouldBe([NotNull] string expectedId);
IEnumerable<ButtonWrapper> ImageButtons();
ImageWrapper ImageWithId([NotNull] string id);
IAmInputThatCanBeChanged InputWithId([NotNull] string id);
IAmInputThatCanBeChanged InputWithLabel([NotNull] string label);
IAmInputThatCanBeChanged InputWithValue([NotNull] string value);
LabelWrapper LabelWithId([NotNull] string id);
IEnumerable<LabelWrapper> Labels();
LinkWrapper LinkWithId([NotNull] string id);
LinkWrapper LinkWithText([NotNull] string text);
IEnumerable<LinkWrapper> Links();
INavigationControl NavigationControlWithId([NotNull] string id);
INavigationControl NavigationControlWithText([NotNull] string text);
PageWrapper Page();
RadioOptionWrapper RadioOptionWithId([NotNull] string idOfOption);
RadioOptionWrapper RadioOptionWithLabel([NotNull] string label);
IEnumerable<RadioOptionWrapper> RadioOptions();
IAmInputThatCanBeChanged Set([NotNull] string labelText);
SpanWrapper SpanWithId([NotNull] string id);
SpanWrapper SpanWithText([NotNull] string spanText);
IEnumerable<SpanWrapper> Spans();
TableWrapper TableWithId([NotNull] string id);
ListWrapper ListWithId([NotNull] string id);
IEnumerable<TableWrapper> Tables();
TextBoxWrapper TextBoxWithId([NotNull] string id);
TextBoxWrapper TextBoxWithLabel([NotNull] string label);
IEnumerable<TextBoxWrapper> TextBoxes();
string GetHiddenValueWithId([NotNull] string id);
IBrowserContext WaitUntil([NotNull] Func<IBrowserContext, bool> func, int secondsToWait = 10);
}
public class BrowserContext : IBrowserContext
{
private readonly IBrowserManager _browserManager;
private IWebDriver _browser;
public BrowserContext(IBrowserManager browserManager)
{
_browserManager = browserManager;
}
public string BaseUrl
{
get { return _browserManager.BaseUrl; }
}
public IWebDriver Browser
{
get { return _browser ?? (_browser = _browserManager.GetBrowser()); }
}
public ButtonWrapper ButtonWithId(string id)
{
const string howFound = "button with id '{0}'";
var button = TryGetElementByIdAndInputType(id, "submit") ?? TryGetElementByIdAndInputType(id, "button");
return new ButtonWrapper(button, String.Format(howFound, id), this);
}
public ButtonWrapper ButtonWithText(string text)
{
const string howFound = "button with visible text '{0}'";
var button = Buttons().FirstOrDefault(x => x.Element.GetAttribute("value") == text);
return new ButtonWrapper(button == null ? null : button.Element, String.Format(howFound, text), this);
}
public IEnumerable<ButtonWrapper> Buttons()
{
const string howFound = "type 'button'";
var submits = GetInputsByInputType("submit");
var buttons = GetInputsByInputType("button");
return submits.Concat(buttons).Select(x => new ButtonWrapper(x, howFound, this));
}
public CheckBoxWrapper CheckBoxWithId(string id)
{
const string howFound = "checkbox with id '{0}'";
var checkBox = TryGetElementByIdAndInputType(id, "checkbox");
return new CheckBoxWrapper(checkBox, String.Format(howFound, id), this);
}
public CheckBoxWrapper CheckBoxWithNameAndValue(string name, string value)
{
const string howFound = "checkbox with name '{0}'";
var checkBox = TryGetElementByNameAndInputTypeAndValue(name, "checkbox", value);
return new CheckBoxWrapper(checkBox, String.Format(howFound, name), this);
}
public CheckBoxWrapper CheckBoxWithIdAndValue(string id, string value)
{
const string howFound = "checkbox with id '{0}'";
var checkBox = TryGetElementByIdAndInputTypeAndValue(id, "checkbox", value);
return new CheckBoxWrapper(checkBox, String.Format(howFound, id), this);
}
public CheckBoxWrapper CheckBoxWithLabel(string label)
{
var input = InputWithLabel(label);
var checkBoxWrapper = input as CheckBoxWrapper;
if (checkBoxWrapper != null)
{
return checkBoxWrapper;
}
return new CheckBoxWrapper(null, "checkbox with label '" + label + "'", input.BrowserContext);
}
public IEnumerable<CheckBoxWrapper> CheckBoxes()
{
const string howFound = "type 'checkbox'";
var checkBoxes = GetInputsByInputType("checkbox");
return checkBoxes.Select(x => new CheckBoxWrapper(x, howFound, this));
}
public IEnumerable<CheckBoxWrapper> CheckBoxesWithName(string name)
{
const string howFound = "type 'checkbox'";
var checkBoxes = GetInputs().Where(x => x.GetAttribute("type") == "checkbox" && x.GetAttribute("name") == name);
return checkBoxes.Select(x => new CheckBoxWrapper(x, howFound, this));
}
public void CloseBrowser()
{
_browserManager.Close();
}
public DialogHandlerWrapper Dialog(Action action)
{
return new DialogHandlerWrapper(this, action);
}
public DivWrapper DivWithId(string id)
{
const string howFound = "div with id '{0}'";
var div = TryGetElementByIdAndTagType(id, "div");
return new DivWrapper(div, String.Format(howFound, id), this);
}
public IEnumerable<IWebElement> GetWebElementsWithClassName(string className)
{
return TryGetElementsByClassName(className);
}
public IEnumerable<DivWrapper> Divs()
{
const string howFound = "type 'div'";
var divs = GetElementsByTagType("div");
return divs.Select(x => new DivWrapper(x, howFound, this));
}
public DropDownListWrapper DropDownListWithId(string idOfList)
{
const string howFound = "drop down list with id '{0}'";
var dropDownList = TryGetElementByIdAndTagType(idOfList, "select");
return new DropDownListWrapper(dropDownList, String.Format(howFound, idOfList), this);
}
public DropDownListWrapper DropDownListWithLabel(string label)
{
var input = InputWithLabel(label);
var downListWithLabel = input as DropDownListWrapper;
if (downListWithLabel != null)
{
return downListWithLabel;
}
return new DropDownListWrapper(null, "drop down list with label '" + label + "'", input.BrowserContext);
}
public IEnumerable<DropDownListWrapper> DropDownLists()
{
const string howFound = "type 'select'";
var dropDowns = GetElementsByTagType("select");
return dropDowns.Select(x => new DropDownListWrapper(x, howFound, this));
}
public void GoToUrl(string url)
{
Browser.Navigate().GoToUrl(url);
}
public TextBoxWrapper HiddenWithId(string id)
{
const string howFound = "hidden input with id '{0}'";
var hidden = TryGetElementByIdAndInputType(id, "hidden");
return new TextBoxWrapper(hidden, String.Format(howFound, id), this);
}
public string GetHiddenValueWithId(string id)
{
var hidden = TryGetElementByIdAndInputType(id, "hidden");
return hidden.GetAttribute("value");
}
public IEnumerable<TextBoxWrapper> Hiddens()
{
const string howFound = "type 'hidden'";
var hiddens = GetElementsByTagType("input").Where(x => x.GetAttribute("type") == "hidden");
return hiddens.Select(x => new TextBoxWrapper(x, howFound, this));
}
public BrowserContext IdOfFieldWithFocusShouldBe(string expectedId)
{
string id = Browser.SwitchTo().ActiveElement().GetAttribute("id");
id.ShouldBeEqualTo(expectedId);
return this;
}
public IEnumerable<ButtonWrapper> ImageButtons()
{
const string howFound = "type 'img'";
var images = GetElementsByTagType("img");
return images.Select(x => new ButtonWrapper(x, howFound, this));
}
public ImageWrapper ImageWithId(string id)
{
const string howFound = "image with id '{0}'";
var button = TryGetElementByIdAndTagType(id, "img");
return new ImageWrapper(button, String.Format(howFound, id), this);
}
public IAmInputThatCanBeChanged InputWithId(string id)
{
_browserManager.Trace("Getting input with id '" + id + "'");
return SetterFor.InputWithId(this, id);
}
public IAmInputThatCanBeChanged InputWithLabel(string label)
{
_browserManager.Trace("Getting input with label '" + label + "'");
return SetterFor.InputWithLabel(this, label);
}
public IAmInputThatCanBeChanged InputWithValue(string value)
{
_browserManager.Trace("Getting input with value '" + value + "'");
return SetterFor.InputWithValue(this, value);
}
public LabelWrapper LabelWithId(string id)
{
const string howFound = "label with id '{0}'";
var label = TryGetElementByIdAndTagType(id, "label");
return new LabelWrapper(label, String.Format(howFound, id), this);
}
public IEnumerable<LabelWrapper> Labels()
{
const string howFound = "type 'label'";
var labels = GetElementsByTagType("label");
return labels.Select(x => new LabelWrapper(x, howFound, this));
}
public LinkWrapper LinkWithId(string id)
{
const string howFound = "link with id '{0}'";
var link = TryGetElementByIdAndTagType(id, "a");
return new LinkWrapper(link, String.Format(howFound, id), this);
}
public LinkWrapper LinkWithText(string text)
{
string htmlEscapedText = HttpUtility.HtmlEncode(text);
const string howFound = "link with visible text '{0}'";
var link = Browser.FindElements(By.LinkText(text)).FirstOrDefault() ?? GetElementsByTagType("a").FirstOrDefault(x =>
{
string
attribute
=
x
.
GetAttribute
("innerHTML");
return
attribute ==
htmlEscapedText ||
attribute ==
text ||
attribute
.
Trim
() ==
text;
});
return new LinkWrapper(link, String.Format(howFound, text), this);
}
public IEnumerable<LinkWrapper> Links()
{
const string howFound = "type 'a'";
var links = GetElementsByTagType("a");
return links.Select(x => new LinkWrapper(x, howFound, this));
}
public INavigationControl NavigationControlWithId(string id)
{
var button = ButtonWithId(id);
if (button.Exists().IsTrue)
{
return button;
}
return LinkWithId(id);
}
public INavigationControl NavigationControlWithText(string text)
{
var button = ButtonWithText(text);
if (button.Exists().IsTrue)
{
return button;
}
return LinkWithText(text);
}
public PageWrapper Page()
{
return new PageWrapper(this);
}
public RadioOptionWrapper RadioOptionWithId(string idOfOption)
{
const string howFound = "radio option with id '{0}'";
var radioButton = TryGetElementByIdAndInputType(idOfOption, "radio");
return new RadioOptionWrapper(radioButton, String.Format(howFound, idOfOption), this);
}
public RadioOptionWrapper RadioOptionWithLabel(string label)
{
var input = InputWithLabel(label);
var radioOptionWrapper = input as RadioOptionWrapper;
if (radioOptionWrapper != null)
{
return radioOptionWrapper;
}
return new RadioOptionWrapper(null, "radio option with label '" + label + "'", input.BrowserContext);
}
public IEnumerable<RadioOptionWrapper> RadioOptions()
{
const string howFound = "type 'radio'";
var radios = GetInputsByInputType("radio");
return radios.Select(x => new RadioOptionWrapper(x, howFound, this));
}
public IAmInputThatCanBeChanged Set(string labelText)
{
return InputWithLabel(labelText);
}
public SpanWrapper SpanWithId(string id)
{
const string howFound = "span with id '{0}'";
var span = TryGetElementByIdAndTagType(id, "span");
return new SpanWrapper(span, String.Format(howFound, id), this);
}
public SpanWrapper SpanWithText(string spanText)
{
return Spans().First(x => x.Text() == spanText);
}
public IEnumerable<SpanWrapper> Spans()
{
const string howFound = "type 'span'";
var spans = GetElementsByTagType("span");
return spans.Select(x => new SpanWrapper(x, howFound, this));
}
public TableWrapper TableWithId(string id)
{
const string howFound = "table with id '{0}'";
var table = TryGetElementByIdAndTagType(id, "table");
return new TableWrapper(table, String.Format(howFound, id), this);
}
public ListWrapper ListWithId(string id)
{
const string howFound = "list with id '{0}'";
var list = TryGetElementByIdAndTagType(id, "ul");
return new ListWrapper(list, String.Format(howFound, id), this);
}
public IEnumerable<TableWrapper> Tables()
{
const string howFound = "type 'table'";
var tables = GetElementsByTagType("table");
return tables.Select(x => new TableWrapper(x, howFound, this));
}
public TextBoxWrapper TextBoxWithId(string id)
{
const string howFound = "text box with id '{0}'";
var textField = TryGetElementByIdAndInputType(id, "text") ?? TryGetElementByIdAndTagType(id, "textarea");
return new TextBoxWrapper(textField, String.Format(howFound, id), this);
}
public IEnumerable<TextBoxWrapper> TextBoxes()
{
var textBoxes = GetInputsByInputType("text").Select(x => new TextBoxWrapper(x, "type 'text'", this));
var textAreas = GetElementsByTagType("textarea").Select(x => new TextBoxWrapper(x, "type 'textarea'", this));
return textBoxes.Concat(textAreas);
}
public IBrowserContext WaitUntil(Func<IBrowserContext, bool> func, int secondsToWait = 10)
{
for (int i = 0; i < secondsToWait; i++)
{
try
{
if (func(this))
{
return this;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message + " -- waiting 1 sec");
}
Thread.Sleep(TimeSpan.FromSeconds(1));
}
throw new AssertionException("state being waited upon never happened.");
}
public ContainerWrapper ContainerWithId(string id)
{
const string howFound = "container with id '{0}'";
var container = TryGetElementByIdAndTagType(id, "div");
if (container == null)
{
container = TryGetElementByIdAndTagType(id, "span");
}
if (container == null)
{
container = TryGetElementByIdAndTagType(id, "ul");
}
if (container == null)
{
container = TryGetElementByIdAndTagType(id, "ol");
}
if (container == null)
{
container = TryGetElementByIdAndTagType(id, "table");
}
return new ContainerWrapper(container, String.Format(howFound, id), this);
}
public TextBoxWrapper TextBoxWithLabel(string label)
{
var input = InputWithLabel(label);
var textBoxWrapper = input as TextBoxWrapper;
if (textBoxWrapper != null)
{
return textBoxWrapper;
}
return new TextBoxWrapper(null, "Textbox with label '" + label + "'", input.BrowserContext);
}
private IEnumerable<IWebElement> GetElementsByTagType(string tag)
{
return Browser.FindElements(By.TagName(tag));
}
private IEnumerable<IWebElement> GetInputs()
{
return Browser.FindElements(By.TagName("input"));
}
private IEnumerable<IWebElement> GetInputsByInputType(string type)
{
return GetInputs().Where(x => x.GetAttribute("type") == type);
}
private IWebElement TryGetElementByIdAndInputType(string id, string type)
{
return Browser.FindElements(By.Id(id)).FirstOrDefault(x => x.GetAttribute("type") == type);
}
private IWebElement TryGetElementByIdAndInputTypeAndValue(string id, string type, string value)
{
return
Browser.FindElements(By.Id(id)).FirstOrDefault(
x => x.GetAttribute("type") == type && x.GetAttribute("value") == value);
}
private IWebElement TryGetElementByNameAndInputTypeAndValue(string name, string type, string value)
{
return
Browser.FindElements(By.Name(name)).FirstOrDefault(
x => x.GetAttribute("type") == type && x.GetAttribute("value") == value);
}
private IWebElement TryGetElementByIdAndTagType(string id, string tag)
{
return Browser.FindElements(By.Id(id)).FirstOrDefault(x => x.TagName == tag);
}
private IEnumerable<IWebElement> TryGetElementsByClassName(string className)
{
return Browser.FindElements(By.ClassName(className));
}
}
}