Skip to content

Commit d39c1fc

Browse files
committed
Created new cross-browser test configuration mechanism for .NET
1 parent 74f96fd commit d39c1fc

File tree

7 files changed

+186
-38
lines changed

7 files changed

+186
-38
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace OpenQA.Selenium.Environment
10+
{
11+
[JsonObject]
12+
public class DriverConfig
13+
{
14+
[JsonProperty]
15+
public string DriverTypeName { get; set; }
16+
17+
[JsonProperty]
18+
public string AssemblyName { get; set; }
19+
20+
[JsonProperty]
21+
[JsonConverter(typeof(StringEnumConverter))]
22+
public Browser BrowserValue { get; set; }
23+
24+
[JsonProperty]
25+
public string RemoteCapabilities { get; set; }
26+
27+
[JsonProperty]
28+
public bool AutoStartRemoteServer { get; set; }
29+
}
30+
}

dotnet/test/common/Environment/EnvironmentManager.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Reflection;
33
using System.IO;
4+
using Newtonsoft.Json;
5+
using NUnit.Framework;
46

57
namespace OpenQA.Selenium.Environment
68
{
@@ -17,23 +19,21 @@ public class EnvironmentManager
1719

1820
private EnvironmentManager()
1921
{
20-
string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
21-
try
22-
{
23-
string driverClassName = GetSettingValue("Driver");
24-
string assemblyName = GetSettingValue("Assembly");
25-
Assembly assembly = Assembly.Load(assemblyName);
26-
driverType = assembly.GetType(driverClassName);
27-
browser = (Browser)Enum.Parse(typeof(Browser), GetSettingValue("DriverName"));
28-
remoteCapabilities = GetSettingValue("RemoteCapabilities");
29-
}
30-
catch (Exception)
31-
{
32-
}
22+
string currentDirectory = this.CurrentDirectory;
23+
string content = File.ReadAllText(Path.Combine(currentDirectory, "appconfig.json"));
24+
TestEnvironment env = JsonConvert.DeserializeObject<TestEnvironment>(content);
25+
string activeDriverConfig = TestContext.Parameters.Get("ActiveDriverConfig", env.ActiveDriverConfig);
26+
string activeWebsiteConfig = TestContext.Parameters.Get("ActiveWebsiteConfig", env.ActiveWebsiteConfig);
27+
DriverConfig driverConfig = env.DriverConfigs[activeDriverConfig];
28+
WebsiteConfig websiteConfig = env.WebSiteConfigs[activeWebsiteConfig];
3329

34-
urlBuilder = new UrlBuilder();
30+
Assembly driverAssembly = Assembly.Load(driverConfig.AssemblyName);
31+
driverType = driverAssembly.GetType(driverConfig.DriverTypeName);
32+
browser = driverConfig.BrowserValue;
33+
remoteCapabilities = driverConfig.RemoteCapabilities;
34+
35+
urlBuilder = new UrlBuilder(websiteConfig);
3536

36-
string currentDirectory = this.CurrentDirectory;
3737
DirectoryInfo info = new DirectoryInfo(currentDirectory);
3838
while (info != info.Root && string.Compare(info.Name, "build", StringComparison.OrdinalIgnoreCase) != 0)
3939
{
@@ -45,7 +45,7 @@ private EnvironmentManager()
4545
bool autoStartRemoteServer = false;
4646
if (browser == Browser.Remote)
4747
{
48-
autoStartRemoteServer = bool.Parse(GetSettingValue("AutoStartRemoteServer"));
48+
autoStartRemoteServer = driverConfig.AutoStartRemoteServer;
4949
}
5050

5151
remoteServer = new RemoteSeleniumServer(info.FullName, autoStartRemoteServer);
@@ -61,20 +61,6 @@ private EnvironmentManager()
6161
}
6262
}
6363

64-
public static string GetSettingValue(string key)
65-
{
66-
string settingValue = string.Empty;
67-
try
68-
{
69-
settingValue = System.Configuration.ConfigurationManager.AppSettings.GetValues(key)[0];
70-
}
71-
catch (Exception)
72-
{
73-
}
74-
75-
return settingValue;
76-
}
77-
7864
public Browser Browser
7965
{
8066
get { return browser; }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenQA.Selenium.Environment
9+
{
10+
[JsonObject]
11+
class TestEnvironment
12+
{
13+
[JsonProperty]
14+
public string ActiveDriverConfig { get; set; }
15+
16+
[JsonProperty]
17+
public string ActiveWebsiteConfig { get; set; }
18+
19+
[JsonProperty]
20+
public Dictionary<string, WebsiteConfig> WebSiteConfigs { get; set; }
21+
22+
[JsonProperty]
23+
public Dictionary<string, DriverConfig> DriverConfigs { get; set; }
24+
}
25+
}

dotnet/test/common/Environment/UrlBuilder.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ public string Path
2727
get { return path; }
2828
}
2929

30-
public UrlBuilder()
30+
public UrlBuilder(WebsiteConfig config)
3131
{
32-
protocol = EnvironmentManager.GetSettingValue("Protocol");
33-
hostName = EnvironmentManager.GetSettingValue("HostName");
34-
port = EnvironmentManager.GetSettingValue("Port");
35-
securePort = EnvironmentManager.GetSettingValue("SecurePort");
36-
// TODO(andre.nogueira): Remove trailing / from folder
37-
path = EnvironmentManager.GetSettingValue("Folder");
32+
protocol = config.Protocol;
33+
hostName = config.HostName;
34+
port = config.Port;
35+
securePort = config.SecurePort;
36+
path = config.Folder;
3837
//Use the first IPv4 address that we find
3938
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
4039
foreach (IPAddress ip in Dns.GetHostEntry(hostName).AddressList)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenQA.Selenium.Environment
9+
{
10+
[JsonObject]
11+
public class WebsiteConfig
12+
{
13+
[JsonProperty]
14+
public string Protocol { get; set; }
15+
16+
[JsonProperty]
17+
public string HostName { get; set; }
18+
19+
[JsonProperty]
20+
public string Port { get; set; }
21+
22+
[JsonProperty]
23+
public string SecurePort { get; set; }
24+
25+
[JsonProperty]
26+
public string Folder { get; set; }
27+
}
28+
}

dotnet/test/common/WebDriver.Common.Tests.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
3232
<ItemGroup>
33+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
34+
<SpecificVersion>False</SpecificVersion>
35+
<HintPath>..\..\..\third_party\dotnet\json-net-10.0r2\net45\Newtonsoft.Json.dll</HintPath>
36+
</Reference>
3337
<Reference Include="NMock3">
3438
<HintPath>..\..\..\third_party\dotnet\nmock-3.3.5.44\net40\NMock3.dll</HintPath>
3539
</Reference>
@@ -71,6 +75,7 @@
7175
<Compile Include="CustomTestAttributes\IgnoreBrowserAttribute.cs" />
7276
<Compile Include="CustomTestAttributes\NeedsFreshDriverAttribute.cs" />
7377
<Compile Include="DesiredCapabililtiesTest.cs" />
78+
<Compile Include="Environment\DriverConfig.cs" />
7479
<Compile Include="DriverDisposalTest.cs" />
7580
<Compile Include="DriverElementFindingTest.cs" />
7681
<Compile Include="DriverTestFixture.cs" />
@@ -118,13 +123,15 @@
118123
<Compile Include="TagNameTest.cs" />
119124
<Compile Include="TakesScreenshotTest.cs" />
120125
<Compile Include="TargetLocatorTest.cs" />
126+
<Compile Include="Environment\TestEnvironment.cs" />
121127
<Compile Include="TestUtilities.cs" />
122128
<Compile Include="TextHandlingTest.cs" />
123129
<Compile Include="TextPagesTest.cs" />
124130
<Compile Include="TypingTest.cs" />
125131
<Compile Include="UploadTest.cs" />
126132
<Compile Include="VisibilityTest.cs" />
127133
<Compile Include="WebElementTest.cs" />
134+
<Compile Include="Environment\WebsiteConfig.cs" />
128135
<Compile Include="WindowSwitchingTest.cs" />
129136
<Compile Include="WindowTest.cs" />
130137
<Compile Include="XPathElementFindingTest.cs" />
@@ -136,7 +143,9 @@
136143
</ProjectReference>
137144
</ItemGroup>
138145
<ItemGroup>
139-
<None Include="App.config" />
146+
<None Include="appconfig.json">
147+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
148+
</None>
140149
</ItemGroup>
141150
<ItemGroup>
142151
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />

dotnet/test/common/appconfig.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"ActiveDriverConfig": "Chrome",
3+
"ActiveWebsiteConfig": "Default",
4+
"WebsiteConfigs": {
5+
"Default": {
6+
"Protocol": "http",
7+
"HostName": "localhost",
8+
"Port": "2310",
9+
"SecurePort": "2410",
10+
"Folder": "common"
11+
}
12+
},
13+
"DriverConfigs": {
14+
"Chrome": {
15+
"DriverTypeName": "OpenQA.Selenium.Chrome.ChromeDriver",
16+
"AssemblyName": "WebDriver",
17+
"BrowserValue": "Chrome",
18+
"RemoteCapabilities": "chrome"
19+
},
20+
21+
"IE": {
22+
"DriverTypeName": "OpenQA.Selenium.IE.InternetExplorerDriver",
23+
"AssemblyName": "WebDriver",
24+
"BrowserValue": "IE",
25+
"RemoteCapabilities": "internet explorer"
26+
},
27+
28+
"Edge": {
29+
"DriverTypeName": "OpenQA.Selenium.Edge.EdgeDriver",
30+
"AssemblyName": "WebDriver",
31+
"BrowserValue": "Edge",
32+
"RemoteCapabilities": "MicrosoftEdge"
33+
},
34+
35+
"Firefox": {
36+
"DriverTypeName": "OpenQA.Selenium.Firefox.ReleaseFirefoxWebDriver",
37+
"AssemblyName": "WebDriver.Firefox.Tests",
38+
"BrowserValue": "Firefox",
39+
"RemoteCapabilities": "firefox"
40+
},
41+
42+
"FirefoxNightly": {
43+
"DriverTypeName": "OpenQA.Selenium.Firefox.NightlyFirefoxWebDriver",
44+
"AssemblyName": "WebDriver.Firefox.Tests",
45+
"BrowserValue": "Firefox",
46+
"RemoteCapabilities": "firefox"
47+
},
48+
49+
"FirefoxLegacy": {
50+
"DriverTypeName": "OpenQA.Selenium.Firefox.LegacyFirefoxWebDriver",
51+
"AssemblyName": "WebDriver.Firefox.Tests",
52+
"BrowserValue": "Firefox",
53+
"RemoteCapabilities": "firefox"
54+
},
55+
56+
"Safari": {
57+
"DriverTypeName": "OpenQA.Selenium.Safari.SafariDriver",
58+
"AssemblyName": "WebDriver",
59+
"BrowserValue": "Safari",
60+
"RemoteCapabilities": "safari"
61+
},
62+
63+
"Remote": {
64+
"DriverTypeName": "OpenQA.Selenium.Remote.TestInternetExplorerRemoteWebDriver",
65+
"AssemblyName": "WebDriver.Remote.Tests",
66+
"BrowserValue": "Remote",
67+
"RemoteCapabilities": "internet explorer",
68+
"AutoStartRemoteServer": true
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)