Skip to content

Commit

Permalink
merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dummy committed Mar 20, 2019
2 parents 9cfb2a8 + cb7253b commit 7da23a1
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class PuppeteerWebAutomationFrameworkInstance : IWebAutomationFrameworkInstance
private readonly Func<Task<Browser>> _driverConstructor;
private readonly IJavaScriptTunnel _domTunnel;

private bool _isDisposed;

public PuppeteerWebAutomationFrameworkInstance(
Func<Task<Browser>> driverConstructor,
IJavaScriptTunnel domTunnel)
Expand All @@ -44,18 +46,20 @@ private async Task<ElementHandle[]> GetElementHandlesFromDomElementsAsync(IReadO
return await _page.QuerySelectorAllAsync(selector);
}

public async Task ClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
public async Task ClickAsync(IReadOnlyList<IDomElement> elements, int? offsetX, int? offsetY)
{
foreach (var element in elements)
{
await _page.Mouse.ClickAsync(
(int)Math.Ceiling(element.BoundingClientRectangle.Left) + offsetX,
(int)Math.Ceiling(element.BoundingClientRectangle.Top) + offsetY);
(element.BoundingClientRectangle.Left + offsetX) ?? element.BoundingClientRectangle.Center.X,
(element.BoundingClientRectangle.Top + offsetY) ?? element.BoundingClientRectangle.Center.Y);
}
}

public Task DisposeAsync()
{
_isDisposed = true;

if (_page != null)
{
_page.Request -= PageRequest;
Expand All @@ -70,13 +74,13 @@ public Task DisposeAsync()
return Task.CompletedTask;
}

public async Task DoubleClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
public async Task DoubleClickAsync(IReadOnlyList<IDomElement> elements, int? offsetX, int? offsetY)
{
foreach (var element in elements)
{
await _page.Mouse.ClickAsync(
(int)Math.Ceiling(element.BoundingClientRectangle.Left) + offsetX,
(int)Math.Ceiling(element.BoundingClientRectangle.Top) + offsetY,
(element.BoundingClientRectangle.Left + offsetX) ?? element.BoundingClientRectangle.Center.X,
(element.BoundingClientRectangle.Top + offsetY) ?? element.BoundingClientRectangle.Center.Y,
new ClickOptions()
{
ClickCount = 2
Expand All @@ -86,20 +90,20 @@ await _page.Mouse.ClickAsync(

public async Task DragDropAsync(
IDomElement from,
int fromOffsetX,
int fromOffsetY,
int? fromOffsetX,
int? fromOffsetY,
IDomElement to,
int toOffsetX,
int toOffsetY)
int? toOffsetX,
int? toOffsetY)
{
var javascriptScope = _domTunnel.DeclareScope(this);
try
{
var dataTransferObjectVariableName = await javascriptScope.CreateNewVariableAsync("new DataTransfer()");

await _page.Mouse.MoveAsync(
(int)Math.Ceiling(from.BoundingClientRectangle.Left) + fromOffsetX,
(int)Math.Ceiling(from.BoundingClientRectangle.Top) + fromOffsetY);
(from.BoundingClientRectangle.Left + fromOffsetX) ?? from.BoundingClientRectangle.Center.X,
(from.BoundingClientRectangle.Top + fromOffsetY) ?? from.BoundingClientRectangle.Center.Y);
await _page.Mouse.DownAsync();

await _domTunnel.DispatchDomElementDragEventAsync(
Expand All @@ -115,8 +119,8 @@ await _domTunnel.DispatchDomElementDragEventAsync(
dataTransferObjectVariableName);

await _page.Mouse.MoveAsync(
(int)Math.Ceiling(to.BoundingClientRectangle.Left) + toOffsetX,
(int)Math.Ceiling(to.BoundingClientRectangle.Top) + toOffsetY);
(to.BoundingClientRectangle.Left + toOffsetX) ?? to.BoundingClientRectangle.Center.X,
(to.BoundingClientRectangle.Top + toOffsetY) ?? to.BoundingClientRectangle.Center.Y);

await _domTunnel.DispatchDomElementDragEventAsync(
this,
Expand Down Expand Up @@ -173,13 +177,30 @@ public async Task EnterTextInAsync(IReadOnlyList<IDomElement> elements, string t

public async Task<string> EvaluateJavaScriptExpressionAsync(string code)
{
var blob = await _page.EvaluateExpressionAsync(code);
return blob?.ToString();
while (true)
{
try
{
while (IsNavigating)
await Task.Delay(100);

var blob = await _page.EvaluateExpressionAsync(code);
return blob?.ToString();
}
catch (PuppeteerException)
{
if(!IsNavigating)
throw;
}
}
}

public Task<IReadOnlyList<IDomElement>> FindDomElementsBySelectorAsync(int methodChainOffset, string selector)
public async Task<IReadOnlyList<IDomElement>> FindDomElementsBySelectorAsync(int methodChainOffset, string selector)
{
return _domTunnel.GetDomElementsFromSelector(this,
if (_isDisposed)
return new List<IDomElement>();

return await _domTunnel.GetDomElementsFromSelector(this,
methodChainOffset,
selector);
}
Expand All @@ -204,11 +225,11 @@ private async Task<ElementHandle> GetElementHandleFromDomElementAsync(IDomElemen
return await _page.QuerySelectorAsync(domElement.CssSelector);
}

public async Task HoverAsync(IDomElement domElement, int offsetX, int offsetY)
public async Task HoverAsync(IDomElement domElement, int? offsetX, int? offsetY)
{
await _page.Mouse.MoveAsync(
(int)Math.Ceiling(domElement.BoundingClientRectangle.Left) + offsetX,
(int)Math.Ceiling(domElement.BoundingClientRectangle.Top) + offsetY);
(domElement.BoundingClientRectangle.Left + offsetX) ?? domElement.BoundingClientRectangle.Center.X,
(domElement.BoundingClientRectangle.Top + offsetY) ?? domElement.BoundingClientRectangle.Center.Y);
}

public async Task InitializeAsync()
Expand Down Expand Up @@ -246,13 +267,13 @@ public async Task OpenAsync(string uri)
await _page.GoToAsync(uri);
}

public async Task RightClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
public async Task RightClickAsync(IReadOnlyList<IDomElement> elements, int? offsetX, int? offsetY)
{
foreach (var element in elements)
{
await _page.Mouse.ClickAsync(
(int)Math.Ceiling(element.BoundingClientRectangle.Left) + offsetX,
(int)Math.Ceiling(element.BoundingClientRectangle.Top) + offsetY,
(element.BoundingClientRectangle.Left + offsetX) ?? element.BoundingClientRectangle.Center.X,
(element.BoundingClientRectangle.Top + offsetY) ?? element.BoundingClientRectangle.Center.Y,
new ClickOptions()
{
Button = MouseButton.Right
Expand Down
7 changes: 3 additions & 4 deletions src/FluffySpoon.Automation.Web.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static async Task Main(string[] args)
serviceCollection.AddPuppeteerWebAutomationFrameworkInstance(GetPuppeteerDriverAsync);

serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriverAsync);
//serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetOperaDriverAsync);

var serviceProvider = serviceCollection.BuildServiceProvider();
var automationEngine = serviceProvider.GetRequiredService<IWebAutomationEngine>();
Expand All @@ -42,10 +41,10 @@ static async Task Main(string[] args)
await automationEngine
.Enter("this is a very long test that works").In("input[type=text]:visible")
.Wait(until =>
until.Exists("input[type=submit]:visible"));
until.Exists("input[name=btnK][type=submit]:visible"));

var elements = await automationEngine
.Click.On("input[type=submit]:visible:first")
.Click.On("input[name=btnK][type=submit]:visible:first")
.Wait(until =>
until.Exists("#rso .g:visible"))
.Expect
Expand Down Expand Up @@ -79,7 +78,7 @@ private static async Task<Browser> GetPuppeteerDriverAsync()
DefaultViewport = new ViewPortOptions()
{
Width = 1100,
Height = 500
Height = 700
}
});
}
Expand Down
Loading

0 comments on commit 7da23a1

Please sign in to comment.