|
1 | 1 | import io.appium.java_client.AppiumDriver; |
2 | 2 | import io.appium.java_client.MobileBy; |
3 | | -import io.appium.java_client.MobileElement; |
| 3 | +//import io.appium.java_client.WebElement; |
| 4 | +import org.openqa.selenium.WebElement; |
| 5 | +import io.appium.java_client.AppiumBy; |
| 6 | +import io.appium.java_client.android.AndroidDriver; |
4 | 7 | import org.openqa.selenium.JavascriptExecutor; |
5 | 8 | import org.openqa.selenium.remote.DesiredCapabilities; |
6 | 9 | import java.net.MalformedURLException; |
7 | 10 | import java.net.URL; |
| 11 | +import java.util.HashMap; |
8 | 12 |
|
9 | 13 | public class vanilla_android { |
10 | | - public static String userName = System.getenv("LT_USERNAME") == null ? "YOUR_USERNAME" // Add username here |
11 | | - : System.getenv("LT_USERNAME"); |
12 | | - public static String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "YOUR_ACCESS_KEY" // Add accessKey here |
13 | | - : System.getenv("LT_ACCESS_KEY"); |
| 14 | + public static String userName = System.getenv("LT_USERNAME") == null ? "YOUR_USERNAME" : System.getenv("LT_USERNAME"); |
| 15 | + public static String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "YOUR_ACCESS_KEY" : System.getenv("LT_ACCESS_KEY"); |
14 | 16 |
|
15 | | - private static AppiumDriver driver; |
| 17 | + static AppiumDriver driver; |
16 | 18 |
|
17 | 19 | public static void main(String args[]) throws MalformedURLException, InterruptedException { |
18 | 20 |
|
19 | 21 | try { |
20 | 22 | DesiredCapabilities capabilities = new DesiredCapabilities(); |
21 | | - capabilities.setCapability("deviceName", "Galaxy S20"); |
22 | | - capabilities.setCapability("platformVersion", "11"); |
23 | | - capabilities.setCapability("platformName", "Android"); |
24 | | - capabilities.setCapability("isRealMobile", true); |
25 | | - capabilities.setCapability("app", "APP_URL"); // Enter your app url |
26 | | - capabilities.setCapability("deviceOrientation", "PORTRAIT"); |
27 | | - capabilities.setCapability("build", "Java Vanilla - Android"); |
28 | | - capabilities.setCapability("name", "Sample Test Java"); |
29 | | - capabilities.setCapability("console", true); |
30 | | - capabilities.setCapability("network", false); |
31 | | - capabilities.setCapability("visual", true); |
32 | | - capabilities.setCapability("devicelog", true); |
33 | | - |
34 | | - driver = new AppiumDriver( |
| 23 | + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); |
| 24 | + ltOptions.put("deviceName", "Galaxy.*"); // You can specify a particular model, or use a wildcard. |
| 25 | + ltOptions.put("platformVersion", "14"); // Make sure this matches the device's OS version. |
| 26 | + ltOptions.put("platformName", "Android"); |
| 27 | + ltOptions.put("isRealMobile", true); // Ensures that the test runs on a real device. |
| 28 | + ltOptions.put("app", "lt://APP10160622431766424164986229"); // Your app URL from LambdaTest. |
| 29 | + ltOptions.put("deviceOrientation", "PORTRAIT"); |
| 30 | + ltOptions.put("build", "Java Vanilla - Android"); |
| 31 | + ltOptions.put("name", "Sample Test Java"); |
| 32 | + ltOptions.put("console", true); |
| 33 | + ltOptions.put("autoGrantPermissions", true); // Auto grants permissions on the app. |
| 34 | + ltOptions.put("network", false); |
| 35 | + ltOptions.put("visual", true); |
| 36 | + ltOptions.put("devicelog", true); |
| 37 | + capabilities.setCapability("lt:options", ltOptions); |
| 38 | + |
| 39 | + // Initialize Appium driver with the LambdaTest server URL and capabilities |
| 40 | + driver = new AndroidDriver( |
35 | 41 | new URL("https://" + userName + ":" + accessKey + "@mobile-hub.lambdatest.com/wd/hub"), |
36 | 42 | capabilities); |
37 | 43 |
|
38 | | - MobileElement color = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/color")); |
| 44 | + // Interact with elements using their IDs |
| 45 | + WebElement color = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/color")); |
39 | 46 | color.click(); |
40 | 47 |
|
41 | | - MobileElement text = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/Text")); |
42 | | - // Changes the text to proverbial |
| 48 | + WebElement text = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/Text")); |
43 | 49 | text.click(); |
44 | 50 |
|
45 | | - // toast is visible |
46 | | - MobileElement toast = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/toast")); |
| 51 | + // Interact with Toast message element |
| 52 | + WebElement toast = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/toast")); |
47 | 53 | toast.click(); |
48 | 54 |
|
49 | | - // notification is visible |
50 | | - MobileElement notification = (MobileElement) driver |
51 | | - .findElement(MobileBy.id("com.lambdatest.proverbial:id/notification")); |
| 55 | + // Interact with Notification element |
| 56 | + WebElement notification = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/notification")); |
52 | 57 | notification.click(); |
53 | 58 |
|
54 | | - // Open the geolocation page |
55 | | - MobileElement geo = (MobileElement) driver |
56 | | - .findElement(MobileBy.id("com.lambdatest.proverbial:id/geoLocation")); |
| 59 | + // Open the Geolocation page |
| 60 | + WebElement geo = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/geoLocation")); |
57 | 61 | geo.click(); |
58 | 62 | Thread.sleep(5000); |
59 | 63 |
|
60 | | - // takes back to home page |
61 | | - MobileElement el3 = (MobileElement) driver.findElementByAccessibilityId("Home"); |
62 | | - |
| 64 | + // Go back to the home page |
63 | 65 | driver.navigate().back(); |
64 | 66 | Thread.sleep(2000); |
65 | 67 |
|
66 | | - // Takes to speed test page |
67 | | - MobileElement speedtest = (MobileElement) driver |
68 | | - .findElement(MobileBy.id("com.lambdatest.proverbial:id/speedTest")); |
| 68 | + // Navigate to the Speed Test page |
| 69 | + WebElement speedtest = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/speedTest")); |
69 | 70 | speedtest.click(); |
70 | 71 | Thread.sleep(5000); |
71 | 72 |
|
72 | 73 | driver.navigate().back(); |
73 | 74 |
|
74 | | - // Opens the browser |
75 | | - MobileElement browser = (MobileElement) driver.findElement(MobileBy.AccessibilityId("Browser")); |
| 75 | + // Open the Browser and perform actions |
| 76 | + WebElement browser = driver.findElement(MobileBy.AccessibilityId("Browser")); |
76 | 77 | browser.click(); |
77 | 78 |
|
78 | | - MobileElement url = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/url")); |
| 79 | + WebElement url = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/url")); |
79 | 80 | url.sendKeys("https://www.lambdatest.com"); |
80 | | - MobileElement find = (MobileElement) driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/find")); |
| 81 | + |
| 82 | + WebElement find = driver.findElement(MobileBy.id("com.lambdatest.proverbial:id/find")); |
81 | 83 | find.click(); |
82 | 84 |
|
83 | 85 | } catch (AssertionError a) { |
| 86 | + // Mark the status as failed if there's an error |
84 | 87 | ((JavascriptExecutor) driver).executeScript("lambda-status=failed"); |
85 | 88 | a.printStackTrace(); |
| 89 | + } finally { |
| 90 | + // Ensure the driver quits to avoid the session hanging |
| 91 | + if (driver != null) { |
| 92 | + driver.quit(); |
| 93 | + } |
86 | 94 | } |
87 | | - // The driver.quit statement is required, otherwise the test continues to |
88 | | - // execute, leading to a timeout. |
89 | | - driver.quit(); |
90 | 95 | } |
91 | 96 | } |
0 commit comments