Skip to content

Commit dade7f8

Browse files
Merge pull request #21 from browserstack/merge_sdk_to_main
Merge sdk to main
2 parents 5f4d700 + 493dac2 commit dade7f8

12 files changed

Lines changed: 170 additions & 299 deletions

.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+
BrowserStackLocal.exe
11+
local.log

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@
33

44
<img src="https://github.com/browserstack/cucumber-java-browserstack/blob/master/src/test/resources/img/browserstack.png?raw=true" width="60" height="60" alt="BrowserStack" > <img src="https://xunit.net/images/full-logo.svg" width="150" height="60" alt="XUnit">
55

6-
## Setup
6+
## Run Sample Build
77
* Clone the repo
8-
* Install dependencies `dotnet build`
9-
* Update `config.json` files inside the `XUnit-BrowserStack` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings).
10-
11-
## Running your tests
12-
* To run tests, run `dotnet test --filter "profile=parallel"`
13-
* To run local tests, run `dotnet test --filter "profile=local"`
8+
* Open the solution `XUnit-BrowserStack.sln` in Visual Studio
9+
* Build the solution
10+
* Update `browserstack.yml` file with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
11+
### Running your tests from CLI
12+
* To run the test suite having cross-platform with parallelization, dotnet test --filter "Category=sample-test"
13+
* To run local tests, dotnet test --filter "Category=sample-local-test"
14+
### Running your tests from Test Explorer
15+
- To run a parallel tests, run test with fixture `sample-test`
16+
- To run local tests, run test with fixture `sample-local-test`
1417

1518
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
1619

20+
## Integrate your test suite
21+
22+
This repository uses the BrowserStack SDK to run tests on BrowserStack. Follow the steps below to install the SDK in your test suite and run tests on BrowserStack:
23+
24+
* Create sample browserstack.yml file with the browserstack related capabilities with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) and place it in your root folder.
25+
* Add nuget library BrowserStack.TestAdapter
26+
```sh
27+
dotnet add BrowserStack.TestAdapter
28+
```
29+
* Build project `dotnet build`
30+
1731
## Notes
1832
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
1933
* To test on a different set of browsers, check out our [platform configurator](https://www.browserstack.com/automate/c-sharp#setting-os-and-browser)
@@ -25,14 +39,20 @@
2539
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
2640
```
2741

28-
* For Windows:
42+
* For Windows Cmd:
2943
```
3044
set BROWSERSTACK_USERNAME=<browserstack-username>
3145
set BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
3246
```
3347

48+
* For Windows Powershell:
49+
```
50+
$env:BROWSERSTACK_USERNAME=<browserstack-username>
51+
$env:BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
52+
```
53+
3454
## Addtional Resources
3555
* [Documentation for writing Automate test scripts in C#](https://www.browserstack.com/docs/automate/selenium/getting-started/c-sharp)
3656
* [Customizing your tests on BrowserStack](https://www.browserstack.com/automate/capabilities)
3757
* [Browsers & mobile devices for selenium testing on BrowserStack](https://www.browserstack.com/list-of-browsers-and-platforms?product=automate)
38-
* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api)
58+
* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {}
5+
}

XUnit-BrowserStack/BaseFixture.cs

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,117 +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-
// Start Local if browserstack.local is set to true
49-
if (profile.Equals("local") && accessKey is not null)
50-
{
51-
bstackOptions["local"] = true;
52-
browserStackLocal = new Local();
53-
List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
54-
new KeyValuePair<string, string>("key", accessKey)
55-
};
56-
foreach (var localOption in config.GetValue("localOptions") as JObject)
57-
{
58-
if (localOption.Value is not null)
59-
{
60-
bsLocalArgs.Add(new KeyValuePair<string, string>(localOption.Key, localOption.Value.ToString()));
61-
}
62-
}
63-
browserStackLocal.start(bsLocalArgs);
64-
}
65-
66-
capabilities["bstack:options"] = bstackOptions;
6716

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

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

8223
return WebDriver;
8324
}
8425

85-
public void SetStatus(bool passed)
86-
{
87-
if(WebDriver is not null)
88-
{
89-
if (passed)
90-
((IJavaScriptExecutor)WebDriver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"Test Passed!\"}}");
91-
else
92-
((IJavaScriptExecutor)WebDriver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Test Failed!\"}}");
93-
}
94-
}
95-
96-
static DriverOptions getBrowserOption(String browser)
97-
{
98-
switch (browser)
99-
{
100-
case "chrome":
101-
return new OpenQA.Selenium.Chrome.ChromeOptions();
102-
case "firefox":
103-
return new OpenQA.Selenium.Firefox.FirefoxOptions();
104-
case "safari":
105-
return new OpenQA.Selenium.Safari.SafariOptions();
106-
case "edge":
107-
return new OpenQA.Selenium.Edge.EdgeOptions();
108-
default:
109-
return new OpenQA.Selenium.Chrome.ChromeOptions();
110-
}
111-
}
112-
11326
public void Dispose()
11427
{
11528
if (WebDriver is not null)
11629
{
11730
WebDriver.Quit();
11831
WebDriver.Dispose();
11932
}
120-
if(browserStackLocal is not null)
121-
{
122-
browserStackLocal.stop();
123-
}
12433
}
12534
}
12635
}

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("Category", "sample-local-test")]
19+
public void BStackLocalTest()
20+
{
21+
RemoteWebDriver driver = baseFixture.GetDriver("chrome", "local");
22+
driver.Navigate().GoToUrl("http://bs-local.com:45454/");
23+
Assert.Contains("BrowserStack Local", driver.Title);
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("Category", "sample-test")]
20+
public void BStackSampleTest()
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+
}

0 commit comments

Comments
 (0)