forked from nikolay-advolodkin/dot-net-sauce
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSelenium4SauceTests.cs
154 lines (139 loc) · 5.75 KB
/
Selenium4SauceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;
namespace Selenium4.MsTest.Scripts
{
[TestClass]
[TestCategory("WebDriver 4 tests on Sauce")]
public class Selenium4SauceTests
{
IWebDriver _driver;
private string sauceUserName;
private string sauceAccessKey;
private Dictionary<string, object> sauceOptions;
public TestContext TestContext { get; set; }
[TestCleanup]
public void CleanUpAfterEveryTestMethod()
{
var passed = TestContext.CurrentTestOutcome == UnitTestOutcome.Passed;
if (_driver == null) { return;}
((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
_driver.Quit();
}
[TestMethod]
public void EdgeW3C()
{
var options = new EdgeOptions
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
//AcceptInsecureCertificates = true //Insecure Certs are Not supported by Edge
};
sauceOptions.Add("name", MethodBase.GetCurrentMethod().Name);
options.AddAdditionalOption("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"), options.ToCapabilities(),
TimeSpan.FromSeconds(30));
GoToThenAssert();
}
[TestMethod]
public void IEW3C()
{
var options = new InternetExplorerOptions
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
//AcceptInsecureCertificates = true //Insecure Certs are Not supported by Edge
};
sauceOptions.Add("name", MethodBase.GetCurrentMethod().Name);
options.AddAdditionalOption("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"), options.ToCapabilities(),
TimeSpan.FromSeconds(30));
GoToThenAssert();
}
private void GoToThenAssert()
{
_driver.Navigate().GoToUrl("https://www.saucedemo.com");
Assert.IsTrue(_driver.Url.Contains("saucedemo.com"));
}
[TestMethod]
public void ChromeW3C()
{
var chromeOptions = new ChromeOptions
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
};
sauceOptions.Add("name", MethodBase.GetCurrentMethod().Name);
chromeOptions.AddAdditionalOption("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"),
chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(30));
GoToThenAssert();
}
[TestMethod]
public void SafariW3C()
{
SafariOptions safariOptions = new SafariOptions
{
BrowserVersion = "12.0",
PlatformName = "macOS 10.13"
//AcceptInsecureCertificates = true Don't use this as Safari doesn't support Insecure certs
};
sauceOptions.Add("name", MethodBase.GetCurrentMethod().Name);
safariOptions.AddAdditionalOption("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"),
safariOptions.ToCapabilities(), TimeSpan.FromSeconds(30));
GoToThenAssert();
}
[TestMethod]
public void FirefoxW3C()
{
var browserOptions = new FirefoxOptions
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
};
sauceOptions.Add("name", MethodBase.GetCurrentMethod().Name);
browserOptions.AddAdditionalOption("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"),
browserOptions.ToCapabilities(), TimeSpan.FromSeconds(30));
_driver.Navigate().GoToUrl("https://www.saucedemo.com");
GoToThenAssert();
}
[TestMethod]
public void EdgeChromiumW3C()
{
var browserOptions = new EdgeOptions()
{
BrowserVersion = "latest",
PlatformName = "macOS 10.14"
};
sauceOptions.Add("name", MethodBase.GetCurrentMethod().Name);
browserOptions.AddAdditionalOption("sauce:options", sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"),
browserOptions.ToCapabilities(), TimeSpan.FromSeconds(30));
_driver.Navigate().GoToUrl("https://www.saucedemo.com");
GoToThenAssert();
}
[TestInitialize]
public void SetupTests()
{
//TODO please supply your Sauce Labs user name in an environment variable
sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User);
//TODO please supply your own Sauce Labs access Key in an environment variable
sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);
sauceOptions = new Dictionary<string, object>
{
["username"] = sauceUserName,
["accessKey"] = sauceAccessKey
};
}
}
}