Skip to content

Commit

Permalink
Expose DevTools as a target (#2321)
Browse files Browse the repository at this point in the history
* Expose DevTools as a target

* hide some methods

* add missing doc

* DevToolsTarget is a PageTarget

* fix constructor

* Ignore on windows
  • Loading branch information
kblok authored Sep 20, 2023
1 parent 19c75cd commit 06ce60a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
20 changes: 19 additions & 1 deletion lib/PuppeteerSharp.Tests/HeadfulTests/HeadfulTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace PuppeteerSharp.Tests.HeadfulTests
public class HeadfulTests : PuppeteerBaseTest
{
private readonly LaunchOptions _forcedOopifOptions;

public HeadfulTests(): base()
{
_forcedOopifOptions = TestConstants.DefaultBrowserOptions();
Expand Down Expand Up @@ -218,5 +218,23 @@ await Task.WhenAll(
browser.WaitForTargetAsync(target => target.Url.Contains("devtools://")));
}
}

[PuppeteerTest("headful.spec.ts", "HEADFUL", "should expose DevTools as a page")]
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Windows)]
public async Task ShouldExposeDevToolsAsAPage()
{
var headfulOptions = TestConstants.DefaultBrowserOptions();
headfulOptions.Devtools = true;
await using var browser = await Puppeteer.LaunchAsync(headfulOptions);
var context = await browser.CreateIncognitoBrowserContextAsync();
var targetTask = browser.WaitForTargetAsync(target => target.Url.Contains("devtools://"));
await Task.WhenAll(
context.NewPageAsync(),
targetTask);

var target = await targetTask;
var page = await target.PageAsync();
Assert.True(await page.EvaluateExpressionAsync<bool>("Boolean(DevToolsAPI)"));
}
}
}
13 changes: 13 additions & 0 deletions lib/PuppeteerSharp/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ private Target CreateTarget(TargetInfo targetInfo, CDPSession session)
TargetManager,
createSession);

if (targetInfo.Url?.StartsWith("devtools://", StringComparison.OrdinalIgnoreCase) == true)
{
return new DevToolsTarget(
targetInfo,
session,
context,
TargetManager,
createSession,
IgnoreHTTPSErrors,
DefaultViewport,
ScreenshotTaskQueue);
}

if (IsPageTargetFunc(otherTarget))
{
return new PageTarget(
Expand Down
25 changes: 25 additions & 0 deletions lib/PuppeteerSharp/DevToolsTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using PuppeteerSharp.Helpers;

namespace PuppeteerSharp
{
/// <summary>
/// DevTools target.
/// </summary>
public class DevToolsTarget : PageTarget
{
internal DevToolsTarget(
TargetInfo targetInfo,
CDPSession session,
BrowserContext context,
ITargetManager targetManager,
Func<bool, Task<CDPSession>> sessionFactory,
bool ignoreHTTPSErrors,
ViewPortOptions defaultViewport,
TaskQueue screenshotTaskQueue)
: base(targetInfo, session, context, targetManager, sessionFactory, ignoreHTTPSErrors, defaultViewport, screenshotTaskQueue)
{
}
}
}
7 changes: 5 additions & 2 deletions lib/PuppeteerSharp/OtherTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace PuppeteerSharp
{
internal class OtherTarget : Target
/// <summary>
/// Other target.
/// </summary>
public class OtherTarget : Target
{
public OtherTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> createSession) : base(targetInfo, session, context, targetManager, createSession)
internal OtherTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> createSession) : base(targetInfo, session, context, targetManager, createSession)
{
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/PuppeteerSharp/PageTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace PuppeteerSharp
{
internal class PageTarget : Target
/// <summary>
/// Page target.
/// </summary>
public class PageTarget : Target
{
private readonly bool _ignoreHTTPSErrors;
private readonly ViewPortOptions _defaultViewport;
private readonly TaskQueue _screenshotTaskQueue;

public PageTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> sessionFactory, bool ignoreHTTPSErrors, ViewPortOptions defaultViewport, TaskQueue screenshotTaskQueue)
internal PageTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> sessionFactory, bool ignoreHTTPSErrors, ViewPortOptions defaultViewport, TaskQueue screenshotTaskQueue)
: base(targetInfo, session, context, targetManager, sessionFactory)
{
_ignoreHTTPSErrors = ignoreHTTPSErrors;
Expand Down Expand Up @@ -71,7 +74,8 @@ internal override void Initialize()
CheckIfInitialized();
}

protected override void CheckIfInitialized()
/// <inheritdoc/>
protected internal override void CheckIfInitialized()
{
if (IsInitialized)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal virtual void Initialize()
/// <summary>
/// Check is the target is not initialized.
/// </summary>
protected virtual void CheckIfInitialized()
protected internal virtual void CheckIfInitialized()
{
IsInitialized = true;
InitializedTaskWrapper.TrySetResult(InitializationStatus.Success);
Expand Down
7 changes: 5 additions & 2 deletions lib/PuppeteerSharp/WorkerTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

namespace PuppeteerSharp
{
internal class WorkerTarget : Target
/// <summary>
/// Worker target.
/// </summary>
public class WorkerTarget : Target
{
private Task<Worker> _workerTask;

public WorkerTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> sessionFactory) : base(targetInfo, session, context, targetManager, sessionFactory)
internal WorkerTarget(TargetInfo targetInfo, CDPSession session, BrowserContext context, ITargetManager targetManager, Func<bool, Task<CDPSession>> sessionFactory) : base(targetInfo, session, context, targetManager, sessionFactory)
{
}

Expand Down

0 comments on commit 06ce60a

Please sign in to comment.