Skip to content

Commit f33e5fd

Browse files
authored
Teach waitForSelector to return null (#932)
1 parent 3c03f62 commit f33e5fd

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

lib/PuppeteerSharp.Tests/FrameTests/WaitForSelectorTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ public async Task HiddenShouldWaitForRemoval()
172172
Assert.True(divRemoved);
173173
}
174174

175+
[Fact]
176+
public async Task ShouldReturnNullIfWaitingToHideNonExistingElement()
177+
{
178+
var handle = await Page.WaitForSelectorAsync("non-existing", new WaitForSelectorOptions { Hidden = true });
179+
Assert.Null(handle);
180+
}
181+
175182
[Fact]
176183
public async Task ShouldRespectTimeout()
177184
{
@@ -209,5 +216,13 @@ public async Task ShouldReturnTheElementHandle()
209216
await Page.SetContentAsync("<div class='zombo'>anything</div>");
210217
Assert.Equal("anything", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForSelector));
211218
}
219+
220+
[Fact]
221+
public async Task ShouldHaveCorrectStackTraceForTimeout()
222+
{
223+
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(async ()
224+
=> await Page.WaitForSelectorAsync(".zombo", new WaitForSelectorOptions { Timeout = 10 }));
225+
Assert.Contains("WaitForSelectorTests", exception.StackTrace);
226+
}
212227
}
213228
}

lib/PuppeteerSharp/DOMWorld.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,13 @@ function hasVisibleBoundingBox() {
407407
options.Visible,
408408
options.Hidden
409409
}).Task.ConfigureAwait(false);
410-
return handle as ElementHandle;
410+
411+
if (!(handle is ElementHandle elementHandle))
412+
{
413+
await handle?.DisposeAsync();
414+
return null;
415+
}
416+
return elementHandle;
411417
}
412418
}
413419
}

lib/PuppeteerSharp/Frame.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ public Task<Response> GoToAsync(string url, int? timeout = null, WaitUntilNaviga
241241
/// </summary>
242242
/// <param name="selector">A selector of an element to wait for</param>
243243
/// <param name="options">Optional waiting parameters</param>
244-
/// <returns>A task that resolves when element specified by selector string is added to DOM</returns>
244+
/// <returns>A task that resolves when element specified by selector string is added to DOM.
245+
/// Resolves to `null` if waiting for `hidden: true` and selector is not found in DOM.</returns>
245246
/// <seealso cref="WaitForXPathAsync(string, WaitForSelectorOptions)"/>
246247
/// <seealso cref="Page.WaitForSelectorAsync(string, WaitForSelectorOptions)"/>
247248
/// <exception cref="WaitTaskTimeoutException">If timeout occurred.</exception>
@@ -253,7 +254,8 @@ public Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelector
253254
/// </summary>
254255
/// <param name="xpath">A xpath selector of an element to wait for</param>
255256
/// <param name="options">Optional waiting parameters</param>
256-
/// <returns>A task that resolves when element specified by selector string is added to DOM</returns>
257+
/// <returns>A task which resolves when element specified by xpath string is added to DOM.
258+
/// Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM.</returns>
257259
/// <example>
258260
/// <code>
259261
/// <![CDATA[

lib/PuppeteerSharp/Page.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,8 @@ public Task<JSHandle> WaitForExpressionAsync(string script, WaitForFunctionOptio
13101310
/// </summary>
13111311
/// <param name="selector">A selector of an element to wait for</param>
13121312
/// <param name="options">Optional waiting parameters</param>
1313-
/// <returns>A task that resolves when element specified by selector string is added to DOM</returns>
1313+
/// <returns>A task that resolves when element specified by selector string is added to DOM.
1314+
/// Resolves to `null` if waiting for `hidden: true` and selector is not found in DOM.</returns>
13141315
/// <seealso cref="WaitForXPathAsync(string, WaitForSelectorOptions)"/>
13151316
/// <seealso cref="Frame.WaitForSelectorAsync(string, WaitForSelectorOptions)"/>
13161317
public Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelectorOptions options = null)
@@ -1321,7 +1322,8 @@ public Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelector
13211322
/// </summary>
13221323
/// <param name="xpath">A xpath selector of an element to wait for</param>
13231324
/// <param name="options">Optional waiting parameters</param>
1324-
/// <returns>A task that resolves when element specified by selector string is added to DOM</returns>
1325+
/// <returns>A task which resolves when element specified by xpath string is added to DOM.
1326+
/// Resolves to `null` if waiting for `hidden: true` and xpath is not found in DOM.</returns>
13251327
/// <example>
13261328
/// <code>
13271329
/// <![CDATA[

0 commit comments

Comments
 (0)