Skip to content

Commit

Permalink
Added support for user-controlled remote WebDriver services, PhantomJ…
Browse files Browse the repository at this point in the history
…S via Selenium, updated to Se 2.34
  • Loading branch information
stirno committed Aug 9, 2013
1 parent acdc139 commit e961f27
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 14 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified FluentAutomation.SeleniumWebDriver/3rdPartyLib/chromedriver.exe
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WebDriver, Version=2.33.0.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="WebDriver, Version=2.34.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Selenium.WebDriver.2.33.0\lib\net40\WebDriver.dll</HintPath>
<HintPath>..\packages\Selenium.WebDriver.2.34.0\lib\net40\WebDriver.dll</HintPath>
</Reference>
<Reference Include="WebDriver.Support, Version=2.33.0.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="WebDriver.Support, Version=2.34.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Selenium.Support.2.33.0\lib\net40\WebDriver.Support.dll</HintPath>
<HintPath>..\packages\Selenium.Support.2.34.0\lib\net40\WebDriver.Support.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -79,7 +79,13 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="3rdPartyLib\chromedriver.exe" />
<EmbeddedResource Include="3rdPartyLib\IEDriverServer.exe" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="3rdPartyLib\IEDriverServer32.exe" />
<EmbeddedResource Include="3rdPartyLib\IEDriverServer64.exe" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="3rdPartyLib\phantomjs.exe" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Expand Down
111 changes: 108 additions & 3 deletions FluentAutomation.SeleniumWebDriver/SeleniumWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using FluentAutomation.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace FluentAutomation
{
Expand All @@ -24,15 +25,25 @@ public enum Browser
/// </summary>
InternetExplorer = 1,

/// <summary>
/// Internet Explorer (64-bit). Before using, make sure to set ProtectedMode settings to be the same for all zones.
/// </summary>
InternetExplorer64 = 2,

/// <summary>
/// Mozilla Firefox
/// </summary>
Firefox = 2,
Firefox = 3,

/// <summary>
/// Google Chrome
/// </summary>
Chrome = 4
Chrome = 4,

/// <summary>
/// PhantomJS - Headless browser - Support is Experimental
/// </summary>
PhantomJs = 5
}

/// <summary>
Expand Down Expand Up @@ -65,7 +76,11 @@ public static void Bootstrap(Browser browser)
switch (SeleniumWebDriver.SelectedBrowser)
{
case Browser.InternetExplorer:
EmbeddedResources.UnpackFromAssembly("IEDriverServer.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
EmbeddedResources.UnpackFromAssembly("IEDriverServer32.exe", "IEDriverServer.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
container.Register<IWebDriver, Wrappers.IEDriverWrapper>().AsMultiInstance();
break;
case Browser.InternetExplorer64:
EmbeddedResources.UnpackFromAssembly("IEDriverServer64.exe", "IEDriverServer.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
container.Register<IWebDriver, Wrappers.IEDriverWrapper>().AsMultiInstance();
break;
case Browser.Firefox:
Expand All @@ -75,7 +90,97 @@ public static void Bootstrap(Browser browser)
EmbeddedResources.UnpackFromAssembly("chromedriver.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
container.Register<IWebDriver, OpenQA.Selenium.Chrome.ChromeDriver>().AsMultiInstance();
break;
case Browser.PhantomJs:
EmbeddedResources.UnpackFromAssembly("phantomjs.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
container.Register<IWebDriver, OpenQA.Selenium.PhantomJS.PhantomJSDriver>().AsMultiInstance();
break;
}
};
}

/// <summary>
/// Bootstrap Selenium provider using a Remote web driver targetting the requested browser
/// </summary>
/// <param name="driverUri"></param>
/// <param name="capabilities"></param>
public static void Bootstrap(Uri driverUri, Browser browser)
{
SeleniumWebDriver.SelectedBrowser = browser;

FluentAutomation.Settings.Registration = (container) =>
{
container.Register<ICommandProvider, CommandProvider>();
container.Register<IExpectProvider, ExpectProvider>();
container.Register<IFileStoreProvider, LocalFileStoreProvider>();

DesiredCapabilities browserCapabilities = null;

switch (SeleniumWebDriver.SelectedBrowser)
{
case Browser.InternetExplorer:
case Browser.InternetExplorer64:
browserCapabilities = DesiredCapabilities.InternetExplorer();
break;
case Browser.Firefox:
browserCapabilities = DesiredCapabilities.Firefox();
break;
case Browser.Chrome:
browserCapabilities = DesiredCapabilities.Chrome();
break;
case Browser.PhantomJs:
browserCapabilities = DesiredCapabilities.PhantomJS();
break;
}

container.Register<IWebDriver, RemoteWebDriver>(new RemoteWebDriver(driverUri, browserCapabilities));
};
}

/// <summary>
/// Bootstrap Selenium provider using a Remote web driver service with the requested capabilities
/// </summary>
/// <param name="driverUri"></param>
/// <param name="capabilities"></param>
public static void Bootstrap(Uri driverUri, Dictionary<string, object> capabilities)
{
FluentAutomation.Settings.Registration = (container) =>
{
container.Register<ICommandProvider, CommandProvider>();
container.Register<IExpectProvider, ExpectProvider>();
container.Register<IFileStoreProvider, LocalFileStoreProvider>();

DesiredCapabilities browserCapabilities = null;

switch (SeleniumWebDriver.SelectedBrowser)
{
case Browser.InternetExplorer:
case Browser.InternetExplorer64:
browserCapabilities = DesiredCapabilities.InternetExplorer();
break;
case Browser.Firefox:
browserCapabilities = DesiredCapabilities.Firefox();
break;
case Browser.Chrome:
browserCapabilities = DesiredCapabilities.Chrome();
break;
case Browser.PhantomJs:
browserCapabilities = DesiredCapabilities.PhantomJS();
break;
}

if (browserCapabilities == null)
{
browserCapabilities = new DesiredCapabilities(capabilities);
}
else
{
foreach (var cap in capabilities)
{
browserCapabilities.SetCapability(cap.Key, cap.Value);
}
}

container.Register<IWebDriver, RemoteWebDriver>(new RemoteWebDriver(driverUri, browserCapabilities));
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions FluentAutomation.SeleniumWebDriver/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="5.0.5" targetFramework="net40" />
<package id="Selenium.Support" version="2.33.0" targetFramework="net40" />
<package id="Selenium.WebDriver" version="2.33.0" targetFramework="net40" />
<package id="Selenium.Support" version="2.34.0" targetFramework="net40" />
<package id="Selenium.WebDriver" version="2.34.0" targetFramework="net40" />
</packages>
1 change: 1 addition & 0 deletions FluentAutomation.Tests/FluentAutomation.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Native\WatiN.cs" />
<Compile Include="Remote\Node.cs" />
<Compile Include="Remote\RemoteTest.cs" />
<Compile Include="WaitUntilTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion FluentAutomation.Tests/Native/SeleniumChrome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SeleniumChrome : RepeatableNativeTest
{
public SeleniumChrome()
{
FluentAutomation.SeleniumWebDriver.Bootstrap(SeleniumWebDriver.Browser.Chrome);
FluentAutomation.SeleniumWebDriver.Bootstrap(SeleniumWebDriver.Browser.PhantomJs);
}

[Fact]
Expand Down
22 changes: 22 additions & 0 deletions FluentAutomation.Tests/Remote/RemoteTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace FluentAutomation.Tests.Remote
{
public class RemoteTest : FluentTest
{
public RemoteTest()
{
FluentAutomation.SeleniumWebDriver.Bootstrap(new Uri("http://localhost:9515"), SeleniumWebDriver.Browser.Chrome);
}

[Fact]
public void TestOp()
{
I.Open("http://www.bing.com");
}
}
}
22 changes: 20 additions & 2 deletions FluentAutomation/EmbeddedResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,34 @@ namespace FluentAutomation
{
public static class EmbeddedResources
{
public static string WorkingDirectory
{
get
{
var execAssembly = Assembly.GetExecutingAssembly();
var dirBasePath = Path.GetDirectoryName(AppDomain.CurrentDomain.ShadowCopyFiles ? execAssembly.CodeBase : execAssembly.Location);

return new Uri(dirBasePath).LocalPath;
}
}

public static void UnpackFromAssembly(string resourceFileName, Assembly assembly)
{
UnpackFromAssembly(resourceFileName, resourceFileName, assembly);
}

public static void UnpackFromAssembly(string resourceFileName, string outputFileName, Assembly assembly)
{
var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.EndsWith(resourceFileName));
if (!File.Exists(resourceFileName))
var outputName = Path.Combine(WorkingDirectory, outputFileName);

if (!File.Exists(outputName))
{
var resourceStream = assembly.GetManifestResourceStream(resourceName);
var resourceBytes = new byte[(int)resourceStream.Length];

resourceStream.Read(resourceBytes, 0, resourceBytes.Length);
File.WriteAllBytes(resourceFileName, resourceBytes);
File.WriteAllBytes(outputName, resourceBytes);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion FluentAutomation/Properties/AssemblyGlobal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.0.0.2")]
[assembly: AssemblyVersion("2.1.0.0")]

0 comments on commit e961f27

Please sign in to comment.