Skip to content

Commit

Permalink
Input and target tests updates (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Mar 7, 2019
1 parent bddf0fc commit 64b784e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
13 changes: 9 additions & 4 deletions lib/PuppeteerSharp.Tests/InputTests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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>(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]
Expand Down Expand Up @@ -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>(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<string>("window.getSelection().toString()"));
Assert.Equal(text, await Page.EvaluateFunctionAsync<string>(@"() => {
const textarea = document.querySelector('textarea');
return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
}"));
}

[Fact]
Expand Down
18 changes: 9 additions & 9 deletions lib/PuppeteerSharp.Tests/TargetTests/TargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public async Task ShouldBeAbleToUseTheDefaultPageInTheBrowser()
[Fact]
public async Task ShouldReportWhenANewPageIsCreatedAndClosed()
{
var otherPageTaskCompletion = new TaskCompletionSource<Page>();
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<string>("['Hello', 'world'].join(' ')"));
Expand Down

0 comments on commit 64b784e

Please sign in to comment.