From 64b784e090726f924836f5bf247301ee0a920f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Thu, 7 Mar 2019 08:07:43 -0300 Subject: [PATCH] Input and target tests updates (#989) --- .../InputTests/InputTests.cs | 13 +++++++++---- .../TargetTests/TargetTests.cs | 18 +++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs b/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs index a494f9453..59196d9c1 100644 --- a/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs +++ b/lib/PuppeteerSharp.Tests/InputTests/InputTests.cs @@ -327,7 +327,7 @@ await Page.EvaluateExpressionAsync(@"{ if (event.key === 'l') event.preventDefault(); if (event.key === 'o') - Promise.resolve().then(() => event.preventDefault()); + event.preventDefault(); }, false); }"); await Page.Keyboard.TypeAsync("Hello World!"); @@ -359,8 +359,8 @@ public async Task ShouldResizeTheTextarea() await mouse.MoveAsync(dimensions.X + dimensions.Width + 100, dimensions.Y + dimensions.Height + 100); await mouse.UpAsync(); var newDimensions = await Page.EvaluateFunctionAsync(Dimensions); - Assert.Equal(dimensions.Width + 104, newDimensions.Width); - Assert.Equal(dimensions.Height + 104, newDimensions.Height); + Assert.Equal(Math.Round(dimensions.Width + 104, MidpointRounding.AwayFromZero), newDimensions.Width); + Assert.Equal(Math.Round(dimensions.Height + 104, MidpointRounding.AwayFromZero), newDimensions.Height); } [Fact] @@ -419,13 +419,18 @@ public async Task ShouldSelectTheTextWithMouse() await Page.FocusAsync("textarea"); const string text = "This is the text that we are going to try to select. Let's see how it goes."; await Page.Keyboard.TypeAsync(text); + // Firefox needs an extra frame here after typing or it will fail to set the scrollTop + await Page.EvaluateExpressionAsync("new Promise(requestAnimationFrame)"); await Page.EvaluateExpressionAsync("document.querySelector('textarea').scrollTop = 0"); var dimensions = await Page.EvaluateFunctionAsync(Dimensions); await Page.Mouse.MoveAsync(dimensions.X + 2, dimensions.Y + 2); await Page.Mouse.DownAsync(); await Page.Mouse.MoveAsync(100, 100); await Page.Mouse.UpAsync(); - Assert.Equal(text, await Page.EvaluateExpressionAsync("window.getSelection().toString()")); + Assert.Equal(text, await Page.EvaluateFunctionAsync(@"() => { + const textarea = document.querySelector('textarea'); + return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd); + }")); } [Fact] diff --git a/lib/PuppeteerSharp.Tests/TargetTests/TargetTests.cs b/lib/PuppeteerSharp.Tests/TargetTests/TargetTests.cs index 1f4f607d9..1b93f0f10 100644 --- a/lib/PuppeteerSharp.Tests/TargetTests/TargetTests.cs +++ b/lib/PuppeteerSharp.Tests/TargetTests/TargetTests.cs @@ -55,15 +55,15 @@ public async Task ShouldBeAbleToUseTheDefaultPageInTheBrowser() [Fact] public async Task ShouldReportWhenANewPageIsCreatedAndClosed() { - var otherPageTaskCompletion = new TaskCompletionSource(); - async void TargetCreatedEventHandler(object sender, TargetChangedArgs e) - { - otherPageTaskCompletion.SetResult(await e.Target.PageAsync()); - Context.TargetCreated -= TargetCreatedEventHandler; - } - Context.TargetCreated += TargetCreatedEventHandler; - await Page.EvaluateFunctionHandleAsync("url => window.open(url)", TestConstants.CrossProcessUrl); - var otherPage = await otherPageTaskCompletion.Task; + var otherPageTask = Context.WaitForTargetAsync(t => t.Url == TestConstants.CrossProcessUrl + "/empty.html") + .ContinueWith(t => t.Result.PageAsync()); + + await Task.WhenAll( + otherPageTask, + Page.EvaluateFunctionHandleAsync("url => window.open(url)", TestConstants.CrossProcessUrl + "/empty.html") + ); + + var otherPage = await otherPageTask.Result; Assert.Contains(TestConstants.CrossProcessUrl, otherPage.Url); Assert.Equal("Hello world", await otherPage.EvaluateExpressionAsync("['Hello', 'world'].join(' ')"));