Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: get rid of Frame::UrlMatches #3058

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Playwright/Core/BrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,13 +721,12 @@ internal async Task OnWebSocketRouteAsync(WebSocketRoute webSocketRoute)
}
}

internal bool UrlMatches(string url, string glob)
=> new Regex(CombineUrlWithBase(glob).GlobToRegex()).Match(url).Success;

internal string CombineUrlWithBase(string url)
{
return URLMatch.JoinWithBaseURL(Options?.BaseURL, url);
}
internal bool UrlMatches(string url, string globMatch)
=> new URLMatch()
{
glob = globMatch,
baseURL = Options.BaseURL,
}.Match(url);

private Task RouteAsync(string globMatch, Regex reMatch, Func<string, bool> funcMatch, Delegate handler, BrowserContextRouteOptions options)
=> RouteAsync(new()
Expand Down
62 changes: 22 additions & 40 deletions src/Playwright/Core/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public Task<IResponse> WaitForNavigationAsync(FrameWaitForNavigationOptions opti
public async Task<IResponse> RunAndWaitForNavigationAsync(Func<Task> action, FrameRunAndWaitForNavigationOptions options = default)
{
using var waiter = SetupNavigationWaiter("frame.WaitForNavigationAsync", options?.Timeout);
var result = WaitForNavigationInternalAsync(waiter, options?.Url, options?.UrlFunc, options?.UrlRegex, options?.UrlString, options?.WaitUntil);
var result = WaitForNavigationInternalAsync(waiter, options?.Url ?? options?.UrlString, options?.UrlFunc, options?.UrlRegex, options?.WaitUntil);

if (action != null)
{
Expand All @@ -276,17 +276,15 @@ await WrapApiBoundaryAsync(() => waiter.CancelWaitOnExceptionAsync(result, actio

private async Task<IResponse> WaitForNavigationInternalAsync(
Waiter waiter,
string url,
string urlString,
Func<string, bool> urlFunc,
Regex urlRegex,
string urlString,
WaitUntilState? waitUntil)
{
WaitUntilState waitUntilNormalized = waitUntil ?? WaitUntilState.Load;
string urlStringNormalized = !string.IsNullOrEmpty(url) ? url! : urlString!;
waitUntil ??= WaitUntilState.Load;
string toUrl = !string.IsNullOrEmpty(urlString) ? $" to \"{urlString}\"" : string.Empty;

waiter.Log($"waiting for navigation{toUrl} until \"{waitUntilNormalized}\"");
waiter.Log($"waiting for navigation{toUrl} until \"{waitUntil}\"");

var navigatedEventTask = waiter.WaitForEventAsync<FrameNavigatedEventArgs>(
this,
Expand All @@ -300,7 +298,13 @@ private async Task<IResponse> WaitForNavigationInternalAsync(
}

waiter.Log($" navigated to \"{e.Url}\"");
return UrlMatches(e.Url, urlStringNormalized, urlRegex, urlFunc);
return new URLMatch()
{
glob = urlString,
re = urlRegex,
func = urlFunc,
baseURL = (Page as Page).Context.Options.BaseURL,
}.Match(e.Url);
});

var navigatedEvent = await navigatedEventTask.ConfigureAwait(false);
Expand All @@ -311,24 +315,23 @@ private async Task<IResponse> WaitForNavigationInternalAsync(
await waiter.WaitForPromiseAsync(Task.FromException<object>(ex)).ConfigureAwait(false);
}

if (!_loadStates.Select(s => s.ToValueString()).Contains(waitUntilNormalized.ToValueString()))
if (!_loadStates.Select(s => s.ToValueString()).Contains(waitUntil.Value.ToValueString()))
{
await waiter.WaitForEventAsync<WaitUntilState>(
this,
"LoadState",
e =>
{
waiter.Log($" \"{e}\" event fired");
return e.ToValueString() == waitUntilNormalized.ToValueString();
return e.ToValueString() == waitUntil.Value.ToValueString();
}).ConfigureAwait(false);
}

var request = navigatedEvent.NewDocument?.Request;
var response = request != null

return request != null
? await waiter.WaitForPromiseAsync(request.FinalRequest.ResponseAsync()).ConfigureAwait(false)
: null;

return response;
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand Down Expand Up @@ -906,7 +909,13 @@ internal async Task<FrameExpectResult> ExpectAsync(string selector, string expre

private Task WaitForURLAsync(string urlString, Regex urlRegex, Func<string, bool> urlFunc, FrameWaitForURLOptions options = default)
{
if (UrlMatches(Url, urlString, urlRegex, urlFunc))
if (new URLMatch()
{
glob = urlString,
re = urlRegex,
func = urlFunc,
baseURL = (Page as Page).Context.Options.BaseURL,
}.Match(Url))
{
return WaitForLoadStateAsync(ToLoadState(options?.WaitUntil), new() { Timeout = options?.Timeout });
}
Expand Down Expand Up @@ -958,33 +967,6 @@ private Waiter SetupNavigationWaiter(string @event, float? timeout)
return waiter;
}

private bool UrlMatches(string url, string matchUrl, Regex regex, Func<string, bool> match)
{
matchUrl = (Page.Context as BrowserContext)?.CombineUrlWithBase(matchUrl);

if (matchUrl == null && regex == null && match == null)
{
return true;
}

if (!string.IsNullOrEmpty(matchUrl))
{
regex = new(matchUrl.GlobToRegex());
}

if (matchUrl != null && url == matchUrl)
{
return true;
}

if (regex != null)
{
return regex.IsMatch(url);
}

return match(url);
}

internal Task HighlightAsync(string selector)
=> SendMessageToServerAsync("highlight", new Dictionary<string, object>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright/Core/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public IFrame Frame(string name)
=> Frames.FirstOrDefault(f => f.Name == name);

[MethodImpl(MethodImplOptions.NoInlining)]
public IFrame FrameByUrl(string urlString) => Frames.FirstOrDefault(f => Context.UrlMatches(urlString, f.Url));
public IFrame FrameByUrl(string urlString) => Frames.FirstOrDefault(f => Context.UrlMatches(f.Url, urlString));

[MethodImpl(MethodImplOptions.NoInlining)]
public IFrame FrameByUrl(Regex urlRegex) => Frames.FirstOrDefault(f => urlRegex.IsMatch(f.Url));
Expand Down
3 changes: 2 additions & 1 deletion src/Playwright/Core/PageAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.Helpers;
using Microsoft.Playwright.Transport.Protocol;

namespace Microsoft.Playwright.Core;
Expand Down Expand Up @@ -57,7 +58,7 @@ public Task ToHaveTitleAsync(Regex titleOrRegExp, PageAssertionsToHaveTitleOptio
ExpectImplAsync("to.have.title", ExpectedRegex(titleOrRegExp, new() { NormalizeWhiteSpace = true }), titleOrRegExp, "Page title expected to be", ConvertToFrameExpectOptions(options));

public Task ToHaveURLAsync(string urlOrRegExp, PageAssertionsToHaveURLOptions options = null) =>
ExpectImplAsync("to.have.url", new ExpectedTextValue() { String = _page.Context.CombineUrlWithBase(urlOrRegExp), IgnoreCase = options?.IgnoreCase }, urlOrRegExp, "Page URL expected to be", ConvertToFrameExpectOptions(options));
ExpectImplAsync("to.have.url", new ExpectedTextValue() { String = URLMatch.JoinWithBaseURL(_page.Context.Options.BaseURL, urlOrRegExp), IgnoreCase = options?.IgnoreCase }, urlOrRegExp, "Page URL expected to be", ConvertToFrameExpectOptions(options));

public Task ToHaveURLAsync(Regex urlOrRegExp, PageAssertionsToHaveURLOptions options = null) =>
ExpectImplAsync("to.have.url", ExpectedRegex(urlOrRegExp, new() { IgnoreCase = options?.IgnoreCase }), urlOrRegExp, "Page URL expected to match regex", ConvertToFrameExpectOptions(options));
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright/Helpers/URLMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public bool Match(string url)
}
return new Regex(globWithBaseURL.GlobToRegex()).IsMatch(url);
}
return false;
return true;
}

internal static string JoinWithBaseURL(string baseUrl, string url)
Expand Down
Loading