My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Threading;
using MbUnit.Framework;
using Selenium;

namespace Accipio.TestFramework
{
/// <summary>
/// Tester class for testing Web application using Selenium. The class provides a fluent interface
/// for testing.
/// </summary>
public class SeleniumTesterBase : TestFramework.AccipioTestRunnerBase<SeleniumTesterBase>
{
/// <summary>
/// Initializes a new instance of the <see cref="SeleniumTesterBase"/> class.
/// </summary>
/// <param name="testCaseName">Name of the test case.</param>
public SeleniumTesterBase(string testCaseName) : base (testCaseName)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

browserType = (BrowserType) Enum.Parse(
typeof(BrowserType),
ConfigurationManager.AppSettings["BrowserType"],
true);
testMachine = ConfigurationManager.AppSettings["TestMachine"];
seleniumPort = int.Parse(
ConfigurationManager.AppSettings["SeleniumPort"],
CultureInfo.InvariantCulture);
seleniumSpeed = ConfigurationManager.AppSettings["SeleniumSpeed"];
browserUrl = ConfigurationManager.AppSettings["BrowserUrl"];

string browserExe;

switch (browserType)
{
case BrowserType.InternetExplorer:
browserExe = "*iexplore";
break;
case BrowserType.Firefox:
browserExe = "*firefox";
break;

default:
throw new NotSupportedException();
}

selenium = new DefaultSelenium(testMachine, seleniumPort, browserExe, browserUrl);
selenium.Start();

Console.WriteLine("Started Selenium session (browser type={0})", browserType);

// sets the speed of execution of GUI commands
if (false == String.IsNullOrEmpty(seleniumSpeed))
selenium.SetSpeed(seleniumSpeed);
}

/// <summary>
/// Gets the underlying Selenium object.
/// </summary>
/// <value>The underlying Selenium object.</value>
[CLSCompliant(false)]
public ISelenium Selenium
{
get { return selenium; }
}

/// <summary>
/// Check the Ansfer confirmation
/// </summary>
/// <param name="confirmationText">Confirmation text</param>
/// <param name="confirm">is it confirmed ?</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AnswerToConfirmation(string confirmationText, bool confirm)
{
Assert.IsTrue(selenium.IsConfirmationPresent());
Assert.AreEqual(confirmationText, selenium.GetConfirmation());

if (confirm)
selenium.ChooseOkOnNextConfirmation();
else
selenium.ChooseCancelOnNextConfirmation();

return this;
}

/// <summary>
/// Asserts that the button with controlId is available on the page.
/// </summary>
/// <param name="controlId">Id of the button.</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AssertButtonAvailable(string controlId)
{
Assert.IsFalse(
selenium.IsElementPresent(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//a[contains(@href,'{0}')]",
controlId)));

return this;
}

/// <summary>
/// Asserts that the DropDown contains given values.
/// </summary>
/// <param name="controlId">Id of dropDown control to assert.</param>
/// <param name="parameters">Selection values that dropDown has to contain.</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AssertDropDownContainsValues(string controlId, params string[] parameters)
{
string locatorForDropDown = string.Format(
CultureInfo.CurrentCulture,
"xpath=//select[contains(@id,'{0}')]",
controlId);
string[] itemValues = selenium.GetSelectOptions(locatorForDropDown);
List<string> list = new List<string>(itemValues);
Assert.AreEqual(parameters.Length, itemValues.Length);
foreach (string parameter in parameters)
Assert.IsTrue(list.Contains(parameter));

return this;
}

/// <summary>
/// Asserts the page displayed a specified list of validation error codes. If at least one error is missing,
/// the method fails.
/// </summary>
/// <param name="errorCodes">The validation error codes which have to be present on the page.</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AssertHasErrors(params string[] errorCodes)
{
List<string> errorCodesList = new List<string>(errorCodes);

foreach (string errorCode in errorCodesList)
{
Assert.IsTrue(selenium.IsTextPresent(errorCode), "Error '{0}' is missing.", errorCode);
}

return this;
}

/// <summary>
/// Asserts the specified HTML element has the expected text.
/// </summary>
/// <param name="locator">The locator for the HTML element.</param>
/// <param name="expectedText">The expected text.</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AssertHtmlText(string locator, string expectedText)
{
string actualText = Selenium.GetText(locator);
Assert.AreEqual(expectedText, actualText, "Unexpected HTML text");

return this;
}

/// <summary>
/// Asserts that the application is currently on the specified page.
/// </summary>
/// <param name="pageName">The name of the page.</param>
/// <param name="pageId">The ID of the dialog.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertIsOnPage(string pageName, string pageId)
{
Uri absoluteUrl = new Uri(selenium.GetLocation());
string localPath = absoluteUrl.LocalPath;
string assertMessage = String.Format(
CultureInfo.InvariantCulture,
"Page '{0}' was expected, actually it is '{1}'.",
pageName,
Path.GetFileName(localPath));

Assert.AreEqual(
pageName,
Path.GetFileName(localPath),
assertMessage);

if (pageId != null)
{
AssertLabelText("DialogIdLabel", pageId);
}

return this;
}

/// <summary>
/// Asserts that the Label with specific Id has a specific Text set
/// </summary>
/// <param name="controlId">ControlId (or part of it) of the label we are chcking</param>
/// <param name="labelText">Text to which we are checking against</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertLabelText(string controlId, string labelText)
{
string text = selenium.GetText("xpath=//span[contains(@id,'" + controlId + "')]");
Assert.AreEqual(
labelText,
text,
"Label Text '{0}' was expected, actually it is '{1}'",
labelText,
text);

return this;
}

/// <summary>
/// Asserts that the TextBox with specific Id has a specific Value set
/// </summary>
/// <param name="controlId">ControlId (or part of it) of the textbox we are chcking</param>
/// <param name="textBoxValue">Value to which we are checking against</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertTextBoxValue(string controlId, string textBoxValue)
{
string text = selenium.GetValue("xpath=//input[contains(@id,'" + controlId + "')]");
Assert.AreEqual(
textBoxValue,
text,
"TextBox Text '{0}' was expected, actually it is '{1}",
textBoxValue,
text);

return this;
}

/// <summary>
/// Asserts that the value is part of selection.
/// </summary>
/// <param name="value">value to select, which is checked for its appearance.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertIsLabelTextAvailable(string value)
{
Assert.IsTrue(
selenium.IsElementPresent(
"xpath=//label[text()='" + value + "']"),
"Value '{0}' is not available",
value);
return this;
}

/// <summary>
/// Asserts that the value is not part of selection.
/// </summary>
/// <param name="controlValue">value to select, which is checked for its appearance.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertIsOptionUnavailable(string controlValue)
{
Assert.IsFalse(selenium.IsElementPresent("xpath=//input[contains(@value,'" + controlValue + "')]"));
return this;
}

/// <summary>
/// Asserts that the Label doesn't exist
/// </summary>
/// <param name="controlId">ControlId (or part of it) of the label we are checking if it exists</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertNoLabel(string controlId)
{
string text = null;
try
{
text = selenium.GetText("xpath=//span[contains(@id,'" + controlId + "')]");
}
catch (SeleniumException)
{
}

Assert.AreEqual(
null,
text,
"Label with Id '{0}' was not expected but was found.",
controlId);
return this;
}

/// <summary>
/// Asserts that the RadioButton contains given values.
/// </summary>
/// <param name="groupName">Name of group of radio buttons.</param>
/// <param name="parameters">Raddio button values that have to be in given group.</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AssertRadioButtonContainsValues(string groupName, params string[] parameters)
{
decimal count = selenium.GetXpathCount(
String.Format(
CultureInfo.InvariantCulture,
"//input[@type='radio' and @name='{0}']",
groupName));

Assert.AreEqual(
parameters.Length,
count,
"Radio button '{0}' does not contain the expected number of values.",
groupName);

foreach (string parameter in parameters)
{
if (false == String.IsNullOrEmpty(parameter))
Assert.IsTrue(
selenium.IsElementPresent(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[@type='radio' and @name='{0}' and @value='{1}']",
groupName,
parameter)),
"Radio button '{0}' does not contain the expected value '{1}'.",
groupName,
parameter);
}

return this;
}

/// <summary>
/// Asserts that the RadioButton contains disabled value.
/// </summary>
/// <param name="groupName">Name of group of radio buttons.</param>
/// <param name="value">Value of the selected radio button.</param>
/// <returns>The same <see cref="SeleniumTesterBase"/> object.</returns>
public SeleniumTesterBase AssertIsRadioButtonValueDisabled(string groupName, string value)
{
try
{
Assert.IsFalse(
selenium.IsEditable(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[@type='radio' and @name='{0}' and @value='{1}']",
groupName,
value)),
"Radio button '{0}' value '{1}' is not disabled.",
groupName,
value);
}
catch (SeleniumException)
{
Assert.Fail("Radio button '{0}' dose not contain value '{1}'.", groupName, value);
}

return this;
}

/// <summary>
/// Asserts that specified value in radio is selected.
/// </summary>
/// <param name="controlValue">the Value property of the radio button</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertIsRadioButtonValueSelected(string controlValue)
{
Assert.IsTrue(
selenium.IsChecked(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[@type='radio' and @value='{0}']",
controlValue)));
return this;
}

/// <summary>
/// Assert checkbox state
/// </summary>
/// <param name="selected">true if checkbox is seleced, otherwise false</param>
/// <param name="locator">checkbox control locator (e.g."xpath=//input[contains(@id,'ctl00_MainContentPlaceholder_ContentPanel1_CheckBox1')]")</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertCheckBoxState(bool selected, string locator)
{
Assert.AreEqual(selected, selenium.IsChecked(locator));
return this;
}

/// <summary>
/// Asserts that the drop down control has the specified value selected.
/// </summary>
/// <param name="dropDownControlId">The ID of the drop down control.</param>
/// <param name="expectedValue">The expected value.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase AssertSelectedDropDownValue(string dropDownControlId, object expectedValue)
{
string locatorForDropDown = string.Format(
CultureInfo.CurrentCulture,
"xpath=//select[contains(@id,'{0}')]",
dropDownControlId);
string selectedValue = selenium.GetSelectedValue(locatorForDropDown);

Assert.AreEqual(expectedValue.ToString(), selectedValue);

return this;
}

/// <summary>
/// Presses the "Back" button on the browser.
/// </summary>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase BrowserBackButton()
{
selenium.GoBack();
return Pause();
}

/// <summary>
/// Clears the value of the specified input control.
/// </summary>
/// <param name="inputControlId">The ID of the input control.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase ClearValue(string inputControlId)
{
return EnterValue(inputControlId, String.Empty);
}

/// <summary>
/// Clicks the on the specified link.
/// </summary>
/// <param name="controlId">The ID of the control which is contained in the link.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase ClickOnLink(string controlId)
{
selenium.Click(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//a[contains(@href,'{0}')]",
controlId));
selenium.WaitForPageToLoad("10000");
//Pause();
return this;
}

/// <summary>
/// Clicks the on the specified button.
/// </summary>
/// <param name="controlId">The ID of the control which is contained in the input field.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase ClickOnButton(string controlId)
{
selenium.Click(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[contains(@id,'{0}')]",
controlId));
selenium.WaitForPageToLoad("10000");
//Pause();
return this;
}

/// <summary>
/// Clicks the on the specified link button.
/// </summary>
/// <param name="controlId">The ID of the control which is contained in the input field.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase ClickOnLinkButton(string controlId)
{
selenium.Click(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//a[contains(@id,'{0}')]",
controlId));
selenium.WaitForPageToLoad("10000");
//Pause();
return this;
}

/// <summary>
/// Clicks the on the specified link and then expects a confirmation message box to appear.
/// If confirmed then it goes to load a new page.
/// </summary>
/// <param name="controlId">The ID of the control which is contained in the link.</param>
/// <param name="confirmationText">The expected confirmation text in the message box.</param>
/// <param name="confirm">if set to <c>true</c>, the action will be confirmed. Otherwise it will be cancelled.</param>
/// <param name="linkButton">if set to true the button is a LinkButton</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase ClickOnWithConfirmation(
string controlId,
string confirmationText,
bool confirm,
bool linkButton)
{
string controlName = "input";
if (linkButton)
{
controlName = "a";
}
// first set up the non-default Selenium behaviour
if (false == confirm)
selenium.ChooseCancelOnNextConfirmation();

selenium.Click(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//{0}[contains(@id,'{1}')]",
controlName,
controlId));

Assert.IsTrue(selenium.IsConfirmationPresent());
Assert.AreEqual(confirmationText, selenium.GetConfirmation());

// only if confirmed will a new page be actually loaded
if (confirm)
selenium.WaitForPageToLoad("10000");

return this;
}

/// <summary>
/// Sets a Radio Button (which is passed in as the value property of the control)
/// </summary>
/// <param name="controlValue">the Value property of the radio button</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase SetRadioButtonByValue(string controlValue)
{
selenium.Check("xpath=//input[contains(@value,'" + controlValue + "')]");
return this;
}

/// <summary>
/// Sets a Radio Button containing string in id
/// </summary>
/// <param name="controlId">the Id property of the radio button</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase SetRadioButtonById(string controlId)
{
selenium.Check("xpath=//input[contains(@id,'" + controlId + "')]");
return this;
}

/// <summary>
/// Checks or unchecks a specific CheckBox
/// </summary>
/// <param name="controlId">Control ID of the control (can be only a part of it)</param>
/// <param name="value">true = check it, false = uncheck it</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase SetCheckBox(string controlId, bool value)
{
if (value)
{
selenium.Check(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[contains(@id,'{0}')]",
controlId));
}
else
{
selenium.Uncheck(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[contains(@id,'{0}')]",
controlId));
}

return this;
}

/// <summary>
/// Enters the text into the input field.
/// </summary>
/// <param name="inputControlId">The ID of the input control.</param>
/// <param name="value">The text value to be entered.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase EnterValue(string inputControlId, string value)
{
selenium.Type(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//input[contains(@id,'{0}') or contains(@name,'{0}')]",
inputControlId),
value);
return this;
}

/// <summary>
/// Pauses the test execution for a short period. This method is sometimes useful when you have
/// to wait for the page to load.
/// </summary>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase Pause()
{
Thread.Sleep(1000);
return this;
}

/// <summary>
/// Selects a specified item from the specified drop down list (combo box).
/// </summary>
/// <param name="inputControlId">The ID of the input control.</param>
/// <param name="value">The value to be selected.</param>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase SelectItem(string inputControlId, string value)
{
selenium.Select(
String.Format(
CultureInfo.InvariantCulture,
"xpath=//select[contains(@id,'{0}')]",
inputControlId),
value);
return Pause();
}

/// <summary>
/// Stops the Selenium test session.
/// </summary>
/// <returns>
/// The same <see cref="SeleniumTesterBase"/> object.
/// </returns>
public SeleniumTesterBase Stop()
{
selenium.Stop();
return this;
}

private readonly ISelenium selenium;

private readonly BrowserType browserType = BrowserType.InternetExplorer;

private readonly string testMachine;

private readonly int seleniumPort;

private readonly string seleniumSpeed;

private readonly string browserUrl;
}
}

Change log

r449 by igor.brejc on Feb 12, 2009   Diff
Accipio:
- handled NotImplementedException
- updated test runner base class
- created test suite runner base class
- added log4net logging
Go to: 
Project members, sign in to write a code review

Older revisions

r403 by igor.brejc on Jan 29, 2009   Diff
Finished implementing
HtmlTestReportGenerator.
r244 by andrej.jeznik on Dec 19, 2008   Diff
Test executes some actions on
ProjectPilot web page.
r243 by andrej.jeznik on Dec 19, 2008   Diff
Added .config to Unit tests.
All revisions of this file

File info

Size: 26115 bytes, 666 lines

File properties

svn:mergeinfo
Powered by Google Project Hosting