Skip to content

Commit

Permalink
progress on puppeteer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dummy committed Feb 2, 2019
1 parent 7f93c35 commit 953496f
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 204 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PuppeteerSharp;

namespace FluffySpoon.Automation.Web.Selenium
{
public static class PuppeteerRegistrationExtensions
{
public static void AddSeleniumWebAutomationFrameworkInstance(this ServiceCollection services, Func<IWebDriver> driverConstructor)
public static void AddPuppeteerWebAutomationFrameworkInstance(this ServiceCollection services, Func<Task<Browser>> driverConstructor)
{
RegistrationExtensions.AddWebAutomationFrameworkInstance(provider =>
new SeleniumWebAutomationFrameworkInstance(
provider.GetRequiredService<IDomSelectorStrategy>(),
driverConstructor()));
new PuppeteerWebAutomationFrameworkInstance(
driverConstructor));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,101 +17,140 @@ class PuppeteerWebAutomationFrameworkInstance : IWebAutomationFrameworkInstance
private Browser _browser;
private Page _page;

private readonly Func<Task<Browser>> _driverConstructor;
private readonly IDomTunnel _domTunnel;

public PuppeteerWebAutomationFrameworkInstance(
Func<Task<Browser>> driverConstructor,
IDomTunnel domTunnel)
{
_driverConstructor = driverConstructor;
_domTunnel = domTunnel;
}

public string UserAgentName => GetType().Name;
private string GetSelectorForDomElements(IReadOnlyList<IDomElement> domElements)

private async Task<ElementHandle[]> GetElementHandlesFromDomElementsAsync(IReadOnlyList<IDomElement> domElements)
{
var selector = domElements
.Select(x => x.CssSelector)
.Aggregate((a, b) => $"{a}, {b}");

return selector;
return await _page.QuerySelectorAllAsync(selector);
}

public Task ClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
public async Task ClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
{

return _page.ClickAsync(
GetSelectorForDomElements(elements),
new ClickOptions() {

});
foreach (var element in elements)
{
await _page.Mouse.ClickAsync(
element.ClientLeft + offsetX,
element.ClientTop + offsetY);
}
}

public void Dispose()
{
throw new NotImplementedException();
_page.Dispose();
_browser.Dispose();
}

public Task DoubleClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
public async Task DoubleClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
{
throw new NotImplementedException();
foreach (var element in elements)
{
await _page.Mouse.ClickAsync(
element.ClientLeft + offsetX,
element.ClientTop + offsetY,
new ClickOptions() {
ClickCount = 2
});
}
}

public Task DragDropAsync(IDomElement from, int fromOffsetX, int fromOffsetY, IDomElement to, int toOffsetX, int toOffsetY)
public async Task DragDropAsync(IDomElement from, int fromOffsetX, int fromOffsetY, IDomElement to, int toOffsetX, int toOffsetY)
{
throw new NotImplementedException();
}
await _page.Mouse.MoveAsync(
from.ClientLeft + fromOffsetX,
from.ClientTop + fromOffsetY);
await _page.Mouse.DownAsync();

public Task EnterTextInAsync(IReadOnlyList<IDomElement> elements, string text)
{
throw new NotImplementedException();
await _page.Mouse.MoveAsync(
to.ClientLeft + toOffsetX,
to.ClientTop + toOffsetY);
await _page.Mouse.UpAsync();
}

public Task<IReadOnlyList<IDomElement>> EvaluateJavaScriptAsDomElementsAsync(int methodChainOffset, string code)
public async Task EnterTextInAsync(IReadOnlyList<IDomElement> elements, string text)
{
throw new NotImplementedException();
var handles = await GetElementHandlesFromDomElementsAsync(elements);
foreach(var handle in handles)
await handle.TypeAsync(text);
}

public Task<string> EvaluateJavaScriptAsync(string code)
public async Task<string> EvaluateJavaScriptAsync(string code)
{
throw new NotImplementedException();
var blob = await _page.EvaluateExpressionAsync(code);
return blob.ToString();
}

public Task<IReadOnlyList<IDomElement>> FindDomElementsByCssSelectorsAsync(int methodChainOffset, string[] selectors)
public Task<IReadOnlyList<IDomElement>> FindDomElementsBySelectorAsync(int methodChainOffset, string selector)
{
throw new NotImplementedException();
return _domTunnel.GetDomElementsFromSelector(this,
methodChainOffset,
selector);
}

public Task<IReadOnlyList<IDomElement>> FindDomElementsBySelectorAsync(int methodChainOffset, string selector)
public async Task FocusAsync(IDomElement domElement)
{
throw new NotImplementedException();
var handle = await GetElementHandleFromDomElementAsync(domElement);
await handle.FocusAsync();
}

public Task FocusAsync(IDomElement domElement, int offsetX, int offsetY)
private async Task<ElementHandle> GetElementHandleFromDomElementAsync(IDomElement domElement)
{
throw new NotImplementedException();
return await _page.QuerySelectorAsync(domElement.CssSelector);
}

public Task HoverAsync(IDomElement domElement, int offsetX, int offsetY)
public async Task HoverAsync(IDomElement domElement, int offsetX, int offsetY)
{
throw new NotImplementedException();
var handle = await GetElementHandleFromDomElementAsync(domElement);
await _page.Mouse.MoveAsync(
domElement.ClientLeft + offsetX,
domElement.ClientTop + offsetY);
}

public async Task InitializeAsync()
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

_browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});
_browser = await _driverConstructor();
_page = await _browser.NewPageAsync();
}

public Task OpenAsync(string uri)
{
throw new NotImplementedException();
return _page.GoToAsync(uri);
}

public Task RightClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
public async Task RightClickAsync(IReadOnlyList<IDomElement> elements, int offsetX, int offsetY)
{
throw new NotImplementedException();
foreach (var element in elements)
{
await _page.Mouse.ClickAsync(
element.ClientLeft + offsetX,
element.ClientTop + offsetY,
new ClickOptions() {
Button = MouseButton.Right
});
}
}

public Task SelectByIndicesAsync(IReadOnlyList<IDomElement> elements, int[] byIndices)
public async Task SelectByIndicesAsync(IReadOnlyList<IDomElement> elements, int[] byIndices)
{
throw new NotImplementedException();
foreach (var element in elements)
{
var handle = await _page.QuerySelectorAsync(element.CssSelector);
await _page.SelectAsync(handle)
}
}

public Task SelectByTextsAsync(IReadOnlyList<IDomElement> elements, string[] byTexts)
Expand All @@ -128,5 +167,10 @@ public Task<SKBitmap> TakeScreenshotAsync()
{
throw new NotImplementedException();
}

public Task<IReadOnlyList<IDomElement>> FindDomElementsByCssSelectorsAsync(int methodChainOffset, string[] selectors)
{
throw new NotImplementedException();
}
}
}
12 changes: 6 additions & 6 deletions src/FluffySpoon.Automation.Web.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ static async Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.UseJQueryDomSelector();
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriver);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetChromeDriver);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriver);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriverAsync);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetChromeDriverAsync);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync);

var serviceProvider = serviceCollection.BuildServiceProvider();

Expand Down Expand Up @@ -53,7 +53,7 @@ await automationEngine
}
}

private static EdgeDriver GetEdgeDriver()
private static async Task<IWebDriver> GetEdgeDriverAsync()
{
var options = new EdgeOptions() {
AcceptInsecureCertificates = true,
Expand All @@ -65,7 +65,7 @@ private static EdgeDriver GetEdgeDriver()
return driver;
}

private static FirefoxDriver GetFirefoxDriver()
private static async Task<IWebDriver> GetFirefoxDriverAsync()
{
var options = new FirefoxOptions() {
PageLoadStrategy = PageLoadStrategy.Eager,
Expand All @@ -77,7 +77,7 @@ private static FirefoxDriver GetFirefoxDriver()
return driver;
}

private static ChromeDriver GetChromeDriver()
private static async Task<IWebDriver> GetChromeDriverAsync()
{
var service = ChromeDriverService.CreateDefaultService(Environment.CurrentDirectory);
service.EnableVerboseLogging = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using FluffySpoon.Automation.Web.Dom;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium;
Expand All @@ -7,12 +8,12 @@ namespace FluffySpoon.Automation.Web.Selenium
{
public static class SeleniumRegistrationExtensions
{
public static void AddSeleniumWebAutomationFrameworkInstance(this ServiceCollection services, Func<IWebDriver> driverConstructor)
public static void AddSeleniumWebAutomationFrameworkInstance(this ServiceCollection services, Func<Task<IWebDriver>> driverConstructor)
{
RegistrationExtensions.AddWebAutomationFrameworkInstance(provider =>
new SeleniumWebAutomationFrameworkInstance(
provider.GetRequiredService<IDomSelectorStrategy>(),
driverConstructor()));
driverConstructor,
provider.GetRequiredService<IDomTunnel>()));
}
}
}
Loading

0 comments on commit 953496f

Please sign in to comment.