Skip to content

Commit b395323

Browse files
committed
Add WaitUntilUrlContains method
1 parent 7f04d64 commit b395323

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

SeleniumUtils/SeleniumUtils.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,51 @@ public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, int
151151

152152
#endregion
153153

154+
#region URL CONTAINS
155+
156+
/// <summary>
157+
/// Polls the driver URL until it contains the fragment of url or until the timeout expires
158+
/// <exception cref="TimeoutException"></exception>
159+
/// </summary>
160+
/// <param name="driver"></param>
161+
/// <param name="targetUrlPart">The URL fragment to look for</param>
162+
/// <param name="timeout">Throws if the URL has not changed before the specified timeout period is elapsed</param>
163+
/// <param name="pollInterval">The time to wait between two polls</param>
164+
public static void WaitUntilUrlContains(this IWebDriver driver, string targetUrlPart, TimeSpan timeout, int pollInterval = 500)
165+
{
166+
var timer = new Stopwatch();
167+
timer.Start();
168+
169+
while (true)
170+
{
171+
if (driver.Url.Contains(targetUrlPart))
172+
return;
173+
174+
var isTimeout = TimeSpan.Compare(timer.Elapsed, timeout) > 0;
175+
if (isTimeout)
176+
throw new TimeoutException($"The URL {driver.Url} has not changed to {targetUrlPart} within the allowed {timeout} milliseconds");
177+
178+
Thread.Sleep(pollInterval);
179+
}
180+
}
181+
182+
/// <summary>
183+
/// Polls the driver URL until it contains the fragment of url or until the timeout expires
184+
/// <exception cref="TimeoutException"></exception>
185+
/// </summary>
186+
/// <param name="driver"></param>
187+
/// <param name="targetUrlPart">The URL fragment to look for</param>
188+
/// <param name="timeout">Throws if the URL has not changed before the specified timeout period is elapsed</param>
189+
/// <param name="pollInterval">The time to wait between two polls</param>
190+
public static void WaitUntilUrlContains(this IWebDriver driver, string targetUrlPart, int timeout = 10000, int pollInterval = 500)
191+
{
192+
193+
var timeoutSpan = new TimeSpan(0, 0, 0, 0, timeout);
194+
WaitUntilUrlContains(driver, targetUrlPart, timeoutSpan, pollInterval);
195+
}
196+
197+
#endregion
198+
154199
#endregion
155200
}
156201
}

SeleniumUtils/SeleniumUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
1515
<Description>A collection of utility methods for Selenium Webdriver</Description>
1616
<NeutralLanguage>en</NeutralLanguage>
17-
<Version>1.2.4</Version>
17+
<Version>1.3.0</Version>
1818
<RootNamespace>Wndrr.Selenium</RootNamespace>
1919
<AssemblyName>Wndrr.Selenium</AssemblyName>
2020
</PropertyGroup>

0 commit comments

Comments
 (0)