├── README.md ├── android.py └── ios.py /README.md: -------------------------------------------------------------------------------- 1 | # How to close/open the app during a session in Real Devices on [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python-openApp) using the Appium & Python Language 2 | 3 | While performing automation testing with Appium on LambdaTest Grid, you might face a scenario where you'd want to close the application or open the application during a test run. 4 | This can be essential sometimes to see how your application behaves in different scenarios. Let's see how we can leverage Appium's commands to achieve it on LambdaTest. 5 | 6 | **Tip:** 7 | 8 | - If you do not have any **.apk** or **.ipa** file, you can run your sample tests on LambdaTest by using our sample :link: [Android app](https://prod-mobile-artefacts.lambdatest.com/assets/docs/proverbial_android.apk) or sample :link: [iOS app](https://prod-mobile-artefacts.lambdatest.com/assets/docs/proverbial_ios.ipa). 9 | - Response of above cURL will be a **JSON** object containing the `App URL` of the format - and will be used in the next step. 10 | - Alternatively you can also use the `Custom ID` that you defined in your own words. 11 | 12 | ## Run Your First Test 13 | 14 | Once you are done with the above-mentioned steps, you can initiate your first Python test on LambdaTest. 15 | 16 | ### Configuring Your Test Capabilities 17 | 18 | You can update your custom capabilities in test scripts. In this sample project, we are passing platform name, platform version, device name and app url (generated earlier) along with other capabilities like build name and test name via capabilities object. The capabilities object in the sample code are defined as: 19 | 20 | 21 | 22 | 23 | 24 | ``` 25 | from appium import webdriver 26 | from appium.webdriver.common.mobileby import MobileBy 27 | from selenium.webdriver.support.ui import WebDriverWait 28 | from selenium.webdriver.support import expected_conditions as EC 29 | import time 30 | import os 31 | 32 | desired_caps = { 33 | "deviceName":"Galaxy S20", 34 | "platformName":"Android", 35 | "platformVersion":"10", 36 | "app":"lt://", 37 | "isRealMobile":True, 38 | "build":"Python Vanilla Android", 39 | "name":"Sample Test - Python", 40 | "network":True, 41 | "visual":True, 42 | "video":True 43 | } 44 | 45 | def startingTest(): 46 | if os.environ.get("LT_USERNAME") is None: 47 | username = "username" #Enter LT username here if environment variables have not been added 48 | else: 49 | username = os.environ.get("LT_USERNAME") 50 | if os.environ.get("LT_ACCESS_KEY") is None: 51 | accesskey = "accesskey" #Enter LT accesskey here if environment variables have not been added 52 | else: 53 | accesskey = os.environ.get("LT_ACCESS_KEY") 54 | 55 | try: 56 | driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://"+username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") 57 | colorElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/color"))) 58 | colorElement.click() 59 | 60 | textElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/Text"))) 61 | textElement.click() 62 | 63 | toastElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/toast"))) 64 | toastElement.click() 65 | 66 | #Close the application being tested 67 | driver.close_app() 68 | 69 | #Open the application being tested 70 | driver.open_app() 71 | 72 | notification = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/notification"))) 73 | notification.click() 74 | 75 | driver.quit() 76 | except: 77 | driver.quit() 78 | 79 | startingTest() 80 | ``` 81 | 82 | For more information on the Appium Commands used here, head over to: 83 | * [Launch App | Appium](https://appium.io/docs/en/commands/device/app/launch-app/) 84 | * [Close App | Appium](https://appium.io/docs/en/commands/device/app/close-app/) 85 | 86 | ## Executing The Tests 87 | 88 | Run the following command in the directory where your project has been saved to execute your build. 89 | 90 | 91 | 92 | 93 | 94 | ```bash 95 | python3 ios.py 96 | ``` 97 | 98 | 99 | 100 | 101 | 102 | ```bash 103 | python3 android.py 104 | ``` 105 | 106 | 107 | 108 | 109 | 110 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on the [LambdaTest App Automation Dashboard](https://appautomation.lambdatest.com/build). 111 | 112 | ## Additional Links 113 | 114 | - [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/desired-capabilities-in-appium/) 115 | - [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) 116 | - [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/) 117 | 118 | ## Documentation & Resources :books: 119 | 120 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing. 121 | 122 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python) 123 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python) 124 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python) 125 | * [LambdaTest Community](http://community.lambdatest.com/) 126 | 127 | ## LambdaTest Community :busts_in_silhouette: 128 | 129 | The [LambdaTest Community](https://community.lambdatest.com/) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎 130 | 131 | ## What's New At LambdaTest ❓ 132 | 133 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/) 134 | 135 | ## About LambdaTest 136 | 137 | [LambdaTest](https://www.lambdatest.com) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs. 138 | 139 | ### Features 140 | 141 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 142 | * Real-time cross browser testing on 3000+ environments. 143 | * Test on Real device cloud 144 | * Blazing fast test automation with HyperExecute 145 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 146 | * Smart Visual Regression Testing on cloud 147 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 148 | * Automated Screenshot testing across multiple browsers in a single click. 149 | * Local testing of web and mobile apps. 150 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 151 | * Geolocation testing of web and mobile apps across 53+ countries. 152 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 153 | 154 | [](https://accounts.lambdatest.com/register) 155 | 156 | ## We are here to help you :headphones: 157 | 158 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com) 159 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python-openApp) 160 | -------------------------------------------------------------------------------- /android.py: -------------------------------------------------------------------------------- 1 | from appium import webdriver 2 | from appium.webdriver.common.mobileby import MobileBy 3 | from selenium.webdriver.support.ui import WebDriverWait 4 | from selenium.webdriver.support import expected_conditions as EC 5 | import time 6 | import os 7 | 8 | desired_caps = { 9 | "deviceName":"Galaxy S20", 10 | "platformName":"Android", 11 | "platformVersion":"10", 12 | "app":"lt://", 13 | "isRealMobile":True, 14 | "build":"Python Vanilla Android", 15 | "name":"Sample Test - Python", 16 | "network":True, 17 | "visual":True, 18 | "video":True 19 | } 20 | 21 | def startingTest(): 22 | if os.environ.get("LT_USERNAME") is None: 23 | username = "username" #Enter LT username here if environment variables have not been added 24 | else: 25 | username = os.environ.get("LT_USERNAME") 26 | if os.environ.get("LT_ACCESS_KEY") is None: 27 | accesskey = "accesskey" #Enter LT accesskey here if environment variables have not been added 28 | else: 29 | accesskey = os.environ.get("LT_ACCESS_KEY") 30 | 31 | try: 32 | driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://"+username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") 33 | colorElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/color"))) 34 | colorElement.click() 35 | 36 | textElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/Text"))) 37 | textElement.click() 38 | 39 | toastElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/toast"))) 40 | toastElement.click() 41 | 42 | #Close the application 43 | driver.close_app() 44 | 45 | #Open the application 46 | driver.open_app() 47 | 48 | notification = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/notification"))) 49 | notification.click() 50 | 51 | driver.quit() 52 | except: 53 | driver.quit() 54 | 55 | startingTest() -------------------------------------------------------------------------------- /ios.py: -------------------------------------------------------------------------------- 1 | from appium import webdriver 2 | from appium.webdriver.common.mobileby import MobileBy 3 | from selenium.webdriver.support.ui import WebDriverWait 4 | from selenium.webdriver.support import expected_conditions as EC 5 | import time 6 | import os 7 | 8 | desired_caps = { 9 | "deviceName":"iPhone 12", 10 | "platformName":"ios", 11 | "platformVersion":"14", 12 | "isRealMobile":True, 13 | "app":"lt://", 14 | "build":"Python Vanilla iOS", 15 | "name":"Sample Test - Python", 16 | "network":True, 17 | "visual":True, 18 | "video":True 19 | } 20 | 21 | def startingTest(): 22 | if os.environ.get("LT_USERNAME") is None: 23 | username = "username" #Enter LT username here if environment variables have not been added 24 | else: 25 | username = os.environ.get("LT_USERNAME") 26 | if os.environ.get("LT_ACCESS_KEY") is None: 27 | accesskey = "accesskey" #Enter LT accesskey here if environment variables have not been added 28 | else: 29 | accesskey = os.environ.get("LT_ACCESS_KEY") 30 | 31 | try: 32 | driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://"+username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") 33 | time.sleep(3) 34 | colorElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"color"))) 35 | colorElement.click() 36 | textElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"Text"))) 37 | textElement.click() 38 | 39 | #Close the application 40 | driver.close_app() 41 | 42 | #Open the application 43 | driver.open_app() 44 | 45 | toastElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"toast"))) 46 | toastElement.click() 47 | notification = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"notification"))) 48 | notification.click() 49 | 50 | driver.quit() 51 | except: 52 | driver.quit() 53 | 54 | startingTest() 55 | --------------------------------------------------------------------------------