Skip to content

Commit 3244bf4

Browse files
committed
Updating .NET tests to provide better error messages for failed wait conditions
1 parent 723563b commit 3244bf4

17 files changed

+165
-153
lines changed

dotnet/test/common/AlertsTest.cs

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

dotnet/test/common/ClickScrollingTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void ShouldBeAbleToClickOnAnElementHiddenByDoubleOverflow()
6363
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("scrolling_tests/page_with_double_overflow_auto.html");
6464

6565
driver.FindElement(By.Id("link")).Click();
66-
WaitFor(TitleToBe("Clicked Successfully!"));
66+
WaitFor(TitleToBe("Clicked Successfully!"), "Browser title was not 'Clicked Successfully'");
6767
}
6868

6969
[Test]
@@ -73,7 +73,7 @@ public void ShouldBeAbleToClickOnAnElementHiddenByYOverflow()
7373
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("scrolling_tests/page_with_y_overflow_auto.html");
7474

7575
driver.FindElement(By.Id("link")).Click();
76-
WaitFor(TitleToBe("Clicked Successfully!"));
76+
WaitFor(TitleToBe("Clicked Successfully!"), "Browser title was not 'Clicked Successfully'");
7777
}
7878

7979
[Test]

dotnet/test/common/ClickTest.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void TearDownMethod()
2626
public void CanClickOnALinkAndFollowIt()
2727
{
2828
driver.FindElement(By.Id("normal")).Click();
29-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
29+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
3030
Assert.AreEqual("XHTML Test Page", driver.Title);
3131
}
3232

@@ -36,7 +36,7 @@ public void CanClickOnALinkThatOverflowsAndFollowIt()
3636
{
3737
driver.FindElement(By.Id("overflowLink")).Click();
3838

39-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
39+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
4040
}
4141

4242
[Test]
@@ -113,7 +113,7 @@ public void CanClickOnAnElementWithTopSetToANegativeNumber()
113113
public void ShouldClickOnFirstBoundingClientRectWithNonZeroSize()
114114
{
115115
driver.FindElement(By.Id("twoClientRects")).Click();
116-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
116+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
117117
Assert.AreEqual("XHTML Test Page", driver.Title);
118118
}
119119

@@ -155,7 +155,7 @@ public void ShouldOnlyFollowHrefOnce()
155155
int windowHandlesBefore = driver.WindowHandles.Count;
156156

157157
driver.FindElement(By.Id("new-window")).Click();
158-
WaitFor(() => { return driver.WindowHandles.Count >= windowHandlesBefore + 1; });
158+
WaitFor(() => { return driver.WindowHandles.Count >= windowHandlesBefore + 1; }, "Window handles was not " + (windowHandlesBefore + 1).ToString());
159159
Assert.AreEqual(windowHandlesBefore + 1, driver.WindowHandles.Count);
160160
}
161161

@@ -181,39 +181,39 @@ public void ClickingLabelShouldSetCheckbox()
181181
public void CanClickOnALinkWithEnclosedImage()
182182
{
183183
driver.FindElement(By.Id("link-with-enclosed-image")).Click();
184-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
184+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
185185
Assert.AreEqual("XHTML Test Page", driver.Title);
186186
}
187187

188188
[Test]
189189
public void CanClickOnAnImageEnclosedInALink()
190190
{
191191
driver.FindElement(By.Id("link-with-enclosed-image")).FindElement(By.TagName("img")).Click();
192-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
192+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
193193
Assert.AreEqual("XHTML Test Page", driver.Title);
194194
}
195195

196196
[Test]
197197
public void CanClickOnALinkThatContainsTextWrappedInASpan()
198198
{
199199
driver.FindElement(By.Id("link-with-enclosed-span")).Click();
200-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
200+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
201201
Assert.AreEqual("XHTML Test Page", driver.Title);
202202
}
203203

204204
[Test]
205205
public void CanClickOnALinkThatContainsEmbeddedBlockElements()
206206
{
207207
driver.FindElement(By.Id("embeddedBlock")).Click();
208-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
208+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
209209
Assert.AreEqual("XHTML Test Page", driver.Title);
210210
}
211211

212212
[Test]
213213
public void CanClickOnAnElementEnclosedInALink()
214214
{
215215
driver.FindElement(By.Id("link-with-enclosed-span")).FindElement(By.TagName("span")).Click();
216-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
216+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
217217
Assert.AreEqual("XHTML Test Page", driver.Title);
218218
}
219219

@@ -222,7 +222,7 @@ public void ShouldBeAbleToClickLinkContainingLineBreak()
222222
{
223223
driver.Url = simpleTestPage;
224224
driver.FindElement(By.Id("multilinelink")).Click();
225-
WaitFor(() => { return driver.Title == "We Arrive Here"; });
225+
WaitFor(() => { return driver.Title == "We Arrive Here"; }, "Browser title was not 'We Arrive Here'");
226226
Assert.AreEqual("We Arrive Here", driver.Title);
227227
}
228228

@@ -242,7 +242,7 @@ public void ClicksASurroundingStrongTag()
242242
{
243243
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("ClickTest_testClicksASurroundingStrongTag.html");
244244
driver.FindElement(By.TagName("a")).Click();
245-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
245+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
246246
}
247247

248248
[Test]
@@ -255,15 +255,15 @@ public void CanClickAnImageMapArea()
255255
{
256256
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("click_tests/google_map.html");
257257
driver.FindElement(By.Id("rectG")).Click();
258-
WaitFor(() => { return driver.Title == "Target Page 1"; });
258+
WaitFor(() => { return driver.Title == "Target Page 1"; }, "Browser title was not 'Target Page 1'");
259259

260260
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("click_tests/google_map.html");
261261
driver.FindElement(By.Id("circleO")).Click();
262-
WaitFor(() => { return driver.Title == "Target Page 2"; });
262+
WaitFor(() => { return driver.Title == "Target Page 2"; }, "Browser title was not 'Target Page 2'");
263263

264264
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("click_tests/google_map.html");
265265
driver.FindElement(By.Id("polyLE")).Click();
266-
WaitFor(() => { return driver.Title == "Target Page 3"; });
266+
WaitFor(() => { return driver.Title == "Target Page 3"; }, "Browser title was not 'Target Page 3'");
267267
}
268268

269269
[Test]
@@ -276,7 +276,7 @@ public void ShouldBeAbleToClickOnAnElementGreaterThanTwoViewports()
276276

277277
element.Click();
278278

279-
WaitFor(() => { return driver.Title == "clicks"; });
279+
WaitFor(() => { return driver.Title == "clicks"; }, "Browser title was not 'clicks'");
280280
}
281281

282282
[Test]
@@ -298,7 +298,7 @@ public void ShouldBeAbleToClickOnAnElementInFrameGreaterThanTwoViewports()
298298

299299
element.Click();
300300

301-
WaitFor(() => { return driver.Title == "clicks"; });
301+
WaitFor(() => { return driver.Title == "clicks"; }, "Browser title was not 'clicks'");
302302
}
303303

304304
[Test]
@@ -308,7 +308,7 @@ public void ShouldBeAbleToClickOnLinkInAbsolutelyPositionedFooter()
308308
driver.Url = url;
309309

310310
driver.FindElement(By.Id("link")).Click();
311-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
311+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
312312
Assert.AreEqual("XHTML Test Page", driver.Title);
313313
}
314314

@@ -319,7 +319,7 @@ public void ShouldBeAbleToClickOnLinkInAbsolutelyPositionedFooterInQuirksMode()
319319
driver.Url = url;
320320

321321
driver.FindElement(By.Id("link")).Click();
322-
WaitFor(() => { return driver.Title == "XHTML Test Page"; });
322+
WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title was not 'XHTML Test Page'");
323323
Assert.AreEqual("XHTML Test Page", driver.Title);
324324
}
325325
}

dotnet/test/common/CorrectEventFiringTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public void ShouldFireTwoClickEventsWhenClickingOnALabel()
213213
driver.FindElement(By.Id("labelForCheckbox")).Click();
214214

215215
IWebElement result = driver.FindElement(By.Id("result"));
216-
Assert.IsTrue(WaitFor(() => { return result.Text.Contains("labelclick chboxclick"); }), "Did not find text: " + result.Text);
216+
Assert.IsTrue(WaitFor(() => { return result.Text.Contains("labelclick chboxclick"); }, "Did not find text: " + result.Text));
217217
}
218218

219219

dotnet/test/common/DriverTestFixture.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,17 @@ protected bool IsIeDriverTimedOutException(Exception e)
128128
return e.GetType().Name.Contains("TimedOutException");
129129
}
130130

131-
protected bool WaitFor(Func<bool> waitFunction)
131+
protected bool WaitFor(Func<bool> waitFunction, string timeoutMessage)
132132
{
133-
return WaitFor<bool>(waitFunction);
133+
return WaitFor<bool>(waitFunction, timeoutMessage);
134134
}
135135

136-
protected T WaitFor<T>(Func<T> waitFunction)
136+
protected T WaitFor<T>(Func<T> waitFunction, string timeoutMessage)
137137
{
138-
return this.WaitFor<T>(waitFunction, TimeSpan.FromSeconds(5));
138+
return this.WaitFor<T>(waitFunction, TimeSpan.FromSeconds(5), timeoutMessage);
139139
}
140140

141-
protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout)
141+
protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout, string timeoutMessage)
142142
{
143143
DateTime endTime = DateTime.Now.Add(timeout);
144144
T value = default(T);
@@ -174,7 +174,7 @@ protected T WaitFor<T>(Func<T> waitFunction, TimeSpan timeout)
174174
throw new WebDriverException("Operation timed out", lastException);
175175
}
176176

177-
Assert.Fail("Condition timed out: " + waitFunction);
177+
Assert.Fail("Condition timed out: " + timeoutMessage);
178178
return default(T);
179179
}
180180
}

dotnet/test/common/ElementFindingTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ public void ShouldBeAbleToClickOnLinkIdentifiedByText()
889889
{
890890
driver.Url = xhtmlTestPage;
891891
driver.FindElement(By.LinkText("click me")).Click();
892-
WaitFor(() => { return driver.Title == "We Arrive Here"; });
892+
WaitFor(() => { return driver.Title == "We Arrive Here"; }, "Browser title is not 'We Arrive Here'");
893893
Assert.AreEqual(driver.Title, "We Arrive Here");
894894
}
895895

@@ -898,7 +898,7 @@ public void ShouldBeAbleToClickOnLinkIdentifiedById()
898898
{
899899
driver.Url = xhtmlTestPage;
900900
driver.FindElement(By.Id("linkId")).Click();
901-
WaitFor(() => { return driver.Title == "We Arrive Here"; });
901+
WaitFor(() => { return driver.Title == "We Arrive Here"; }, "Browser title is not 'We Arrive Here'");
902902
Assert.AreEqual(driver.Title, "We Arrive Here");
903903
}
904904

dotnet/test/common/FormHandlingTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void ShouldClickOnSubmitInputElements()
1414
{
1515
driver.Url = formsPage;
1616
driver.FindElement(By.Id("submitButton")).Click();
17-
WaitFor(TitleToBe("We Arrive Here"));
17+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
1818
Assert.AreEqual(driver.Title, "We Arrive Here");
1919
}
2020

@@ -30,7 +30,7 @@ public void ShouldBeAbleToClickImageButtons()
3030
{
3131
driver.Url = formsPage;
3232
driver.FindElement(By.Id("imageButton")).Click();
33-
WaitFor(TitleToBe("We Arrive Here"));
33+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
3434
Assert.AreEqual(driver.Title, "We Arrive Here");
3535
}
3636

@@ -39,7 +39,7 @@ public void ShouldBeAbleToSubmitForms()
3939
{
4040
driver.Url = formsPage;
4141
driver.FindElement(By.Name("login")).Submit();
42-
WaitFor(TitleToBe("We Arrive Here"));
42+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
4343
Assert.AreEqual(driver.Title, "We Arrive Here");
4444
}
4545

@@ -48,7 +48,7 @@ public void ShouldSubmitAFormWhenAnyInputElementWithinThatFormIsSubmitted()
4848
{
4949
driver.Url = formsPage;
5050
driver.FindElement(By.Id("checky")).Submit();
51-
WaitFor(TitleToBe("We Arrive Here"));
51+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
5252
Assert.AreEqual(driver.Title, "We Arrive Here");
5353
}
5454

@@ -57,7 +57,7 @@ public void ShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted()
5757
{
5858
driver.Url = formsPage;
5959
driver.FindElement(By.XPath("//form/p")).Submit();
60-
WaitFor(TitleToBe("We Arrive Here"));
60+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
6161
Assert.AreEqual(driver.Title, "We Arrive Here");
6262
}
6363

@@ -103,7 +103,7 @@ public void ShouldSubmitAFormUsingTheNewlineLiteral()
103103
IWebElement input = nestedForm.FindElement(By.Name("x"));
104104
input.SendKeys("\n");
105105

106-
WaitFor(TitleToBe("We Arrive Here"));
106+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
107107
Assert.AreEqual("We Arrive Here", driver.Title);
108108
Assert.IsTrue(driver.Url.EndsWith("?x=name"));
109109
}
@@ -116,7 +116,7 @@ public void ShouldSubmitAFormUsingTheEnterKey()
116116
IWebElement input = nestedForm.FindElement(By.Name("x"));
117117
input.SendKeys(Keys.Enter);
118118

119-
WaitFor(TitleToBe("We Arrive Here"));
119+
WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
120120
Assert.AreEqual("We Arrive Here", driver.Title);
121121
Assert.IsTrue(driver.Url.EndsWith("?x=name"));
122122
}
@@ -366,7 +366,7 @@ private void CheckSubmitButton(string buttonId)
366366
driver.FindElement(By.Id("name")).SendKeys(name);
367367
driver.FindElement(By.Id(buttonId)).Click();
368368

369-
WaitFor(TitleToBe("Submitted Successfully!"));
369+
WaitFor(TitleToBe("Submitted Successfully!"), "Browser title is not 'Submitted Successfully!'");
370370

371371
Assert.That(driver.Url.Contains("name=" + name), "URL does not contain 'name=" + name + "'. Actual URL:" + driver.Url);
372372
}

dotnet/test/common/FrameSwitchingTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ public void ShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUs()
450450
}
451451

452452
return success != null;
453-
});
453+
}, "Element with id 'success' still exists on page");
454454
}
455455
catch (WebDriverException)
456456
{

dotnet/test/common/Interactions/BasicMouseInterfaceTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void ShouldClickElementInIFrame()
187187
IWebElement element = driver.FindElement(By.Id("otherframe"));
188188
new Actions(driver).MoveToElement(element).Click().Perform();
189189
driver.SwitchTo().DefaultContent().SwitchTo().Frame("target");
190-
WaitFor(() => { return driver.FindElement(By.Id("span")).Text == "An inline element"; });
190+
WaitFor(() => { return driver.FindElement(By.Id("span")).Text == "An inline element"; }, "Could not find element with text 'An inline element'");
191191
}
192192
finally
193193
{
@@ -242,11 +242,11 @@ public void MovingMouseByRelativeOffset()
242242

243243
IWebElement reporter = driver.FindElement(By.Id("status"));
244244

245-
WaitFor(FuzzyMatchingOfCoordinates(reporter, 50, 200));
245+
WaitFor(FuzzyMatchingOfCoordinates(reporter, 50, 200), "Coordinate matching was not within tolerance");
246246

247247
new Actions(driver).MoveByOffset(10, 20).Build().Perform();
248248

249-
WaitFor(FuzzyMatchingOfCoordinates(reporter, 60, 220));
249+
WaitFor(FuzzyMatchingOfCoordinates(reporter, 60, 220), "Coordinate matching was not within tolerance");
250250
}
251251

252252
[Test]
@@ -262,7 +262,7 @@ public void MovingMouseToRelativeElementOffset()
262262

263263
IWebElement reporter = driver.FindElement(By.Id("status"));
264264

265-
WaitFor(FuzzyMatchingOfCoordinates(reporter, 95, 195));
265+
WaitFor(FuzzyMatchingOfCoordinates(reporter, 95, 195), "Coordinate matching was not within tolerance");
266266
}
267267

268268
[Test]
@@ -278,7 +278,7 @@ public void MoveRelativeToBody()
278278

279279
IWebElement reporter = driver.FindElement(By.Id("status"));
280280

281-
WaitFor(FuzzyMatchingOfCoordinates(reporter, 40, 20));
281+
WaitFor(FuzzyMatchingOfCoordinates(reporter, 40, 20), "Coordinate matching was not within tolerance");
282282
}
283283

284284
[Test]

0 commit comments

Comments
 (0)