Skip to content

Commit 2600fbd

Browse files
committed
update: refactor for sdk
1 parent d432cbf commit 2600fbd

File tree

10 files changed

+157
-288
lines changed

10 files changed

+157
-288
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ TestResult.xml
44
bin
55
obj
66
.vs
7-
.DS_Store
7+
.DS_Store
8+
browserstack.err
9+
log
10+
local.log
11+
BrowserStackLocal.exe
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"browserstack-sdk": {
6+
"version": "0.0.4",
7+
"commands": [
8+
"browserstack-sdk"
9+
]
10+
}
11+
}
12+
}

XUnit-BrowserStack/BaseFixture.cs

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -10,120 +10,26 @@ public class BaseFixture : IDisposable
1010
{
1111

1212
private RemoteWebDriver? WebDriver;
13-
private Local? browserStackLocal;
14-
1513

1614
public RemoteWebDriver GetDriver(string platform, string profile)
1715
{
18-
// Get Configuration for correct profile
19-
string currentDirectory = Directory.GetCurrentDirectory();
20-
string path = Path.Combine(currentDirectory, "config.json");
21-
JObject config = JObject.Parse(File.ReadAllText(path));
22-
if (config is null)
23-
throw new Exception("Configuration not found!");
24-
25-
// Get Platform specific capabilities
26-
JObject capabilities = config.GetValue("environments").Where(x => x is JObject y && x["browserName"].ToString().Equals(platform)).ToList()[0] as JObject;
27-
28-
// Get Common Capabilities
29-
JObject commonCapabilities = config.GetValue("capabilities") as JObject;
30-
31-
// Merge Capabilities
32-
capabilities.Merge(commonCapabilities);
33-
34-
JObject bstackOptions = capabilities["bstack:options"] as JObject;
35-
36-
// Get username and accesskey
37-
string? username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
38-
if (username is null)
39-
username = config.GetValue("user").ToString();
40-
41-
string? accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
42-
if (accessKey is null)
43-
accessKey = config.GetValue("key").ToString();
44-
45-
bstackOptions["userName"] = username;
46-
bstackOptions["accessKey"] = accessKey;
47-
48-
// Add session name
49-
bstackOptions["sessionName"] = $"{profile}_test";
50-
51-
// Start Local if browserstack.local is set to true
52-
if (profile.Equals("local") && accessKey is not null)
53-
{
54-
bstackOptions["local"] = true;
55-
browserStackLocal = new Local();
56-
List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
57-
new KeyValuePair<string, string>("key", accessKey)
58-
};
59-
foreach (var localOption in config.GetValue("localOptions") as JObject)
60-
{
61-
if (localOption.Value is not null)
62-
{
63-
bsLocalArgs.Add(new KeyValuePair<string, string>(localOption.Key, localOption.Value.ToString()));
64-
}
65-
}
66-
browserStackLocal.start(bsLocalArgs);
67-
}
68-
69-
capabilities["bstack:options"] = bstackOptions;
7016

7117
// Create Desired Cappabilities for WebDriver
72-
DriverOptions desiredCapabilities = getBrowserOption(capabilities.GetValue("browserName").ToString());
73-
capabilities.Remove("browserName");
74-
foreach (var x in capabilities)
75-
{
76-
if (x.Key.Equals("bstack:options"))
77-
desiredCapabilities.AddAdditionalOption(x.Key, x.Value.ToObject<Dictionary<string, object>>());
78-
else
79-
desiredCapabilities.AddAdditionalOption(x.Key, x.Value);
80-
}
18+
DriverOptions desiredCapabilities = new OpenQA.Selenium.Chrome.ChromeOptions();
8119

8220
// Create RemoteWebDriver instance
83-
WebDriver = new RemoteWebDriver(new Uri($"https://{config["server"]}/wd/hub"), desiredCapabilities);
21+
WebDriver = new RemoteWebDriver(new Uri($"http://localhost:4444/wd/hub"), desiredCapabilities);
8422

8523
return WebDriver;
8624
}
8725

88-
public void SetStatus(bool passed)
89-
{
90-
if(WebDriver is not null)
91-
{
92-
if (passed)
93-
((IJavaScriptExecutor)WebDriver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"Test Passed!\"}}");
94-
else
95-
((IJavaScriptExecutor)WebDriver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Test Failed!\"}}");
96-
}
97-
}
98-
99-
static DriverOptions getBrowserOption(String browser)
100-
{
101-
switch (browser)
102-
{
103-
case "chrome":
104-
return new OpenQA.Selenium.Chrome.ChromeOptions();
105-
case "firefox":
106-
return new OpenQA.Selenium.Firefox.FirefoxOptions();
107-
case "safari":
108-
return new OpenQA.Selenium.Safari.SafariOptions();
109-
case "edge":
110-
return new OpenQA.Selenium.Edge.EdgeOptions();
111-
default:
112-
return new OpenQA.Selenium.Chrome.ChromeOptions();
113-
}
114-
}
115-
11626
public void Dispose()
11727
{
11828
if (WebDriver is not null)
11929
{
12030
WebDriver.Quit();
12131
WebDriver.Dispose();
12232
}
123-
if(browserStackLocal is not null)
124-
{
125-
browserStackLocal.stop();
126-
}
12733
}
12834
}
12935
}

XUnit-BrowserStack/LocalTest.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

XUnit-BrowserStack/ParallelTest.cs

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Xunit;
2+
using OpenQA.Selenium.Remote;
3+
using System.Text.RegularExpressions;
4+
5+
namespace XUnit_BrowserStack
6+
{
7+
public class SampleLocalTest : IClassFixture<BaseFixture>
8+
{
9+
10+
private readonly BaseFixture baseFixture;
11+
12+
public SampleLocalTest(BaseFixture baseFixture)
13+
{
14+
this.baseFixture = baseFixture;
15+
}
16+
17+
[Fact]
18+
[Trait("profile", "sample_local_test")]
19+
public void Test()
20+
{
21+
RemoteWebDriver driver = baseFixture.GetDriver("chrome", "local");
22+
driver.Navigate().GoToUrl("http://bs-local.com:45454/");
23+
Assert.True(Regex.IsMatch(driver.Title, @"/BrowserStack Local/i", RegexOptions.IgnoreCase));
24+
}
25+
}
26+
}

XUnit-BrowserStack/SampleTest.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Xunit;
2+
using OpenQA.Selenium;
3+
using OpenQA.Selenium.Remote;
4+
using OpenQA.Selenium.Support.UI;
5+
6+
namespace XUnit_BrowserStack
7+
{
8+
public class SampleTest : IClassFixture<BaseFixture>
9+
{
10+
11+
private readonly BaseFixture baseFixture;
12+
13+
public SampleTest(BaseFixture baseFixture)
14+
{
15+
this.baseFixture = baseFixture;
16+
}
17+
18+
[Fact]
19+
[Trait("profile", "sample_test")]
20+
public void Test()
21+
{
22+
RemoteWebDriver driver = baseFixture.GetDriver("chrome", "single");
23+
WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(2000));
24+
driver.Manage().Window.Maximize();
25+
driver.Navigate().GoToUrl("https://bstackdemo.com/");
26+
Assert.Equal("StackDemo", driver.Title);
27+
28+
string productOnPageText = webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@id=\"1\"]/p"))).Text;
29+
30+
webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@id=\"1\"]/div[4]"))).Click();
31+
bool cartOpened = webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@class=\"float-cart__content\"]"))).Displayed;
32+
Assert.True(cartOpened);
33+
string productOnCartText = webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).Text;
34+
Assert.Equal(productOnCartText, productOnPageText);
35+
}
36+
}
37+
}

XUnit-BrowserStack/SingleTest.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)