@@ -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}
0 commit comments