forked from nikolay-advolodkin/dot-net-sauce
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSelenium4Demo.cs
69 lines (55 loc) · 2.42 KB
/
Selenium4Demo.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
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace Core.Selenium.Examples
{
[TestClass]
public class Selenium4Demo
{
public IWebDriver Driver { get; set; }
public TestContext TestContext { get; set; }
[TestInitialize]
public void Setup()
{
var browserOptions = new ChromeOptions();
browserOptions.PlatformName = "Windows 10";
browserOptions.BrowserVersion = "latest";
var sauceOptions = new Dictionary<string, object>();
sauceOptions.Add("name", TestContext.TestName);
sauceOptions.Add("username", Environment.GetEnvironmentVariable("SAUCE_USERNAME"));
sauceOptions.Add("accessKey", Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY"));
browserOptions.AddAdditionalOption("sauce:options", sauceOptions);
var sauceUrl = new Uri("https://ondemand.us-west-1.saucelabs.com/wd/hub");
Driver = new RemoteWebDriver(sauceUrl, browserOptions);
}
[TestMethod]
public void LoginTest()
{
Driver.Navigate().GoToUrl("https://www.saucedemo.com");
var usernameLocator = By.CssSelector("#user-name");
var passwordLocator = By.CssSelector("#password");
var submitLocator = By.CssSelector(".btn_action");
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(15));
wait.Until(drv => drv.FindElement(usernameLocator));
var usernameElement = Driver.FindElement(usernameLocator);
var passwordElement = Driver.FindElement(passwordLocator);
var submitElement = Driver.FindElement(submitLocator);
usernameElement.SendKeys("standard_user");
passwordElement.SendKeys("secret_sauce");
submitElement.Click();
Assert.AreEqual("https://www.saucedemo.com/inventory.html", Driver.Url);
}
[TestCleanup]
public void Teardown()
{
var isPassed = TestContext.CurrentTestOutcome == UnitTestOutcome.Passed;
var script = "sauce:job-result=" + (isPassed ? "passed" : "failed");
((IJavaScriptExecutor) Driver).ExecuteScript(script);
Driver?.Quit();
}
}
}