Skip to content

Commit

Permalink
stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dummy committed Feb 19, 2019
2 parents 09aa951 + 3ba71a4 commit 1358b6c
Show file tree
Hide file tree
Showing 58 changed files with 600 additions and 226 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# FluffySpoon.Automation
An abstraction that makes Selenium or Puppeteer testing fun, stable and fast.

<b>This is still not ready for production-use and is experimental! There are still open issues on it, and it is not 100% bug-free.</b>

## Selectors
A selector determines how to select elements. Right now, only jQuery selectors are supported.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace FluffySpoon.Automation.Web.JQuery
{
public static class JQueryRegistrationExtensions
{
public static void UseJQueryDomSelector(this ServiceCollection services)
public static void AddJQueryDomSelector(this IServiceCollection services)
{
services.UseFluffySpoonAutomationWeb();
services.AddFluffySpoonAutomationWeb();
services.AddSingleton<IDomSelectorStrategy, JQueryDomSelectorStrategy>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
using Microsoft.Extensions.DependencyInjection;
using PuppeteerSharp;

namespace FluffySpoon.Automation.Web.Selenium
namespace FluffySpoon.Automation.Web.Puppeteer
{
public static class PuppeteerRegistrationExtensions
{
public static void AddPuppeteerWebAutomationFrameworkInstance(this ServiceCollection services, Func<Task<Browser>> driverConstructor)
public static void AddPuppeteerWebAutomationFrameworkInstance(this IServiceCollection services, Func<Task<Browser>> driverConstructor)
{
RegistrationExtensions.AddWebAutomationFrameworkInstance(provider =>
services.AddFluffySpoonAutomationWeb();
services.AddTransient<IWebAutomationFrameworkInstance>(provider =>
new PuppeteerWebAutomationFrameworkInstance(
driverConstructor,
provider.GetRequiredService<IDomTunnel>()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using FluffySpoon.Automation.Web.Dom;
using Newtonsoft.Json;
using PuppeteerSharp;
using PuppeteerSharp.Input;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FluffySpoon.Automation.Web.Selenium
namespace FluffySpoon.Automation.Web.Puppeteer
{
class PuppeteerWebAutomationFrameworkInstance : IWebAutomationFrameworkInstance
{
Expand Down
1 change: 0 additions & 1 deletion src/FluffySpoon.Automation.Web.Sample/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>
43 changes: 27 additions & 16 deletions src/FluffySpoon.Automation.Web.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.Threading.Tasks;
using FluffySpoon.Automation.Web.JQuery;
using FluffySpoon.Automation.Web.Selenium;
using FluffySpoon.Automation.Web.Puppeteer;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using PuppeteerSharp;
using System.Diagnostics;

namespace FluffySpoon.Automation.Web.Sample
{
Expand All @@ -18,32 +20,34 @@ static async Task Main(string[] args)
try
{
var serviceCollection = new ServiceCollection();
serviceCollection.UseJQueryDomSelector();

//serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriverAsync);
//serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetChromeDriverAsync);
//serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync);
serviceCollection.AddJQueryDomSelector();

serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriverAsync);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetChromeDriverAsync);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync);

serviceCollection.AddPuppeteerWebAutomationFrameworkInstance(GetPuppeteerDriverAsync);

var serviceProvider = serviceCollection.BuildServiceProvider();

using (var automationEngine = serviceProvider.GetRequiredService<IWebAutomationEngine>())
{
await automationEngine.InitializeAsync();

await automationEngine
.Open("https://google.com");

await automationEngine
.Enter("this is a very long test that works").In("input[type=text]:visible")
.Wait(until =>
until.Exists("input[type=submit]:visible"));

var elements = await automationEngine
.Click.On("input[type=submit]:visible:first")
.Wait(until =>
until.Exists("input[type=submit][name=btnK]:visible"))
until.Exists("#rso .g:visible"))
.Expect
.Count(1).Of("input[type=submit][name=btnK]:visible");

foreach (var element in elements) {
await automationEngine.TakeScreenshot.Of(element).SaveAs("screenshot.jpg");
}
.Count(10).Of("#rso .g:visible");

Console.WriteLine("Test done!");
}
Expand All @@ -57,11 +61,15 @@ await automationEngine

private static async Task<Browser> GetPuppeteerDriverAsync()
{
foreach (var process in Process.GetProcessesByName("chrome"))
process.Kill();

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
return await Puppeteer.LaunchAsync(new LaunchOptions
return await PuppeteerSharp.Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false,
DefaultViewport = new ViewPortOptions() {
DefaultViewport = new ViewPortOptions()
{
Width = 1100,
Height = 500
}
Expand All @@ -70,7 +78,8 @@ private static async Task<Browser> GetPuppeteerDriverAsync()

private static async Task<IWebDriver> GetEdgeDriverAsync()
{
var options = new EdgeOptions() {
var options = new EdgeOptions()
{
AcceptInsecureCertificates = true,
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
PageLoadStrategy = PageLoadStrategy.Eager
Expand All @@ -82,7 +91,8 @@ private static async Task<IWebDriver> GetEdgeDriverAsync()

private static async Task<IWebDriver> GetFirefoxDriverAsync()
{
var options = new FirefoxOptions() {
var options = new FirefoxOptions()
{
PageLoadStrategy = PageLoadStrategy.Eager,
AcceptInsecureCertificates = true,
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept
Expand All @@ -99,7 +109,8 @@ private static async Task<IWebDriver> GetChromeDriverAsync()
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;

var options = new ChromeOptions() {
var options = new ChromeOptions()
{
Proxy = null,
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
AcceptInsecureCertificates = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace FluffySpoon.Automation.Web.Selenium
{
public static class SeleniumRegistrationExtensions
{
public static void AddSeleniumWebAutomationFrameworkInstance(this ServiceCollection services, Func<Task<IWebDriver>> driverConstructor)
public static void AddSeleniumWebAutomationFrameworkInstance(this IServiceCollection services, Func<Task<IWebDriver>> driverConstructor)
{
RegistrationExtensions.AddWebAutomationFrameworkInstance(provider =>
services.AddTransient<IWebAutomationFrameworkInstance>(provider =>
new SeleniumWebAutomationFrameworkInstance(
driverConstructor,
provider.GetRequiredService<IDomTunnel>()));
Expand Down
26 changes: 26 additions & 0 deletions src/FluffySpoon.Automation.Web.Tests/AutomationEngineFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using PuppeteerSharp;
using System.Diagnostics;
using System.Threading.Tasks;

namespace FluffySpoon.Automation.Web.Tests
{
class AutomationEngineFactory
{
public static async Task<Browser> GetPuppeteerDriverAsync()
{
foreach (var process in Process.GetProcessesByName("chrome"))
process.Kill();

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
return await PuppeteerSharp.Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false,
DefaultViewport = new ViewPortOptions()
{
Width = 1100,
Height = 500
}
});
}
}
}
15 changes: 15 additions & 0 deletions src/FluffySpoon.Automation.Web.Tests/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace FluffySpoon.Automation.Web.Tests
{
public static class Extensions
{
public static async Task OpenTest(this IWebAutomationEngine engine, string page)
{
await engine.Open(WebServerHelper.Url + "/" + page);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluffySpoon.Automation.Web.JQuery\FluffySpoon.Automation.Web.JQuery.csproj" />
<ProjectReference Include="..\FluffySpoon.Automation.Web.Puppeteer\FluffySpoon.Automation.Web.Puppeteer.csproj" />
<ProjectReference Include="..\FluffySpoon.Automation.Web.Selenium\FluffySpoon.Automation.Web.Selenium.csproj" />
<ProjectReference Include="..\FluffySpoon.Automation.Web\FluffySpoon.Automation.Web.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="wwwroot\engine\click.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="wwwroot\engine\find.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="wwwroot\selector\index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
80 changes: 80 additions & 0 deletions src/FluffySpoon.Automation.Web.Tests/SelectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using FluffySpoon.Automation.Web.JQuery;
using FluffySpoon.Automation.Web.Puppeteer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace FluffySpoon.Automation.Web.Tests
{
[TestClass]
public class SelectorTests
{
private class TestCase
{
public Action<IServiceCollection> SelectorConfiguration { get; }

public string OuterElementSelector { get; }

public string InnerElementSelector { get; }

public TestCase(
Action<IServiceCollection> selectorConfiguration,
string outerElementSelector,
string innerElementSelector)
{
SelectorConfiguration = selectorConfiguration;
OuterElementSelector = outerElementSelector;
InnerElementSelector = innerElementSelector;
}
}

[TestMethod]
public async Task SelectorTest()
{
var testCases = new List<TestCase>
{
new TestCase(
p => p.AddJQueryDomSelector(),
"#outer:first",
".inner:first")
};

using (var server = WebServerHelper.CreateWebServer())
{
foreach (var testCase in testCases)
{
var serviceCollection = new ServiceCollection();
testCase.SelectorConfiguration(serviceCollection);

serviceCollection.AddPuppeteerWebAutomationFrameworkInstance(AutomationEngineFactory.GetPuppeteerDriverAsync);

var serviceProvider = serviceCollection.BuildServiceProvider();
using (var automationEngine = serviceProvider.GetRequiredService<IWebAutomationEngine>())
{
await automationEngine.InitializeAsync();

await automationEngine.OpenTest("selector/index.html");

var outerElements = await automationEngine.Find(testCase.OuterElementSelector);
Assert.IsNotNull(outerElements);

var outerElement = outerElements.SingleOrDefault();
Assert.IsNotNull(outerElement);
Assert.AreEqual("hello world", outerElement.TextContent);

var innerElements = await automationEngine.Find(testCase.InnerElementSelector);
Assert.IsNotNull(innerElements);

var innerElement = innerElements.SingleOrDefault();
Assert.IsNotNull(innerElement);
Assert.AreEqual("world", innerElement.TextContent?.Trim());
}
}
}
}
}
}
Loading

0 comments on commit 1358b6c

Please sign in to comment.