-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathNUnitSeleniumTests.cs
117 lines (98 loc) · 4.8 KB
/
NUnitSeleniumTests.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
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using NUnit.Framework;
using System.Threading;
using System.Collections.Generic;
namespace NUnitSelenium
{
[TestFixture("chrome", "60", "Windows 10")]
[TestFixture("internet explorer", "11", "Windows 7")]
[TestFixture("firefox", "60", "Windows 7")]
[TestFixture("chrome", "71", "Windows 7")]
[TestFixture("internet explorer", "11", "Windows 10")]
[TestFixture("firefox", "58", "Windows 7")]
[TestFixture("chrome", "67", "Windows 7")]
[TestFixture("internet explorer", "10", "Windows 7")]
[TestFixture("firefox", "55", "Windows 7")]
[Parallelizable(ParallelScope.Children)]
public class NUnitSeleniumSample
{
public static string LT_USERNAME = Environment.GetEnvironmentVariable("LT_USERNAME") ==null ? "your username" : Environment.GetEnvironmentVariable("LT_USERNAME");
public static string LT_ACCESS_KEY = Environment.GetEnvironmentVariable("LT_ACCESS_KEY") == null ? "your accessKey" : Environment.GetEnvironmentVariable("LT_ACCESS_KEY");
public static bool tunnel = Boolean.Parse(Environment.GetEnvironmentVariable("LT_TUNNEL")== null ? "false" : Environment.GetEnvironmentVariable("LT_TUNNEL"));
public static string build = Environment.GetEnvironmentVariable("LT_BUILD") == null ? "your build name" : Environment.GetEnvironmentVariable("LT_BUILD");
public static string seleniumUri = "https://hub.lambdatest.com:443/wd/hub";
ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
private String browser;
private String version;
private String os;
public NUnitSeleniumSample(String browser, String version, String os)
{
this.browser = browser;
this.version = version;
this.os = os;
}
[SetUp]
public void Init()
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(CapabilityType.BrowserName, browser);
capabilities.SetCapability(CapabilityType.Version, version);
capabilities.SetCapability(CapabilityType.Platform, os);
//Requires a named tunnel.
if (tunnel)
{
capabilities.SetCapability("tunnel", tunnel);
}
if (build != null)
{
capabilities.SetCapability("build", build);
}
capabilities.SetCapability("user", LT_USERNAME);
capabilities.SetCapability("accessKey", LT_ACCESS_KEY);
capabilities.SetCapability("name",
String.Format("{0}:{1}",
TestContext.CurrentContext.Test.ClassName,
TestContext.CurrentContext.Test.MethodName));
driver.Value = new RemoteWebDriver(new Uri(seleniumUri), capabilities, TimeSpan.FromSeconds(600));
Console.Out.WriteLine(driver);
}
[Test]
public void Todotest()
{
{
Console.WriteLine("Navigating to todos app.");
driver.Value.Navigate().GoToUrl("https://lambdatest.github.io/sample-todo-app/");
driver.Value.FindElement(By.Name("li4")).Click();
Console.WriteLine("Clicking Checkbox");
driver.Value.FindElement(By.Name("li5")).Click();
// If both clicks worked, then te following List should have length 2
IList<IWebElement> elems = driver.Value.FindElements(By.ClassName("done-true"));
// so we'll assert that this is correct.
Assert.AreEqual(2, elems.Count);
Console.WriteLine("Entering Text");
driver.Value.FindElement(By.Id("sampletodotext")).SendKeys("Yey, Let's add it to list");
driver.Value.FindElement(By.Id("addbutton")).Click();
// lets also assert that the new todo we added is in the list
string spanText = driver.Value.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span")).Text;
Assert.AreEqual("Yey, Let's add it to list", spanText);
}
}
[TearDown]
public void Cleanup()
{
bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
try
{
// Logs the result to LambdaTest
((IJavaScriptExecutor)driver.Value).ExecuteScript("lambda-status=" + (passed ? "passed" : "failed"));
}
finally
{
// Terminates the remote webdriver session
driver.Value.Quit();
}
}
}
}