Skip to content

Commit 44eb38c

Browse files
committed
Add trimTrailingSlash param
1 parent 113f05e commit 44eb38c

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

SeleniumUtils/SeleniumUtils.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,25 @@ public static void WaitUntilCurrentUrlChange(this IWebDriver driver, int timeout
116116
/// <param name="targetUrl">The URL to look for</param>
117117
/// <param name="timeout">Throws if the URL has not changed before the specified timeout period is elapsed</param>
118118
/// <param name="pollInterval">The time to wait between two polls</param>
119-
public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, TimeSpan timeout, int pollInterval = 500)
119+
/// <param name="trimTrailingSlash">Remove trailing slashes from the internet call to <paramref name="driver"/>.Url</param>
120+
public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, TimeSpan timeout, int pollInterval = 500, bool trimTrailingSlash = true)
120121
{
121122
var timer = new Stopwatch();
122123
timer.Start();
123124

124125
while (true)
125126
{
126-
if (driver.Url == targetUrl)
127+
var driverUrl = driver.Url;
128+
129+
if (trimTrailingSlash)
130+
driverUrl = driverUrl.TrimEnd('/');
131+
132+
if (driverUrl == targetUrl)
127133
return;
128134

129135
var isTimeout = TimeSpan.Compare(timer.Elapsed, timeout) > 0;
130136
if (isTimeout)
131-
throw new TimeoutException($"The URL {driver.Url} has not changed to {targetUrl} within the allowed {timeout} milliseconds");
137+
throw new TimeoutException($"The URL {driverUrl} has not changed to {targetUrl} within the allowed {timeout} milliseconds");
132138

133139
Thread.Sleep(pollInterval);
134140
}
@@ -142,11 +148,12 @@ public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, Time
142148
/// <param name="targetUrl">The URL to look for</param>
143149
/// <param name="timeout">Throws if the URL has not changed before the specified timeout period is elapsed</param>
144150
/// <param name="pollInterval">The time to wait between two polls</param>
145-
public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, int timeout = 10000, int pollInterval = 500)
151+
/// <param name="trimTrailingSlash">Remove trailing slashes from the internet call to <paramref name="driver"/>.Url</param>
152+
public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, int timeout = 10000, int pollInterval = 500, bool trimTrailingSlash = true)
146153
{
147154

148155
var timeoutSpan = new TimeSpan(0, 0, 0, 0, timeout);
149-
WaitUntilUrlIs(driver, targetUrl, timeoutSpan, pollInterval);
156+
WaitUntilUrlIs(driver, targetUrl, timeoutSpan, pollInterval, trimTrailingSlash);
150157
}
151158

152159
#endregion
@@ -161,19 +168,25 @@ public static void WaitUntilUrlIs(this IWebDriver driver, string targetUrl, int
161168
/// <param name="targetUrlPart">The URL fragment to look for</param>
162169
/// <param name="timeout">Throws if the URL has not changed before the specified timeout period is elapsed</param>
163170
/// <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)
171+
/// <param name="trimTrailingSlash">Remove trailing slashes from the internet call to <paramref name="driver"/>.Url</param>
172+
public static void WaitUntilUrlContains(this IWebDriver driver, string targetUrlPart, TimeSpan timeout, int pollInterval = 500, bool trimTrailingSlash = true)
165173
{
166174
var timer = new Stopwatch();
167175
timer.Start();
168176

169177
while (true)
170178
{
171-
if (driver.Url.Contains(targetUrlPart))
179+
var driverUrl = driver.Url;
180+
181+
if (trimTrailingSlash)
182+
driverUrl = driverUrl.TrimEnd('/');
183+
184+
if (driverUrl.Contains(targetUrlPart))
172185
return;
173186

174187
var isTimeout = TimeSpan.Compare(timer.Elapsed, timeout) > 0;
175188
if (isTimeout)
176-
throw new TimeoutException($"The URL {driver.Url} has not changed to {targetUrlPart} within the allowed {timeout} milliseconds");
189+
throw new TimeoutException($"The URL {driverUrl} has not changed to {targetUrlPart} within the allowed {timeout} milliseconds");
177190

178191
Thread.Sleep(pollInterval);
179192
}
@@ -187,11 +200,12 @@ public static void WaitUntilUrlContains(this IWebDriver driver, string targetUrl
187200
/// <param name="targetUrlPart">The URL fragment to look for</param>
188201
/// <param name="timeout">Throws if the URL has not changed before the specified timeout period is elapsed</param>
189202
/// <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)
203+
/// <param name="trimTrailingSlash">Remove trailing slashes from the internet call to <paramref name="driver"/>.Url</param>
204+
public static void WaitUntilUrlContains(this IWebDriver driver, string targetUrlPart, int timeout = 10000, int pollInterval = 500, bool trimTrailingSlash = true)
191205
{
192206

193207
var timeoutSpan = new TimeSpan(0, 0, 0, 0, timeout);
194-
WaitUntilUrlContains(driver, targetUrlPart, timeoutSpan, pollInterval);
208+
WaitUntilUrlContains(driver, targetUrlPart, timeoutSpan, pollInterval, trimTrailingSlash);
195209
}
196210

197211
#endregion

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.3.0</Version>
17+
<Version>1.3.1</Version>
1818
<RootNamespace>Wndrr.Selenium</RootNamespace>
1919
<AssemblyName>Wndrr.Selenium</AssemblyName>
2020
</PropertyGroup>

0 commit comments

Comments
 (0)