Skip to content

Commit

Permalink
Upgraded to .Net Core
Browse files Browse the repository at this point in the history
  • Loading branch information
sayems committed Aug 5, 2019
1 parent 4fc8a2f commit 541360d
Show file tree
Hide file tree
Showing 34 changed files with 248 additions and 238 deletions.
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This repository contains Webdriver code examples, exercises and tutorials for de
Over time, more and more test example will be uploaded here.
All tests examples in this repository is to be considered public domain unless stated otherwise.

**NOTE:** Currently supports the chrome Driver, and Gecko (firefox) support is on the way.

## Demo website to practice Webdriver:
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.

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

--

## Pre-requisites
- Windows OS
- Visual Studio

--

## Let's get started

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

#### Visual Studio
- (1) Start Visual Studio.
- (2) On the menu bar, choose File, Open, Project/Solution.
#### Install Visual Studio and .NET Core

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

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

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

#### Install Selenium
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.

#### Install *Unofficial* Selenium for .Net Core
- Search for "CoreCompat.Selenium.WebDriver" library and install it

### Create NUnit test
Click on *Create new project*
![](https://github.com/sayems/csharp.webdriver/blob/master/docs/images/start-page.png)

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


### Add NUnit test from Solution Explorer

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

Congratulations! You have just created your first NUnit test!

###Contributions


### Issues
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)



### Contributions

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

##Feedback
## Feedback

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.


##Contact
## Contact

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.
19 changes: 19 additions & 0 deletions Selenium/Selenium.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="System.Diagnostics.Contracts" Version="4.3.0" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Selenium/Selenium.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Selenium", "Selenium.csproj", "{E45A9547-F4BA-4DE4-9D29-875DD8D54794}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E45A9547-F4BA-4DE4-9D29-875DD8D54794}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
24 changes: 24 additions & 0 deletions Selenium/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"BaseUrl": "https://www.google.com/",
"Username": "sayem",
"Password": "Passw0rd!",

"RemoteBrowser": "false",

"Browser": "Chrome",

"Platform": "Windows 7",

"UseSeleniumGrid": "false",
"GridHubUrl": "http://localhost:4444/wd/hub",

"UseSauceLabs": "true",
"SauceLabsUsername": "ssayem",
"SauceLabsAccessKey": "",
"SauceLabsHubUrl": "http://ondemand.saucelabs.com:80/wd/hub",

"UseBrowserstack": "false",
"BrowserStackUsername": "ssayem",
"BrowserStackAccessKey": "",
"BrowserStackHubUrl": "http://hub-cloud.browserstack.com/wd/hub/"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using NUnit.Framework;
using Selenium.config;
using Selenium.core.browsers;

namespace Selenium.core
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using Selenium.config;

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

public IBrowser GetBrowser(BrowserType type)
{
if ((Config.RemoteBrowser && Config.UseSauceLabs) || (Config.RemoteBrowser && Config.UseBrowserstack) ||
(Config.RemoteBrowser && Config.UseSeleniumGrid))
return _browsers[BrowserType.Remote].Invoke();
return _browsers.ContainsKey(type) ? _browsers[type].Invoke() : _browsers[BrowserType.Firefox].Invoke();
}

private IBrowser Remote()
{
return Create<RemoteWebDriver>();
Expand All @@ -48,5 +39,16 @@ private IBrowser Firefox()
{
return Create<FirefoxDriver>();
}

public IBrowser GetBrowser(BrowserType type)
{
return Config.RemoteBrowser && Config.UseSauceLabs ||
Config.RemoteBrowser && Config.UseBrowserstack ||
Config.RemoteBrowser && Config.UseSeleniumGrid
? _browsers[BrowserType.Remote].Invoke()
: (_browsers.ContainsKey(type)
? _browsers[type].Invoke()
: _browsers[BrowserType.Firefox].Invoke());
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using Selenium.config;

namespace Selenium.core.browsers
{
Expand All @@ -15,24 +14,23 @@ public sealed class BrowserFactory :
{
IBrowser<ChromeDriver> IBrowserWebDriver<ChromeDriver>.Create()
{
var dirName = AppDomain.CurrentDomain.BaseDirectory;
var dirName = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin", StringComparison.Ordinal));
var fileInfo = new FileInfo(dirName);
var parentDir = fileInfo.Directory?.Parent;
var parentDirName = parentDir?.FullName;
return new BrowserAdapter<ChromeDriver>(new ChromeDriver(parentDirName + @"\libs"), BrowserType.Firefox);
var parentDirName = fileInfo?.FullName;
return new BrowserAdapter<ChromeDriver>(new ChromeDriver(parentDirName + @"libs"), BrowserType.Chrome);
}

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

// TODO - Fix Remote WebDriver
IBrowser<RemoteWebDriver> IBrowserWebDriver<RemoteWebDriver>.Create()
{
DesiredCapabilities capabilities;
Expand All @@ -41,10 +39,10 @@ IBrowser<RemoteWebDriver> IBrowserWebDriver<RemoteWebDriver>.Create()
switch (Config.Browser)
{
case BrowserType.Chrome:
capabilities = DesiredCapabilities.Chrome();
capabilities = new DesiredCapabilities();
break;
case BrowserType.Firefox:
capabilities = DesiredCapabilities.Firefox();
capabilities = new DesiredCapabilities();
break;
default:
throw new ArgumentOutOfRangeException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;

namespace Selenium.core.browsers
namespace Selenium.core.browsers
{
[Serializable]
public enum BrowserType
{
Firefox,
Expand Down
44 changes: 44 additions & 0 deletions Selenium/core/browsers/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace Selenium.core.browsers
{
public class Config
{
public static bool RemoteBrowser => bool.Parse(GetValue("RemoteBrowser"));

public static BrowserType Browser
=> (BrowserType) Enum.Parse(typeof(BrowserType), GetValue("Browser"));

public static string Platform => GetValue("Platform");
public static string BaseUrl => GetValue("BaseUrl");
public static string Username => GetValue("Username");
public static string Password => GetValue("Password");

public static bool UseSeleniumGrid => bool.Parse(GetValue("UseSeleniumGrid"));
public static string GridHubUri => GetValue("GridHubUrl");

public static bool UseSauceLabs => bool.Parse(GetValue("UseSauceLabs"));
public static string SauceLabsHubUri => GetValue("SauceLabsHubUrl");
public static string SauceLabsUsername => GetValue("SauceLabsUsername");
public static string SauceLabsAccessKey => GetValue("SauceLabsAccessKey");

public static bool UseBrowserstack => bool.Parse(GetValue("BrowserStack"));
public static string BrowserStackHubUrl => GetValue("BrowserStackHubUrl");
public static string BrowserStackUsername => GetValue("BrowserStackUsername");
public static string BrowserStackAccessKey => GetValue("BrowserStackAccessKey");

private static string GetValue(string value)
{
var dirName = AppContext.BaseDirectory.Substring(0, AppContext.BaseDirectory.IndexOf("bin"));
var fileInfo = new FileInfo(dirName);
var parentDirName = fileInfo?.FullName;

var builder = new ConfigurationBuilder()
.SetBasePath(parentDirName)
.AddJsonFile("appsettings.json");
return builder.Build()[value];
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added Selenium/libs/chromedriver
Binary file not shown.
43 changes: 43 additions & 0 deletions Selenium/pages/HomePage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Selenium.core.browsers;

namespace Selenium.pages
{
public class HomePage
{
private readonly IBrowser _browser;

public HomePage(IBrowser browser)
{
_browser = browser;
}

public RegisterPage Register()
{
Console.WriteLine("Navigating to Register page");
_browser.Page.GoToUrl("http://www.google.com");
return new RegisterPage(_browser);
}

public RegisterPage OpenYahooPage()
{
Console.WriteLine("Navigating to Yahoo page");
_browser.Page.GoToUrl("http://www.yahoo.com");
return new RegisterPage(_browser);
}

public RegisterPage OpenGooglePage()
{
Console.WriteLine("Navigating to Google page");
_browser.Page.GoToUrl("http://www.google.com");
return new RegisterPage(_browser);
}

public RegisterPage OpenGithubPage()
{
Console.WriteLine("Navigating to Github page");
_browser.Page.GoToUrl("http://www.github.com");
return new RegisterPage(_browser);
}
}
}
File renamed without changes.
18 changes: 18 additions & 0 deletions Selenium/tests/GithubTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NUnit.Framework;
using Selenium.core;
using Selenium.pages;

namespace Selenium.tests
{
[TestFixture]
[Parallelizable]
public class GithubTest : TestBase
{
[Test]
public void TestMethod()
{
var page = new HomePage(Driver);
page.OpenGithubPage();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Selenium.tests
{
[TestFixture]
[Parallelizable]
public class GoogleSearch : TestBase
{
[Test]
Expand Down
Loading

0 comments on commit 541360d

Please sign in to comment.