-
Notifications
You must be signed in to change notification settings - Fork 25
/
BrowserStack.cs
111 lines (94 loc) · 3.12 KB
/
BrowserStack.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using System.Configuration;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System.Collections.Specialized;
using BrowserStack;
using System.IO;
namespace SpecFlow_BrowserStack
{
[Binding]
public sealed class BrowserStack
{
private readonly ScenarioContext _scenarioContext;
private BrowserStackDriver bsDriver;
private string[] tags;
public BrowserStack(ScenarioContext context)
{
_scenarioContext = context;
}
[BeforeScenario]
public void BeforeScenario()
{
bsDriver = new BrowserStackDriver(_scenarioContext);
_scenarioContext["bsDriver"] = bsDriver;
}
[AfterScenario]
public void AfterScenario()
{
bsDriver.Cleanup();
}
}
public class BrowserStackDriver
{
private IWebDriver driver;
private Local browserStackLocal;
private string profile;
private string environment;
private ScenarioContext context;
public BrowserStackDriver(ScenarioContext context)
{
this.context = context;
}
public IWebDriver Init(string profile, string environment)
{
NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection;
DesiredCapabilities capability = new DesiredCapabilities();
foreach (string key in caps.AllKeys)
{
capability.SetCapability(key, caps[key]);
}
foreach (string key in settings.AllKeys)
{
capability.SetCapability(key, settings[key]);
}
String username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
if (username == null)
{
username = ConfigurationManager.AppSettings.Get("user");
}
String accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
if (accesskey == null)
{
accesskey = ConfigurationManager.AppSettings.Get("key");
}
capability.SetCapability("browserstack.user", username);
capability.SetCapability("browserstack.key", accesskey);
if (capability.GetCapability("browserstack.local") != null && capability.GetCapability("browserstack.local").ToString() == "true")
{
browserStackLocal = new Local();
List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("key", accesskey),
new KeyValuePair<string, string>("logfile", Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack\\local.log"))
};
browserStackLocal.start(bsLocalArgs);
}
driver = new RemoteWebDriver(new Uri("https://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub/"), capability);
return driver;
}
public void Cleanup()
{
driver.Quit();
if (browserStackLocal != null)
{
browserStackLocal.stop();
}
}
}
}