forked from nikolay-advolodkin/dot-net-sauce
-
Notifications
You must be signed in to change notification settings - Fork 38
/
DesktopTests.cs
98 lines (85 loc) · 4.03 KB
/
DesktopTests.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using OpenQA.Selenium.Support.UI;
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;
namespace Core.Selenium.Examples
{
[TestClass]
public class DesktopTests
{
private IWebDriver _driver;
private string _sauceUserName;
private string _sauceAccessKey;
private Dictionary<string, object> _sauceOptions;
public TestContext TestContext { get; set; }
[TestCleanup]
public void CleanUpAfterEveryTestMethod()
{
if (_driver == null) return;
var passed = TestContext.CurrentTestOutcome == UnitTestOutcome.Passed;
((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
_driver.Quit();
}
[TestMethod]
public void EdgeW3C()
{
//TODO please set your Sauce Labs username/access key in an environment variable
_sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
// Do NOT use EnvironmentVariableTarget as it won't work in CI
_sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");
_sauceOptions = new Dictionary<string, object>
{
["username"] = _sauceUserName,
["accessKey"] = _sauceAccessKey,
["name"] = TestContext.TestName
};
var browserOptions = new EdgeOptions
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
//AcceptInsecureCertificates = true //Insecure Certs are Not supported by Edge
};
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");
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(6));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("#user-name")));
}
[TestMethod]
public void VisibilityTest()
{
//TODO please set your Sauce Labs username/access key in an environment variable
_sauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
// Do NOT use EnvironmentVariableTarget as it won't work in CI
_sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");
_sauceOptions = new Dictionary<string, object>
{
["username"] = _sauceUserName,
["accessKey"] = _sauceAccessKey,
["name"] = TestContext.TestName,
//Set the visibility of your test: https://docs.saucelabs.com/test-results/sharing-test-results/index.html
["public"] = "public"
};
var browserOptions = new EdgeOptions
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
//AcceptInsecureCertificates = true //Insecure Certs are Not supported by Edge
};
browserOptions.AddAdditionalOption("sauce:options", _sauceOptions);
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"), browserOptions.ToCapabilities(),
TimeSpan.FromSeconds(30));
var sessionId = ((RemoteWebDriver) _driver).SessionId;
_driver.Navigate().GoToUrl("https://www.saucedemo.com");
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(6));
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#user-name")));
// share this with your teams
var testUrl = "https://app.saucelabs.com/tests/" + sessionId;
}
}
}