Skip to content

Commit

Permalink
SeleniumManager testing
Browse files Browse the repository at this point in the history
  • Loading branch information
quigamdev committed Jun 3, 2024
1 parent 13e65f5 commit 9984bb2
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static ChromeDriver CreateChromeDriver(LocalWebBrowserFactory factory)
options.AddArgument("disable-popup-blocking");

options.AddArguments(factory.Capabilities);
options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable");
options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion));

if (factory.GetBooleanOption("disableExtensions"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Riganti.Selenium.Core.Drivers.Implementation
public static class DictionaryExtensions
{

public static T2 TryGetOrDefault<T1, T2>(this IDictionary<T1, T2> dic, T1 key, T2 defaultValue)
public static T2 TryGet<T1, T2>(this IDictionary<T1, T2> dic, T1 key)
{
if (dic is not null && dic.TryGetValue(key, out T2 value) && value is T2)
{
return value;
}
return defaultValue;
return default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static EdgeDriver CreateEdgeDriver(LocalWebBrowserFactory factory)
{
};

options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable");
options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion));
return new EdgeDriver(options);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Newtonsoft.Json;
using OpenQA.Selenium.Firefox;
using Riganti.Selenium.Core.Factories;

Expand All @@ -16,7 +18,11 @@ static FirefoxHelpers()

public static FirefoxDriver CreateFirefoxDriver(LocalWebBrowserFactory factory)
{
return new FirefoxDriver(service, GetFirefoxOptions(factory.Options));
Console.WriteLine("Firefox applies capabilities: " + JsonConvert.SerializeObject(factory.Capabilities));
var ffOptions = GetFirefoxOptions(factory.Options);
Console.WriteLine("Firefox applies options: " + JsonConvert.SerializeObject(ffOptions));
var driver = new FirefoxDriver(service, ffOptions);
return driver;
}

public static FirefoxProfile GetFirefoxProfile()
Expand All @@ -30,8 +36,12 @@ public static FirefoxProfile GetFirefoxProfile()

public static FirefoxOptions GetFirefoxOptions(System.Collections.Generic.IDictionary<string, string> _options)
{
var options = new FirefoxOptions { Profile = GetFirefoxProfile() };
options.BrowserVersion = _options.TryGetOrDefault(nameof(options.BrowserVersion), "stable");
var options = new FirefoxOptions
{
// Profile = GetFirefoxProfile()
};
options.BrowserVersion = _options.TryGet(nameof(options.BrowserVersion));
Console.WriteLine($"Requesting {options.BrowserVersion} version of browser");
return options;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static InternetExplorerDriver CreateInternetExplorerDriver(LocalWebBrowse
{
BrowserCommandLineArguments = "-private"
};
options.BrowserVersion = factory.Options.TryGetOrDefault(nameof(options.BrowserVersion), "stable");
options.BrowserVersion = factory.Options.TryGet(nameof(options.BrowserVersion));
return new InternetExplorerDriver(options);
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/Integrations/Riganti.Selenium.xUnit/XunitTestSuiteRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,4 @@ public override void RunInAllBrowsers(ISeleniumTest testClass, Action<IBrowserWr
base.RunInAllBrowsers(testClass, action, callerMemberName, callerFilePath, callerLineNumber);
}
}

//public class SeleniumInternalXunitRunnerReporter : Xunit.IRunnerReporter
//{
// public string Description => "This reporter is a bit hack to close all resources when test execution ends.";

// public bool IsEnvironmentallyEnabled => true;

// public string RunnerSwitch => "riganti-selenium";

// public IMessageSink CreateMessageHandler(IRunnerLogger logger)
// {
// return new SeleniumMessageSink();
// }
//}
//public class SeleniumMessageSink : IMessageSink
//{
// public bool OnMessage(IMessageSinkMessage message)
// {
// if (message is TestAssemblyExecutionFinished)
// {
// if (Debugger.IsAttached)
// Debugger.Break();
// }
// return true;
// }
//}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"factories": {
"chrome:fast": {
"capabilities": [ "--window-size=1920,1080" , "--headless", "--disable-gpu" ],
"BrowserVersion": "124"
"capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ],
"Options": {
"BrowserVersion": "124"
}
}
//"firefox:dev": {
//}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"factories": {
"chrome:fast": {
"capabilities": [ "--no-sandbox" ]
"capabilities": [ "--no-sandbox" ],
"Options": {
"BrowserVersion": "124"
}
}
},
"baseUrls": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using OpenQA.Selenium;

[TestClass]
public class SeleniumManagerTests
{
[TestMethod]
public void GetBinariesVersion113()
{
var data1 = SeleniumManager.BinaryPaths("--browser firefox --driver geckodriver --browser-version 113");
Console.WriteLine("Firefox binaries: " + JsonConvert.SerializeObject(data1));
Assert.IsNotNull(data1["browser_path"]);
Assert.IsNotNull(data1["driver_path"]);

var data2 = SeleniumManager.BinaryPaths("--browser chrome --driver chromedriver --browser-version 113");
Console.WriteLine("Chrome binaries: " + JsonConvert.SerializeObject(data2));
Assert.IsNotNull(data2["browser_path"]);
Assert.IsNotNull(data2["driver_path"]);
}

[TestMethod]
public void GetBinariesVersionStable()
{
var data1 = SeleniumManager.BinaryPaths("--browser firefox --driver geckodriver --browser-version stable");
Console.WriteLine("Firefox binaries: " + JsonConvert.SerializeObject(data1));
Assert.IsNotNull(data1["browser_path"]);
Assert.IsNotNull(data1["driver_path"]);

var data2 = SeleniumManager.BinaryPaths("--browser chrome --driver chromedriver --browser-version stable");
Console.WriteLine("Chrome binaries: " + JsonConvert.SerializeObject(data2));
Assert.IsNotNull(data2["browser_path"]);
Assert.IsNotNull(data2["driver_path"]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"factories": {
"chrome:fast": {
"capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ],
"BrowserVersion": "124"
"Options": {
"BrowserVersion": "124"
}
},
//"firefox:fast": {
// "capabilities": [ "--window-size=1920,1080", "--headless", "--disable-gpu" ],
// "BrowserVersion": "stable"
// "Options": {
// "BrowserVersion": "104"
// }
//}
//"chrome:coordinator": {
// "options": {
Expand Down
22 changes: 22 additions & 0 deletions src/Tests/Riganti.Selenium.Core.UnitTests/SeleniumManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Text.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;

[TestClass]
public class SeleniumManagerTests
{
[TestMethod]
public void GetBinaries()
{
var data1 = SeleniumManager.BinaryPaths("--browser firefox --driver geckodriver --browser-version 104");
Console.WriteLine("Firefox binaries: " + JsonSerializer.Serialize(data1));
Assert.IsNotNull(data1["browser_path"]);
Assert.IsNotNull(data1["driver_path"]);

var data2 = SeleniumManager.BinaryPaths("--browser chrome --driver chromedriver --browser-version 104");
Console.WriteLine("Chrome binaries: " + JsonSerializer.Serialize(data2));
Assert.IsNotNull(data2["browser_path"]);
Assert.IsNotNull(data2["driver_path"]);
}
}
4 changes: 3 additions & 1 deletion src/Tests/Riganti.Selenium.Sandbox/seleniumconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"factories": {
"firefox:fast": {
"BrowserVersion": "stable"
"Options": {
"BrowserVersion": "stable"
}
}
},
"baseUrls": [
Expand Down

0 comments on commit 9984bb2

Please sign in to comment.