Skip to content

Commit

Permalink
Set capabilities from config (#54)
Browse files Browse the repository at this point in the history
* Added a custom dictionary parser for Config.NET.
* Allows supporting adding capabilities via config

* Implement the custom parser

* Allow configuring acceptance of insecure certs.
Allow configuring capabilties

* Add CreateDriver overload with no args

* Rename base options/service map classes
Add SetCapability() and AcceptsInsecureCerts()

* Implement SetCapabilities() method

* Throw error if page key does not exist

* Move Selenoid and Docker stuff to their own folder

* Fix check for null dictionary
  • Loading branch information
tnypxl authored Jul 27, 2020
1 parent f3b8404 commit 620e1d2
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 68 deletions.
2 changes: 0 additions & 2 deletions Basin.Tests/Steps/StepsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.IO;
using Basin.PageObjects;
using Basin.Selenium;
Expand All @@ -22,7 +21,6 @@ public static void BeforeFeatureHook()
[BeforeScenario]
public void BeforeScenarioHook()
{
// BasinEnv.UseBrowser("Chrome");
BrowserSession.Init();
}

Expand Down
5 changes: 4 additions & 1 deletion Basin/BasinEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Basin.Config.Interfaces;
using Config.Net;
using Basin.PageObjects.Interfaces;
using Basin.Config.Extensions;

namespace Basin
{
Expand All @@ -22,7 +23,9 @@ public static class BasinEnv

public static void SetConfig(string configPath)
{
_config = new CurrentConfig(GetConfig.UseJsonFile(configPath).Build());
_config = new CurrentConfig(GetConfig.UseJsonFile(configPath)
.UseTypeParser(new DictionaryParser())
.Build());
Site = _config.Site;
Browser = _config.Browser;
Pages = _config.Pages;
Expand Down
31 changes: 31 additions & 0 deletions Basin/Config/Extensions/DictionaryParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using Config.Net;
using Newtonsoft.Json;

namespace Basin.Config.Extensions
{
public class DictionaryParser : ITypeParser
{
public IEnumerable<Type> SupportedTypes => new[] { typeof(Dictionary<string, object>) };

public bool TryParse(string value, Type t, out object result)
{
if (value == null)
{
result = null;
return false;
}

result = JsonConvert.DeserializeObject(value, t);

return true;
}

public string ToRawString(object value)
{
if (value == null) return null;
return JsonConvert.SerializeObject(value);
}
}
}
9 changes: 9 additions & 0 deletions Basin/Config/Interfaces/IBrowserConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ public interface IBrowserConfig
[Option(DefaultValue = false)]
bool Headless { get; set; }

[Option(DefaultValue = false)]
bool AcceptsInsecureCerts { get; set; }

string PathToDriverBinary { get; set; }

string PathToBrowserExecutable { get; set; }

Uri Host { get; set; }

Dictionary<string, object> Capabilities { get; }
}

public class BrowserConfig : IBrowserConfig
Expand All @@ -55,5 +60,9 @@ public class BrowserConfig : IBrowserConfig
public string PathToBrowserExecutable { get; set; }

public Uri Host { get; set; }

public bool AcceptsInsecureCerts { get; set; }

public Dictionary<string, object> Capabilities { get; }
}
}
4 changes: 2 additions & 2 deletions Basin/Core/Browsers/ChromeBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public ChromeBrowser(IBrowserConfig config)

public IWebDriver Driver { get; set; }

// public void CreateDriver() => Driver = new ChromeDriver(Service, Options);
public void CreateDriver() => CreateDriver(null);

public void CreateDriver(Uri host = null)
public void CreateDriver(Uri host)
{
Driver = (host == null)
? new ChromeDriver(Service, Options)
Expand Down
2 changes: 2 additions & 0 deletions Basin/Core/Browsers/FirefoxBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public FirefoxBrowser(IBrowserConfig config)

public IWebDriver Driver { get; set; }

public void CreateDriver() => CreateDriver(null);

public void CreateDriver(Uri host = null)
{
Driver = host == null
Expand Down
3 changes: 3 additions & 0 deletions Basin/Core/Browsers/InternetExplorerBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public InternetExplorerBrowser(IBrowserConfig config)

public IWebDriver Driver { get; set; }

public void CreateDriver() => CreateDriver(null);

public void CreateDriver(Uri host)
{

Driver = (host == null)
? new InternetExplorerDriver(Service, Options)
: new RemoteWebDriver(host, Options);
Expand Down
24 changes: 19 additions & 5 deletions Basin/Core/Browsers/Mappers/ChromeOptionsMapper.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Basin.Config.Interfaces;
using OpenQA.Selenium.Chrome;

namespace Basin.Core.Browsers.Mappers
{
public class ChromeOptionsMapper : BrowserOptionsMapper<ChromeOptions>
public class ChromeOptionsMapper : DriverOptionsMap<ChromeOptions>
{
public ChromeOptionsMapper() : base(new ChromeOptions())
public ChromeOptionsMapper()
{
}

public ChromeOptionsMapper(IBrowserConfig config) : base(new ChromeOptions())
public ChromeOptionsMapper(IBrowserConfig config)
{
PathToBrowserBinary = config.PathToBrowserExecutable;
BrowserVersion = config.Version;
PlatformName = config.PlatformName;
Arguments = config.Arguments;
EnableHeadlessMode = config.Headless;
AcceptsInsecureCerts = config.AcceptsInsecureCerts;

SetCapabilities(config.Capabilities);
}

public override string PathToBrowserBinary
Expand All @@ -40,6 +42,11 @@ public override IEnumerable<string> Arguments
set => Options.AddArguments(value);
}

public override bool AcceptsInsecureCerts
{
set => Options.AcceptInsecureCertificates = value;
}

public override bool EnableHeadlessMode
{
set
Expand All @@ -48,5 +55,12 @@ public override bool EnableHeadlessMode
Options.AddArguments("--headless", "--disable-gpu");
}
}

public override void SetCapabilities(Dictionary<string, object> caps = null)
{
if (caps == null || caps.Count == 0) return;

foreach (var cap in caps) Options.AddAdditionalCapability(cap.Key, cap.Value, true);
}
}
}
2 changes: 1 addition & 1 deletion Basin/Core/Browsers/Mappers/ChromeServiceMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Basin.Core.Browsers.Mappers
{
public class ChromeServiceMapper : BrowserServiceMapper<ChromeDriverService>
public class ChromeServiceMapper : DriverServiceMap<ChromeDriverService>
{
public override string PathToDriverBinary { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

namespace Basin.Core.Browsers.Mappers
{
public abstract class BrowserOptionsMapper<TDriverOptions> where TDriverOptions : new()
public abstract class DriverOptionsMap<TDriverOptions> where TDriverOptions : new()
{
protected BrowserOptionsMapper(TDriverOptions options)
{
Options = options;
}
protected DriverOptionsMap() => Options = new TDriverOptions();

public abstract string PathToBrowserBinary { set; }

Expand All @@ -20,6 +17,10 @@ protected BrowserOptionsMapper(TDriverOptions options)

public abstract bool EnableHeadlessMode { set; }

public abstract bool AcceptsInsecureCerts { set; }

public abstract void SetCapabilities(Dictionary<string, object> value);

public TDriverOptions Options { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Basin.Config.Interfaces;
using Basin.Config.Interfaces;
using OpenQA.Selenium;

namespace Basin.Core.Browsers.Mappers
{
public abstract class BrowserServiceMapper<TDriverService>
public abstract class DriverServiceMap<TDriverService>
{
public abstract string PathToDriverBinary { get; set; }

Expand Down
43 changes: 31 additions & 12 deletions Basin/Core/Browsers/Mappers/FirefoxOptionsMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@

namespace Basin.Core.Browsers.Mappers
{
public class FirefoxOptionsMapper : BrowserOptionsMapper<FirefoxOptions>
public class FirefoxOptionsMapper : DriverOptionsMap<FirefoxOptions>
{
private readonly IBrowserConfig _config;
public FirefoxOptionsMapper()
{
}

public FirefoxOptionsMapper(IBrowserConfig config) : base(new FirefoxOptions())
public FirefoxOptionsMapper(IBrowserConfig config)
{
_config = config;
PathToBrowserBinary = _config.PathToBrowserExecutable;
BrowserVersion = _config.Version;
PlatformName = _config.PlatformName;
Arguments = _config.Arguments;
EnableHeadlessMode = _config.Headless;
PathToBrowserBinary = config.PathToBrowserExecutable;
BrowserVersion = config.Version;
PlatformName = config.PlatformName;
Arguments = config.Arguments;
EnableHeadlessMode = config.Headless;
AcceptsInsecureCerts = config.AcceptsInsecureCerts;

SetCapabilities(config.Capabilities);
}

public override string PathToBrowserBinary
Expand All @@ -35,16 +39,31 @@ public override string PlatformName

public override IEnumerable<string> Arguments
{
set => Options.AddArguments(value);
set
{
if (value == null) return;
Options.AddArguments(value);
}
}

public override bool AcceptsInsecureCerts
{
set => Options.AcceptInsecureCertificates = value;
}

public override bool EnableHeadlessMode
{
set
{
if (value)
Options.AddArgument("-headless");
if (value) Options.AddArgument("-headless");
}
}

public override void SetCapabilities(Dictionary<string, object> caps)
{
if (caps == null || caps.Count == 0) return;

foreach (var cap in caps) Options.AddAdditionalCapability(cap.Key, cap.Value, true);
}
}
}
4 changes: 1 addition & 3 deletions Basin/Core/Browsers/Mappers/FirefoxServiceMapper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.IO;
using Basin.Config.Interfaces;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;

namespace Basin.Core.Browsers.Mappers
{
public class FirefoxServiceMapper : BrowserServiceMapper<FirefoxDriverService>
public class FirefoxServiceMapper : DriverServiceMap<FirefoxDriverService>
{
public override string PathToDriverBinary { get; set; }

Expand Down
34 changes: 23 additions & 11 deletions Basin/Core/Browsers/Mappers/InternetExplorerOptionsMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

namespace Basin.Core.Browsers.Mappers
{
public class InternetExplorerOptionsMapper : BrowserOptionsMapper<InternetExplorerOptions>
public class InternetExplorerOptionsMapper : DriverOptionsMap<InternetExplorerOptions>
{
private readonly IBrowserConfig _config;

public InternetExplorerOptionsMapper() : base(new InternetExplorerOptions())
public InternetExplorerOptionsMapper()
{
}

public InternetExplorerOptionsMapper(IBrowserConfig config) : base(new InternetExplorerOptions())
public InternetExplorerOptionsMapper(IBrowserConfig config)
{
_config = config;
PathToBrowserBinary = _config.PathToBrowserExecutable;
BrowserVersion = _config.Version;
PlatformName = _config.PlatformName;
Arguments = _config.Arguments;
EnableHeadlessMode = _config.Headless;
PathToBrowserBinary = config.PathToBrowserExecutable;
BrowserVersion = config.Version;
PlatformName = config.PlatformName;
Arguments = config.Arguments;
EnableHeadlessMode = config.Headless;
AcceptsInsecureCerts = config.AcceptsInsecureCerts;

SetCapabilities(config.Capabilities);
}

public override string PathToBrowserBinary
Expand All @@ -48,12 +48,24 @@ public override IEnumerable<string> Arguments
}
}

public override bool AcceptsInsecureCerts
{
set => Options.AcceptInsecureCertificates = value;
}

public override bool EnableHeadlessMode
{
set
{
if (value) throw new NotSupportedException("Internet Explorer does not support headless execution.");
}
}

public override void SetCapabilities(Dictionary<string, object> caps)
{
if (caps == null || caps.Count == 0) return;

foreach (var cap in caps) Options.AddAdditionalCapability(cap.Key, cap.Value, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Basin.Core.Browsers.Mappers
{
public class InternetExplorerServiceMapper : BrowserServiceMapper<InternetExplorerDriverService>
public class InternetExplorerServiceMapper : DriverServiceMap<InternetExplorerDriverService>
{
public override string PathToDriverBinary { get; set; }

Expand Down
5 changes: 4 additions & 1 deletion Basin/PageObjects/PageCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ public TPage Get<TPage>()
{
var pageKey = typeof(TPage).ToString();

if (!Pages.ContainsKey(pageKey))
throw new NullReferenceException($"Collection does not contain a page with key `{pageKey}`."); ;

Pages.TryGetValue(pageKey, out object page);

return (TPage)page ?? throw new NullReferenceException($"Collection does not contain a page with key `{pageKey}`.");
return (TPage)page;
}

public IDictionary<string, object> Pages { get; }
Expand Down
Loading

0 comments on commit 620e1d2

Please sign in to comment.