Skip to content

Commit 541360d

Browse files
committed
Upgraded to .Net Core
1 parent 4fc8a2f commit 541360d

34 files changed

+248
-238
lines changed

README.md

+27-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This repository contains Webdriver code examples, exercises and tutorials for de
55
Over time, more and more test example will be uploaded here.
66
All tests examples in this repository is to be considered public domain unless stated otherwise.
77

8+
**NOTE:** Currently supports the chrome Driver, and Gecko (firefox) support is on the way.
9+
810
## Demo website to practice Webdriver:
911
Here is a list of websites where you can practice Selenium webdriver. You will find the list incredibly useful as these will cover many of your real-time web automation use case scenario. Some of the common examples includes are like testing of a login page, online registration forms, and automating flight booking.
1012

@@ -31,13 +33,11 @@ Here is a list of websites where you can practice Selenium webdriver. You will f
3133
- [ ] Checkbox
3234
- [ ] Datepicker
3335

34-
--
3536

3637
## Pre-requisites
3738
- Windows OS
3839
- Visual Studio
3940

40-
--
4141

4242
## Let's get started
4343

@@ -46,9 +46,12 @@ Here is a list of websites where you can practice Selenium webdriver. You will f
4646
$ git clone https://github.com/sayems/csharp.webdriver
4747
```
4848

49-
#### Visual Studio
50-
- (1) Start Visual Studio.
51-
- (2) On the menu bar, choose File, Open, Project/Solution.
49+
#### Install Visual Studio and .NET Core
50+
51+
- (1) [Install Visual Studio](https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx)
52+
- (2) [.NET Core](https://www.microsoft.com/net/download/core)
53+
- (3) Start Visual Studio.
54+
- (4) On the menu bar, choose File, Open, Project/Solution.
5255
Navigate to the csharp.webdriver project folder and open it
5356

5457
#### Install NUnit Templates for Visual Studio
@@ -63,16 +66,23 @@ $ git clone https://github.com/sayems/csharp.webdriver
6366

6467
#### Install Nuget package
6568
- Right-click on your project and click “Manage NuGet Packages”.
66-
- Search for "[Selenium.Support](https://www.nuget.org/packages/Selenium.Support)" library and install it
69+
- Search for "Microsoft.Extensions.Configuration.UserSecrets" and install it
70+
- Search for "System.Diagnostics.Contracts" and install it
6771
- Search for "[NUnit](https://www.nuget.org/packages/NUnit/)" Framework and install it
6872

73+
#### Install Selenium
74+
Currently, there is no official support for .NET Core, But there is a Nuget package which provides .NET Core support for those of us who can’t wait, please visit [CoreCompat.Selenium.WebDriver](https://www.nuget.org/packages/CoreCompat.Selenium.WebDriver/2.54.0-beta002) to learn more about it.
75+
76+
#### Install *Unofficial* Selenium for .Net Core
77+
- Search for "CoreCompat.Selenium.WebDriver" library and install it
78+
6979
### Create NUnit test
7080
Click on *Create new project*
7181
![](https://github.com/sayems/csharp.webdriver/blob/master/docs/images/start-page.png)
7282

7383
Select *Test* and then Select *NUnit 3 Test Project* to create NUnit 3 project
7484
![](https://github.com/sayems/csharp.webdriver/blob/master/docs/images/nunit3-test.png)
75-
--
85+
7686

7787
### Add NUnit test from Solution Explorer
7888

@@ -84,15 +94,22 @@ Select *Test* and then select *NUnit Test Fixture* or *NUnit Setup Fixture*
8494

8595
Congratulations! You have just created your first NUnit test!
8696

87-
###Contributions
97+
98+
99+
### Issues
100+
Parallel execution of methods within a class is not yet implemented. More information is provided here: [Run test methods within a fixture in parallel](https://github.com/nunit/nunit/issues/164)
101+
102+
103+
104+
### Contributions
88105

89106
If you have any code examples you would like to contribute to this repository, please feel free to open a pull request.
90107

91-
##Feedback
108+
## Feedback
92109

93110
Contributors to this repo would be very grateful to receive feedback! If you would like to praise or comment on any test examples, or the repo as a whole, please do so in the issue tracker. I'd love to hear what you think, so please take a moment to let me know.
94111

95112

96-
##Contact
113+
## Contact
97114

98115
If you have any questions about this repo, or need some help to contribute, please do not hesitate to contact me.
File renamed without changes.

Selenium/Selenium.csproj

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
11+
<PackageReference Include="nunit" Version="3.11.0" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
14+
<PackageReference Include="Selenium.Support" Version="3.141.0" />
15+
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
16+
<PackageReference Include="System.Diagnostics.Contracts" Version="4.3.0" />
17+
</ItemGroup>
18+
19+
</Project>

Selenium/Selenium.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Selenium", "Selenium.csproj", "{E45A9547-F4BA-4DE4-9D29-875DD8D54794}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

Selenium/appsettings.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"BaseUrl": "https://www.google.com/",
3+
"Username": "sayem",
4+
"Password": "Passw0rd!",
5+
6+
"RemoteBrowser": "false",
7+
8+
"Browser": "Chrome",
9+
10+
"Platform": "Windows 7",
11+
12+
"UseSeleniumGrid": "false",
13+
"GridHubUrl": "http://localhost:4444/wd/hub",
14+
15+
"UseSauceLabs": "true",
16+
"SauceLabsUsername": "ssayem",
17+
"SauceLabsAccessKey": "",
18+
"SauceLabsHubUrl": "http://ondemand.saucelabs.com:80/wd/hub",
19+
20+
"UseBrowserstack": "false",
21+
"BrowserStackUsername": "ssayem",
22+
"BrowserStackAccessKey": "",
23+
"BrowserStackHubUrl": "http://hub-cloud.browserstack.com/wd/hub/"
24+
}
File renamed without changes.

src/Selenium/Selenium/core/TestBase.cs renamed to Selenium/core/TestBase.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using NUnit.Framework;
3-
using Selenium.config;
43
using Selenium.core.browsers;
54

65
namespace Selenium.core
File renamed without changes.

src/Selenium/Selenium/core/browsers/AbstractFactory.cs renamed to Selenium/core/browsers/AbstractFactory.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using OpenQA.Selenium.Chrome;
55
using OpenQA.Selenium.Firefox;
66
using OpenQA.Selenium.Remote;
7-
using Selenium.config;
87

98
namespace Selenium.core.browsers
109
{
@@ -26,14 +25,6 @@ public IBrowser Create<T>() where T : IWebDriver
2625
return factoryMethod?.Create();
2726
}
2827

29-
public IBrowser GetBrowser(BrowserType type)
30-
{
31-
if ((Config.RemoteBrowser && Config.UseSauceLabs) || (Config.RemoteBrowser && Config.UseBrowserstack) ||
32-
(Config.RemoteBrowser && Config.UseSeleniumGrid))
33-
return _browsers[BrowserType.Remote].Invoke();
34-
return _browsers.ContainsKey(type) ? _browsers[type].Invoke() : _browsers[BrowserType.Firefox].Invoke();
35-
}
36-
3728
private IBrowser Remote()
3829
{
3930
return Create<RemoteWebDriver>();
@@ -48,5 +39,16 @@ private IBrowser Firefox()
4839
{
4940
return Create<FirefoxDriver>();
5041
}
42+
43+
public IBrowser GetBrowser(BrowserType type)
44+
{
45+
return Config.RemoteBrowser && Config.UseSauceLabs ||
46+
Config.RemoteBrowser && Config.UseBrowserstack ||
47+
Config.RemoteBrowser && Config.UseSeleniumGrid
48+
? _browsers[BrowserType.Remote].Invoke()
49+
: (_browsers.ContainsKey(type)
50+
? _browsers[type].Invoke()
51+
: _browsers[BrowserType.Firefox].Invoke());
52+
}
5153
}
5254
}

src/Selenium/Selenium/core/browsers/BrowserFactory.cs renamed to Selenium/core/browsers/BrowserFactory.cs

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using OpenQA.Selenium.Chrome;
44
using OpenQA.Selenium.Firefox;
55
using OpenQA.Selenium.Remote;
6-
using Selenium.config;
76

87
namespace Selenium.core.browsers
98
{
@@ -15,24 +14,23 @@ public sealed class BrowserFactory :
1514
{
1615
IBrowser<ChromeDriver> IBrowserWebDriver<ChromeDriver>.Create()
1716
{
18-
var dirName = AppDomain.CurrentDomain.BaseDirectory;
17+
var dirName = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin", StringComparison.Ordinal));
1918
var fileInfo = new FileInfo(dirName);
20-
var parentDir = fileInfo.Directory?.Parent;
21-
var parentDirName = parentDir?.FullName;
22-
return new BrowserAdapter<ChromeDriver>(new ChromeDriver(parentDirName + @"\libs"), BrowserType.Firefox);
19+
var parentDirName = fileInfo?.FullName;
20+
return new BrowserAdapter<ChromeDriver>(new ChromeDriver(parentDirName + @"libs"), BrowserType.Chrome);
2321
}
2422

2523
IBrowser<FirefoxDriver> IBrowserWebDriver<FirefoxDriver>.Create()
2624
{
27-
var dirName = AppDomain.CurrentDomain.BaseDirectory;
25+
var dirName = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin", StringComparison.Ordinal));
2826
var fileInfo = new FileInfo(dirName);
29-
var parentDir = fileInfo.Directory?.Parent;
30-
var parentDirName = parentDir?.FullName;
27+
var parentDirName = fileInfo?.FullName;
3128
var service = FirefoxDriverService.CreateDefaultService(parentDirName + @"\libs");
3229
service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
3330
return new BrowserAdapter<FirefoxDriver>(new FirefoxDriver(service), BrowserType.Firefox);
3431
}
3532

33+
// TODO - Fix Remote WebDriver
3634
IBrowser<RemoteWebDriver> IBrowserWebDriver<RemoteWebDriver>.Create()
3735
{
3836
DesiredCapabilities capabilities;
@@ -41,10 +39,10 @@ IBrowser<RemoteWebDriver> IBrowserWebDriver<RemoteWebDriver>.Create()
4139
switch (Config.Browser)
4240
{
4341
case BrowserType.Chrome:
44-
capabilities = DesiredCapabilities.Chrome();
42+
capabilities = new DesiredCapabilities();
4543
break;
4644
case BrowserType.Firefox:
47-
capabilities = DesiredCapabilities.Firefox();
45+
capabilities = new DesiredCapabilities();
4846
break;
4947
default:
5048
throw new ArgumentOutOfRangeException();

src/Selenium/Selenium/core/browsers/BrowserType.cs renamed to Selenium/core/browsers/BrowserType.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
3-
namespace Selenium.core.browsers
1+
namespace Selenium.core.browsers
42
{
5-
[Serializable]
63
public enum BrowserType
74
{
85
Firefox,

Selenium/core/browsers/Config.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.IO;
3+
using Microsoft.Extensions.Configuration;
4+
5+
namespace Selenium.core.browsers
6+
{
7+
public class Config
8+
{
9+
public static bool RemoteBrowser => bool.Parse(GetValue("RemoteBrowser"));
10+
11+
public static BrowserType Browser
12+
=> (BrowserType) Enum.Parse(typeof(BrowserType), GetValue("Browser"));
13+
14+
public static string Platform => GetValue("Platform");
15+
public static string BaseUrl => GetValue("BaseUrl");
16+
public static string Username => GetValue("Username");
17+
public static string Password => GetValue("Password");
18+
19+
public static bool UseSeleniumGrid => bool.Parse(GetValue("UseSeleniumGrid"));
20+
public static string GridHubUri => GetValue("GridHubUrl");
21+
22+
public static bool UseSauceLabs => bool.Parse(GetValue("UseSauceLabs"));
23+
public static string SauceLabsHubUri => GetValue("SauceLabsHubUrl");
24+
public static string SauceLabsUsername => GetValue("SauceLabsUsername");
25+
public static string SauceLabsAccessKey => GetValue("SauceLabsAccessKey");
26+
27+
public static bool UseBrowserstack => bool.Parse(GetValue("BrowserStack"));
28+
public static string BrowserStackHubUrl => GetValue("BrowserStackHubUrl");
29+
public static string BrowserStackUsername => GetValue("BrowserStackUsername");
30+
public static string BrowserStackAccessKey => GetValue("BrowserStackAccessKey");
31+
32+
private static string GetValue(string value)
33+
{
34+
var dirName = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin"));
35+
var fileInfo = new FileInfo(dirName);
36+
var parentDirName = fileInfo?.FullName;
37+
38+
var builder = new ConfigurationBuilder()
39+
.SetBasePath(parentDirName)
40+
.AddJsonFile("appsettings.json");
41+
return builder.Build()[value];
42+
}
43+
}
44+
}

Selenium/libs/chromedriver

10.5 MB
Binary file not shown.

Selenium/pages/HomePage.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using Selenium.core.browsers;
3+
4+
namespace Selenium.pages
5+
{
6+
public class HomePage
7+
{
8+
private readonly IBrowser _browser;
9+
10+
public HomePage(IBrowser browser)
11+
{
12+
_browser = browser;
13+
}
14+
15+
public RegisterPage Register()
16+
{
17+
Console.WriteLine("Navigating to Register page");
18+
_browser.Page.GoToUrl("http://www.google.com");
19+
return new RegisterPage(_browser);
20+
}
21+
22+
public RegisterPage OpenYahooPage()
23+
{
24+
Console.WriteLine("Navigating to Yahoo page");
25+
_browser.Page.GoToUrl("http://www.yahoo.com");
26+
return new RegisterPage(_browser);
27+
}
28+
29+
public RegisterPage OpenGooglePage()
30+
{
31+
Console.WriteLine("Navigating to Google page");
32+
_browser.Page.GoToUrl("http://www.google.com");
33+
return new RegisterPage(_browser);
34+
}
35+
36+
public RegisterPage OpenGithubPage()
37+
{
38+
Console.WriteLine("Navigating to Github page");
39+
_browser.Page.GoToUrl("http://www.github.com");
40+
return new RegisterPage(_browser);
41+
}
42+
}
43+
}

Selenium/tests/GithubTest.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NUnit.Framework;
2+
using Selenium.core;
3+
using Selenium.pages;
4+
5+
namespace Selenium.tests
6+
{
7+
[TestFixture]
8+
[Parallelizable]
9+
public class GithubTest : TestBase
10+
{
11+
[Test]
12+
public void TestMethod()
13+
{
14+
var page = new HomePage(Driver);
15+
page.OpenGithubPage();
16+
}
17+
}
18+
}

src/Selenium/Selenium/tests/GoogleSearch.cs renamed to Selenium/tests/GoogleSearch.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Selenium.tests
66
{
77
[TestFixture]
8+
[Parallelizable]
89
public class GoogleSearch : TestBase
910
{
1011
[Test]

0 commit comments

Comments
 (0)