Skip to content

Commit

Permalink
Options refactor (#806)
Browse files Browse the repository at this point in the history
* Options refactor

related to #641

* dotnet format

* ops
  • Loading branch information
kblok authored Sep 5, 2020
1 parent 4004dbb commit 39c61c5
Show file tree
Hide file tree
Showing 19 changed files with 1,147 additions and 646 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task ShouldBypassCSPMetatag()
Assert.Null(await page.EvaluateAsync("window.__injected"));
}
// By-pass CSP and try one more time.
await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true }))
await using (var context = await Browser.NewContextAsync(bypassCSP: true))
{
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/csp.html");
Expand All @@ -58,7 +58,7 @@ public async Task ShouldBypassCSPHeader()
}

// By-pass CSP and try one more time.
await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true }))
await using (var context = await Browser.NewContextAsync(bypassCSP: true))
{
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
Expand All @@ -73,7 +73,7 @@ public async Task ShouldBypassCSPHeader()
[Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]
public async Task ShouldBypassAfterCrossProcessNavigation()
{
await using var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true });
await using var context = await Browser.NewContextAsync(bypassCSP: true);
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/csp.html");
await page.AddScriptTagAsync(content: "window.__injected = 42;");
Expand Down Expand Up @@ -102,7 +102,7 @@ public async Task ShouldBypassCSPInIframesAsWell()
}

// By-pass CSP and try one more time.
await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { BypassCSP = true }))
await using (var context = await Browser.NewContextAsync(bypassCSP: true))
{
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public BrowserContextJavaScriptEnabledTests(ITestOutputHelper output) : base(out
[Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]
public async Task ShouldWork()
{
await using (var context = await Browser.NewContextAsync(new BrowserContextOptions { JavaScriptEnabled = false }))
await using (var context = await Browser.NewContextAsync(javaScriptEnabled: false))
{
var page = await context.NewPageAsync();
await page.GoToAsync("data:text/html, <script>var something = 'forbidden'</script>");
Expand All @@ -49,7 +49,7 @@ public async Task ShouldWork()
[Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]
public async Task ShouldBeAbleToNavigateAfterDisablingJavascript()
{
await using var context = await Browser.NewContextAsync(new BrowserContextOptions { JavaScriptEnabled = false });
await using var context = await Browser.NewContextAsync(javaScriptEnabled: false);
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public async Task ShouldFireCloseEventForAPersistentContext()
var tmp = new TempDirectory();
var context = await BrowserType.LaunchPersistenContextAsync(
tmp.Path,
(LaunchPersistentOptions)TestConstants.GetDefaultBrowserOptions() + (options ?? new BrowserContextOptions()));
TestConstants.GetDefaultBrowserOptions().ToPersistentOptions() + (options ?? new BrowserContextOptions()));
var page = context.Pages.First();

return (tmp, context, page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task InitializeAsync()
_downloadsPath = new TempDirectory();
_userDataDir = new TempDirectory();

var options = (LaunchPersistentOptions)TestConstants.GetDefaultBrowserOptions();
var options = TestConstants.GetDefaultBrowserOptions().ToPersistentOptions();
options.DownloadsPath = _downloadsPath.Path;
options.AcceptDownloads = true;
_context = await Playwright[TestConstants.Product].LaunchPersistenContextAsync(_userDataDir.Path, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public BrowserContextLocaleTests(ITestOutputHelper output) : base(output)
[Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]
public async Task ShouldAffectAcceptLanguageHeader()
{
await using var context = await Browser.NewContextAsync(new BrowserContextOptions
{
Locale = "fr-CH"
});
await using var context = await Browser.NewContextAsync(locale: "fr-CH");
string acceptLanguage = string.Empty;
var page = await context.NewPageAsync();
var requestTask = Server.WaitForRequest("/empty.html", c => acceptLanguage = c.Headers["accept-language"]);
Expand Down
2 changes: 1 addition & 1 deletion src/PlaywrightSharp.Tests/Page/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ await TaskUtils.WhenAll(
[Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]
public async Task ShouldReportDownloadsWithAcceptDownloadsTrue()
{
var page = await Browser.NewPageAsync(new BrowserContextOptions { AcceptDownloads = true });
var page = await Browser.NewPageAsync(acceptDownloads: true);
await page.SetContentAsync($"<a href=\"{TestConstants.ServerUrl}/download\">download</a>");
var downloadTask = page.WaitForEvent<DownloadEventArgs>(PageEvent.Download);

Expand Down
3 changes: 1 addition & 2 deletions src/PlaywrightSharp.Tests/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class TestConstants
public const string FirefoxProduct = "FIREFOX";

public static string Product => string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PRODUCT")) ?
WebkitProduct :
ChromiumProduct :
Environment.GetEnvironmentVariable("PRODUCT");

public const string TestFixtureBrowserCollectionName = "PlaywrightSharpBrowserLoaderFixture collection";
Expand All @@ -34,7 +34,6 @@ internal static LaunchOptions GetDefaultBrowserOptions()
SlowMo = Convert.ToInt32(Environment.GetEnvironmentVariable("SLOW_MO")),
Headless = Convert.ToBoolean(Environment.GetEnvironmentVariable("HEADLESS") ?? "true"),
Timeout = 0,
LogProcess = true,
};

public static LaunchOptions GetHeadfulOptions()
Expand Down
154 changes: 151 additions & 3 deletions src/PlaywrightSharp/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,83 @@ public async Task CloseAsync()
}

/// <inheritdoc/>
public async Task<IBrowserContext> NewContextAsync(BrowserContextOptions options = null)
public Task<IBrowserContext> NewContextAsync(
ViewportSize viewport,
string userAgent = null,
bool? bypassCSP = null,
bool? javaScriptEnabled = null,
string timezoneId = null,
Geolocation geolocation = null,
ContextPermission[] permissions = null,
bool? isMobile = null,
bool? offline = null,
decimal? deviceScaleFactor = null,
Credentials httpCredentials = null,
bool? hasTouch = null,
bool? acceptDownloads = null,
ColorScheme? colorScheme = null,
string locale = null,
Dictionary<string, string> extraHttpHeaders = null)
=> NewContextAsync(new BrowserContextOptions
{
Viewport = viewport,
UserAgent = userAgent,
BypassCSP = bypassCSP,
JavaScriptEnabled = javaScriptEnabled,
TimezoneId = timezoneId,
Geolocation = geolocation,
Permissions = permissions,
IsMobile = isMobile,
Offline = offline,
DeviceScaleFactor = deviceScaleFactor,
HttpCredentials = httpCredentials,
HasTouch = hasTouch,
AcceptDownloads = acceptDownloads,
ColorScheme = colorScheme,
Locale = locale,
ExtraHttpHeaders = extraHttpHeaders,
});

/// <inheritdoc/>
public Task<IBrowserContext> NewContextAsync(
string userAgent = null,
bool? bypassCSP = null,
bool? javaScriptEnabled = null,
string timezoneId = null,
Geolocation geolocation = null,
ContextPermission[] permissions = null,
bool? isMobile = null,
bool? offline = null,
decimal? deviceScaleFactor = null,
Credentials httpCredentials = null,
bool? hasTouch = null,
bool? acceptDownloads = null,
ColorScheme? colorScheme = null,
string locale = null,
Dictionary<string, string> extraHttpHeaders = null)
=> NewContextAsync(new BrowserContextOptions
{
UserAgent = userAgent,
BypassCSP = bypassCSP,
JavaScriptEnabled = javaScriptEnabled,
TimezoneId = timezoneId,
Geolocation = geolocation,
Permissions = permissions,
IsMobile = isMobile,
Offline = offline,
DeviceScaleFactor = deviceScaleFactor,
HttpCredentials = httpCredentials,
HasTouch = hasTouch,
AcceptDownloads = acceptDownloads,
ColorScheme = colorScheme,
Locale = locale,
ExtraHttpHeaders = extraHttpHeaders,
});

/// <inheritdoc/>
public async Task<IBrowserContext> NewContextAsync(BrowserContextOptions options)
{
var context = (await _channel.NewContextAsync(options).ConfigureAwait(false)).Object;
var context = (await _channel.NewContextAsync(options ?? new BrowserContextOptions()).ConfigureAwait(false)).Object;
BrowserContextsList.Add(context);
context.Browser = this;
return context;
Expand All @@ -89,7 +163,81 @@ public async Task<IBrowserContext> NewContextAsync(BrowserContextOptions options
public async Task<ICDPSession> NewBrowserCDPSessionAsync() => (await _channel.NewBrowserCDPSessionAsync().ConfigureAwait(false)).Object;

/// <inheritdoc/>
public async Task<IPage> NewPageAsync(BrowserContextOptions options = null)
public Task<IPage> NewPageAsync(
ViewportSize viewport,
string userAgent = null,
bool? bypassCSP = null,
bool? javaScriptEnabled = null,
string timezoneId = null,
Geolocation geolocation = null,
ContextPermission[] permissions = null,
bool? isMobile = null,
bool? offline = null,
decimal? deviceScaleFactor = null,
Credentials httpCredentials = null,
bool? hasTouch = null,
bool? acceptDownloads = null,
ColorScheme? colorScheme = null,
string locale = null,
Dictionary<string, string> extraHttpHeaders = null)
=> NewPageAsync(new BrowserContextOptions
{
Viewport = viewport,
UserAgent = userAgent,
BypassCSP = bypassCSP,
JavaScriptEnabled = javaScriptEnabled,
TimezoneId = timezoneId,
Geolocation = geolocation,
Permissions = permissions,
IsMobile = isMobile,
Offline = offline,
DeviceScaleFactor = deviceScaleFactor,
HttpCredentials = httpCredentials,
HasTouch = hasTouch,
AcceptDownloads = acceptDownloads,
ColorScheme = colorScheme,
Locale = locale,
ExtraHttpHeaders = extraHttpHeaders,
});

/// <inheritdoc/>
public Task<IPage> NewPageAsync(
string userAgent = null,
bool? bypassCSP = null,
bool? javaScriptEnabled = null,
string timezoneId = null,
Geolocation geolocation = null,
ContextPermission[] permissions = null,
bool? isMobile = null,
bool? offline = null,
decimal? deviceScaleFactor = null,
Credentials httpCredentials = null,
bool? hasTouch = null,
bool? acceptDownloads = null,
ColorScheme? colorScheme = null,
string locale = null,
Dictionary<string, string> extraHttpHeaders = null)
=> NewPageAsync(new BrowserContextOptions
{
UserAgent = userAgent,
BypassCSP = bypassCSP,
JavaScriptEnabled = javaScriptEnabled,
TimezoneId = timezoneId,
Geolocation = geolocation,
Permissions = permissions,
IsMobile = isMobile,
Offline = offline,
DeviceScaleFactor = deviceScaleFactor,
HttpCredentials = httpCredentials,
HasTouch = hasTouch,
AcceptDownloads = acceptDownloads,
ColorScheme = colorScheme,
Locale = locale,
ExtraHttpHeaders = extraHttpHeaders,
});

/// <inheritdoc/>
public async Task<IPage> NewPageAsync(BrowserContextOptions options)
{
var context = await NewContextAsync(options).ConfigureAwait(false) as BrowserContext;
var page = await context.NewPageAsync().ConfigureAwait(false) as Page;
Expand Down
Loading

0 comments on commit 39c61c5

Please sign in to comment.