Skip to content

Commit 5214cef

Browse files
kblokMeir017
authored andcommitted
Fix null-type bugs (#662)
This is more for consistency with Puppeteer.
1 parent 7b5cb82 commit 5214cef

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/PuppeteerSharp/CDPSession.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ public async Task<T> SendAsync<T>(string method, dynamic args = null)
120120
/// <param name="args">The method args</param>
121121
/// <param name="rawContent"></param>
122122
/// <returns>The task.</returns>
123+
/// <exception cref="T:PuppeteerSharp.PuppeteerException"></exception>
123124
public async Task<dynamic> SendAsync(string method, bool rawContent, dynamic args = null)
124125
{
125126
if (Connection == null)
126127
{
127-
throw new Exception($"Protocol error ({method}): Session closed. Most likely the {TargetType} has been closed.");
128+
throw new PuppeteerException($"Protocol error ({method}): Session closed. Most likely the {TargetType} has been closed.");
128129
}
129130
var id = ++_lastId;
130131
var message = JsonConvert.SerializeObject(new Dictionary<string, object>
@@ -167,8 +168,15 @@ public async Task<dynamic> SendAsync(string method, bool rawContent, dynamic arg
167168
/// Detaches session from target. Once detached, session won't emit any events and can't be used to send messages.
168169
/// </summary>
169170
/// <returns></returns>
171+
/// <exception cref="T:PuppeteerSharp.PuppeteerException"></exception>
170172
public Task DetachAsync()
171-
=> Connection.SendAsync("Target.detachFromTarget", new { sessionId = SessionId });
173+
{
174+
if (Connection == null)
175+
{
176+
throw new PuppeteerException($"Session already detached.Most likely the { TargetType } has been closed.");
177+
}
178+
return Connection.SendAsync("Target.detachFromTarget", new { sessionId = SessionId });
179+
}
172180

173181
internal bool HasPendingCallbacks() => _callbacks.Count != 0;
174182

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public async Task<string> ScreenshotBase64Async(ScreenshotOptions options)
126126
}
127127

128128
var viewport = Page.Viewport;
129-
if (boundingBox.Width > viewport.Width || boundingBox.Height > viewport.Height)
129+
if (viewport != null && (boundingBox.Width > viewport.Width || boundingBox.Height > viewport.Height))
130130
{
131131
var newRawViewport = JObject.FromObject(viewport);
132132
newRawViewport.Merge(new ViewPortOptions

lib/PuppeteerSharp/Page.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,10 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
16741674

16751675
if (options != null && options.FullPage)
16761676
{
1677+
if (Viewport == null)
1678+
{
1679+
throw new PuppeteerException("FullPage screenshots do not work without first setting viewport.");
1680+
}
16771681
dynamic metrics = await Client.SendAsync("Page.getLayoutMetrics").ConfigureAwait(false);
16781682
var width = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.width.Value)));
16791683
var height = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(metrics.contentSize.height.Value)));

0 commit comments

Comments
 (0)