Skip to content

Commit f24c854

Browse files
kblokamaitland
andauthored
Waittask refactor (#2190)
* Waittask refactor * remove typescript code * Fix some javascript calls * Some improvements * cr * Fix some tests * passing tests * WaitTask Refactor - Fix Evaluation failed: TypeError: fun is not a function (#2192) * Fix Evaluation failed: TypeError: fun is not a function - The whitespace is causing the new Function() call to fail. * Alternate fix * Fix ShouldRespectTimeout test * Fix failing test --------- Co-authored-by: Alex Maitland <[email protected]>
1 parent 28f0354 commit f24c854

13 files changed

+580
-252
lines changed

lib/PuppeteerSharp.Tests/TestConstants.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ public static class TestConstants
4040
" http://localhost:<PORT>/frames/frame.html (aframe)"
4141
};
4242

43-
public static LaunchOptions DefaultBrowserOptions() => new LaunchOptions
43+
public static LaunchOptions DefaultBrowserOptions() => new()
4444
{
4545
SlowMo = Convert.ToInt32(Environment.GetEnvironmentVariable("SLOW_MO")),
46-
Headless = Convert.ToBoolean(Environment.GetEnvironmentVariable("HEADLESS") ?? "true"),
46+
Headless = Convert.ToBoolean(
47+
Environment.GetEnvironmentVariable("HEADLESS") ??
48+
(System.Diagnostics.Debugger.IsAttached ? "false" : "true")),
4749
Product = IsChrome ? Product.Chrome : Product.Firefox,
4850
EnqueueAsyncMessages = Convert.ToBoolean(Environment.GetEnvironmentVariable("ENQUEUE_ASYNC_MESSAGES") ?? "false"),
4951
Timeout = 0,
@@ -55,7 +57,7 @@ public static class TestConstants
5557
#endif
5658
};
5759

58-
public static LaunchOptions BrowserWithExtensionOptions() => new LaunchOptions
60+
public static LaunchOptions BrowserWithExtensionOptions() => new()
5961
{
6062
Headless = false,
6163
Args = new[]

lib/PuppeteerSharp.Tests/WaitTaskTests/FrameWaitForFunctionTests.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,29 @@ await Page.WaitForFunctionAsync(@"() =>
3131
[PuppeteerFact]
3232
public async Task ShouldPollOnInterval()
3333
{
34-
var success = false;
3534
var startTime = DateTime.UtcNow;
3635
var polling = 100;
37-
var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling })
38-
.ContinueWith(_ => success = true);
39-
await Page.EvaluateExpressionAsync("window.__FOO = 'hit'");
40-
Assert.False(success);
36+
var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling });
37+
// Wait for function will release the execution faster than in node.
38+
// We add some CDP action to wait for the task to start the polling
4139
await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
40+
await Page.EvaluateFunctionAsync("() => setTimeout(window.__FOO = 'hit', 50)");
4241
await watchdog;
42+
4343
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > polling / 2);
4444
}
4545

4646
[PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on interval async")]
4747
[PuppeteerFact]
4848
public async Task ShouldPollOnIntervalAsync()
4949
{
50-
var success = false;
5150
var startTime = DateTime.UtcNow;
52-
var polling = 100;
53-
var watchdog = Page.WaitForFunctionAsync("async () => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling })
54-
.ContinueWith(_ => success = true);
55-
await Page.EvaluateFunctionAsync("async () => window.__FOO = 'hit'");
56-
Assert.False(success);
51+
var polling = 1000;
52+
var watchdog = Page.WaitForFunctionAsync("async () => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling });
53+
// Wait for function will release the execution faster than in node.
54+
// We add some CDP action to wait for the task to start the polling
5755
await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
56+
await Page.EvaluateFunctionAsync("async () => setTimeout(window.__FOO = 'hit', 50)");
5857
await watchdog;
5958
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > polling / 2);
6059
}
@@ -162,7 +161,7 @@ public async Task ShouldRespectTimeout()
162161
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
163162
=> Page.WaitForExpressionAsync("false", new WaitForFunctionOptions { Timeout = 10 }));
164163

165-
Assert.Contains("waiting for function failed: timeout", exception.Message);
164+
Assert.Contains("Waiting failed: 10ms exceeded", exception.Message);
166165
}
167166

168167
[PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should respect default timeout")]
@@ -173,7 +172,7 @@ public async Task ShouldRespectDefaultTimeout()
173172
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
174173
=> Page.WaitForExpressionAsync("false"));
175174

176-
Assert.Contains("waiting for function failed: timeout", exception.Message);
175+
Assert.Contains("Waiting failed: 1ms exceeded", exception.Message);
177176
}
178177

179178
[PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should disable timeout when its set to 0")]

lib/PuppeteerSharp.Tests/WaitTaskTests/FrameWaitForSelectorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public async Task ShouldThrowWhenFrameIsDetached()
105105
var frame = Page.FirstChildFrame();
106106
var waitTask = frame.WaitForSelectorAsync(".box");
107107
await FrameUtils.DetachFrameAsync(Page, "frame1");
108-
var waitException = await Assert.ThrowsAsync<Exception>(() => waitTask);
108+
var waitException = await Assert.ThrowsAsync<WaitTaskTimeoutException>(() => waitTask);
109109

110110
Assert.NotNull(waitException);
111111
Assert.Contains("waitForFunction failed: frame got detached.", waitException.Message);
@@ -206,7 +206,7 @@ public async Task ShouldRespectTimeout()
206206
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(async ()
207207
=> await Page.WaitForSelectorAsync("div", new WaitForSelectorOptions { Timeout = 10 }));
208208

209-
Assert.Contains("waiting for selector 'div' failed: timeout", exception.Message);
209+
Assert.Contains("Waiting for selector `div` failed: Waiting failed: 10ms exceeded", exception.Message);
210210
}
211211

212212
[PuppeteerTest("waittask.spec.ts", "Frame.waitForSelector", "should have an error message specifically for awaiting an element to be hidden")]
@@ -217,7 +217,7 @@ public async Task ShouldHaveAnErrorMessageSpecificallyForAwaitingAnElementToBeHi
217217
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(async ()
218218
=> await Page.WaitForSelectorAsync("div", new WaitForSelectorOptions { Hidden = true, Timeout = 10 }));
219219

220-
Assert.Contains("waiting for selector 'div' to be hidden failed: timeout", exception.Message);
220+
Assert.Contains("Waiting for selector `div` failed: Waiting failed: 10ms exceeded", exception.Message);
221221
}
222222

223223
[PuppeteerTest("waittask.spec.ts", "Frame.waitForSelector", "should respond to node attribute mutation")]

lib/PuppeteerSharp.Tests/WaitTaskTests/FrameWaitForXPathTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ public async Task ShouldAllowYouToSelectAnElementWithSingleSlash()
100100
[PuppeteerFact]
101101
public async Task ShouldRespectTimeout()
102102
{
103+
const int timeout = 10;
104+
103105
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
104-
=> Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Timeout = 10 }));
106+
=> Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Timeout = timeout }));
105107

106-
Assert.Contains("waiting for XPath '//div' failed: timeout", exception.Message);
108+
Assert.Contains($"Waiting failed: {timeout}ms exceeded", exception.Message);
107109
}
108110
}
109111
}

lib/PuppeteerSharp.Tests/WaitTaskTests/PageWaitForTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task ShouldWaitForAnXpath()
4747
public async Task ShouldNotAllowYouToSelectAnElementWithSingleSlashXpath()
4848
{
4949
await Page.SetContentAsync("<div>some text</div>");
50-
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() =>
50+
var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(() =>
5151
Page.WaitForSelectorAsync("/html/body/div"));
5252
Assert.NotNull(exception);
5353
}

lib/PuppeteerSharp/AriaQueryHandlerFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async Task<IElementHandle> WaitFor(IElementHandle root, string selector, WaitFor
5252

5353
return await frame.PuppeteerWorld.WaitForSelectorInPageAsync(
5454
@"(_, selector) => globalThis.ariaQuerySelector(selector)",
55+
element,
5556
selector,
5657
options,
5758
new[] { binding }).ConfigureAwait(false);

lib/PuppeteerSharp/CustomQueriesManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ private static InternalQueryHandler MakeQueryHandler(CustomQueryHandler handler)
154154
internalHandler.WaitFor = async (IElementHandle root, string selector, WaitForSelectorOptions options) =>
155155
{
156156
var frame = (root as ElementHandle).Frame;
157-
var element = await frame.PuppeteerWorld.AdoptHandleAsync(root).ConfigureAwait(false);
157+
var element = await frame.PuppeteerWorld.AdoptHandleAsync(root).ConfigureAwait(false) as IElementHandle;
158158

159-
return await frame.PuppeteerWorld.WaitForSelectorInPageAsync(handler.QueryOne, selector, options).ConfigureAwait(false);
159+
return await frame.PuppeteerWorld.WaitForSelectorInPageAsync(handler.QueryOne, element, selector, options).ConfigureAwait(false);
160160
};
161161
}
162162

0 commit comments

Comments
 (0)