1717
1818public class BrowserStackAndroidLocal {
1919
20- private static Local localInstance ;
21- public static String accessKey = "BROWSERSTACK_USERNAME" ;
22- public static String userName = "BROWSERSTACK_ACCESS_KEY" ;
23-
20+ private static Local localInstance ;
21+ public static String userName = "YOUR_USERNAME" ;
22+ public static String accessKey = "YOUR_ACCESS_KEY" ;
2423
25- public static void setupLocal () throws Exception {
26- localInstance = new Local ();
27- Map <String , String > options = new HashMap <String , String >();
28- options .put ("key" , accessKey );
29- localInstance .start (options );
30- }
31-
32- public static void tearDownLocal () throws Exception {
33- localInstance .stop ();
34- }
3524
25+ public static void setupLocal () throws Exception {
26+ localInstance = new Local ();
27+ Map <String , String > options = new HashMap <String , String >();
28+ options .put ("key" , accessKey );
29+ localInstance .start (options );
30+ }
3631
32+ public static void tearDownLocal () throws Exception {
33+ localInstance .stop ();
34+ }
3735
3836 public static void main (String [] args ) throws Exception {
39- setupLocal ();
37+ // Start the BrowserStack Local binary
38+ setupLocal ();
39+
40+ DesiredCapabilities capabilities = new DesiredCapabilities ();
41+
42+ // Set your access credentials
43+ capabilities .setCapability ("browserstack.user" , userName );
44+ capabilities .setCapability ("browserstack.key" , accessKey );
45+
46+ // Set URL of the application under test
47+ capabilities .setCapability ("app" , "bs://<app-id>" );
48+
49+ // Specify device and os_version for testing
50+ capabilities .setCapability ("device" , "Google Pixel 3" );
51+ capabilities .setCapability ("os_version" , "9.0" );
4052
41- DesiredCapabilities capabilities = new DesiredCapabilities ();
53+ // Set the browserstack.local capability to true
54+ capabilities .setCapability ("browserstack.local" , true );
4255
43- capabilities .setCapability ("browserstack.local" , true );
44- capabilities .setCapability ("device" , "Samsung Galaxy S7" );
45- capabilities .setCapability ("app" , "bs://<hashed app-id>" );
56+ // Set other BrowserStack capabilities
57+ capabilities .setCapability ("project" , "First Java Project" );
58+ capabilities .setCapability ("build" , "Java Android Local" );
59+ capabilities .setCapability ("name" , "local_test" );
60+
4661
47- AndroidDriver <AndroidElement > driver = new AndroidDriver <AndroidElement >(new URL ("https://" +userName +":" +accessKey +"@hub.browserstack.com/wd/hub" ), capabilities );
62+ // Initialise the remote Webdriver using BrowserStack remote URL
63+ // and desired capabilities defined above
64+ AndroidDriver <AndroidElement > driver = new AndroidDriver <AndroidElement >(
65+ new URL ("http://hub.browserstack.com/wd/hub" ), capabilities );
4866
67+ // Test case for the BrowserStack sample Android Local app.
68+ // If you have uploaded your app, update the test case here.
4969 AndroidElement searchElement = (AndroidElement ) new WebDriverWait (driver , 30 ).until (
5070 ExpectedConditions .elementToBeClickable (MobileBy .id ("com.example.android.basicnetworking:id/test_action" )));
5171 searchElement .click ();
@@ -69,8 +89,10 @@ public static void main(String[] args) throws Exception {
6989 assert (matchedString .contains ("The active connection is wifi" ));
7090 assert (matchedString .contains ("Up and running" ));
7191
92+ // Invoke driver.quit() after the test is done to indicate that the test is completed.
7293 driver .quit ();
7394
95+ // Stop the BrowserStack Local binary
7496 tearDownLocal ();
7597
7698 }
0 commit comments