11package com .lambdatest ;
22
3- import io .appium .java_client .MobileBy ;
3+ import io .appium .java_client .AppiumBy ;
4+ import io .appium .java_client .android .AndroidDriver ;
45import org .junit .After ;
56import org .junit .Before ;
67import org .junit .Test ;
7- import org .openqa .selenium .remote .DesiredCapabilities ;
8- import org .openqa .selenium .remote .RemoteWebDriver ;
98import org .openqa .selenium .By ;
10-
9+ import org .openqa .selenium .WebElement ;
10+ import org .openqa .selenium .remote .DesiredCapabilities ;
1111import org .openqa .selenium .support .ui .ExpectedConditions ;
1212import org .openqa .selenium .support .ui .WebDriverWait ;
1313
1414import java .net .URL ;
15+ import java .time .Duration ;
16+ import java .util .HashMap ;
1517
1618public class android {
17- String username = System .getenv ("LT_USERNAME" ) == null ? "LT_USERNAME" : System .getenv ("LT_USERNAME" ); //Enter the Username here
18- String accessKey = System .getenv ("LT_ACCESS_KEY" ) == null ? "LT_ACCESS_KEY" : System .getenv ("LT_ACCESS_KEY" ); //Enter the Access key here
19- public String app_id = System .getenv ("LT_APP_ID" ) == null ? "lt://proverbial-android" : System .getenv ("LT_APP_ID" ); //Enter your LambdaTest App ID at the place of lt://proverbial-android
20- public String grid_url = System .getenv ("LT_GRID_URL" ) == null ? "mobile-hub.lambdatest.com" : System .getenv ("LT_GRID_URL" );
19+ String username = System .getenv ("LT_USERNAME" ) == null ? "YOUR_USERNAME" : System .getenv ("LT_USERNAME" );
20+ String accessKey = System .getenv ("LT_ACCESS_KEY" ) == null ? "YOUR_ACCESS_KEY" : System .getenv ("LT_ACCESS_KEY" );
2121 public String status = "passed" ;
2222
23- public static RemoteWebDriver driver = null ;
23+ // Fix 1: Use AndroidDriver instead of RemoteWebDriver
24+ public static AndroidDriver driver = null ;
2425
2526 @ Before
2627 public void setUp () throws Exception {
2728 DesiredCapabilities capabilities = new DesiredCapabilities ();
2829
29- capabilities .setCapability ("build" , "JUNIT Native App automation" );
30- capabilities .setCapability ("name" , "Java JUnit Android" );
31- capabilities .setCapability ("platformName" , "android" );
32- capabilities .setCapability ("deviceName" , "Pixel.*" ); //Enter the name of the device here
33- capabilities .setCapability ("isRealMobile" , true );
34- capabilities .setCapability ("platformVersion" , "12" );
35- capabilities .setCapability ("app" , app_id ); //Enter the App ID here
36- capabilities .setCapability ("deviceOrientation" , "PORTRAIT" );
37- capabilities .setCapability ("network" , false );
38- capabilities .setCapability ("visual" , true );
39- capabilities .setCapability ("autoGrantPermissions" , true );
40-
41- driver = new RemoteWebDriver (new URL ("https://" + username + ":" + accessKey + "@" + grid_url + "/wd/hub" ),
30+ // Fix 2: Use lt:options for W3C compliance
31+ HashMap <String , Object > ltOptions = new HashMap <String , Object >();
32+ ltOptions .put ("w3c" , true );
33+ ltOptions .put ("platformName" , "android" );
34+ ltOptions .put ("deviceName" , "Pixel.*" );
35+ ltOptions .put ("platformVersion" , "12" );
36+ ltOptions .put ("isRealMobile" , true );
37+ ltOptions .put ("app" , "lt://APP10160622431766424164986229" );
38+ ltOptions .put ("build" , "JUNIT Native App automation" );
39+ ltOptions .put ("name" , "Java JUnit Android" );
40+ ltOptions .put ("visual" , true );
41+ ltOptions .put ("console" , true );
42+ ltOptions .put ("autoGrantPermissions" , true );
43+
44+ capabilities .setCapability ("lt:options" , ltOptions );
45+
46+ driver = new AndroidDriver (
47+ new URL ("https://" + username + ":" + accessKey + "@mobile-hub.lambdatest.com/wd/hub" ),
4248 capabilities );
4349 }
4450
4551 @ Test
4652 public void testSimple () throws Exception {
4753 try {
48- WebDriverWait wait = new WebDriverWait (driver , 30 );
49- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("color" ))).click ();
50- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("geoLocation" ))).click ();
51- Thread .sleep (5000 );
54+ // Fix 3: Use Duration for WebDriverWait in Selenium 4
55+ WebDriverWait wait = new WebDriverWait (driver , Duration .ofSeconds (30 ));
56+
57+ // Fix 4: Use AppiumBy instead of MobileBy
58+ wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("color" ))).click ();
59+ wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("geoLocation" ))).click ();
60+
61+ Thread .sleep (2000 );
62+
63+ // Fix 5: Proper Mobile Back Navigation
5264 driver .navigate ().back ();
53- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("Text" ))).click ();
54- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("notification" ))).click ();
55- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("toast" ))).click ();
56- wait .until (ExpectedConditions .elementToBeClickable (By .id ("webview" ))).click ();
57- Thread .sleep (10000 );
58- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("url" ))).sendKeys ("https://www.lambdatest.com/" );
59- wait .until (ExpectedConditions .elementToBeClickable (MobileBy .id ("find" ))).click ();
60- Thread .sleep (5000 );
65+
66+ wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("Text" ))).click ();
67+ wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("notification" ))).click ();
68+ wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("toast" ))).click ();
69+
70+ // Interaction with WebView
71+ wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("webview" ))).click ();
72+
73+ WebElement urlField = wait .until (ExpectedConditions .elementToBeClickable (AppiumBy .id ("url" )));
74+ urlField .sendKeys ("https://www.lambdatest.com/" );
75+
76+ driver .findElement (AppiumBy .id ("find" )).click ();
77+
78+ Thread .sleep (2000 );
6179 driver .navigate ().back ();
80+
6281 status = "passed" ;
6382 } catch (Exception e ) {
64- System .out .println (e .getMessage ());
83+ System .out .println ("Test Error: " + e .getMessage ());
6584 status = "failed" ;
85+ throw e ;
6686 }
6787 }
6888
@@ -73,5 +93,4 @@ public void tearDown() throws Exception {
7393 driver .quit ();
7494 }
7595 }
76- }
77-
96+ }
0 commit comments