├── README.md ├── features ├── AndroidFeature.feature └── IOSFeature.feature ├── pavement.py ├── requirements.txt └── steps ├── AndroidStepDef.py ├── IosStepDef.py └── appConfig.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-behave-openApp) using the Appium Python Behave Framework 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 | import sys 26 | import os 27 | path = os.getcwd() 28 | sys.path.append(os.path.abspath(os.path.join(path, os.pardir))) 29 | from time import time 30 | from behave import given 31 | from appium import webdriver 32 | import appConfig as appConf 33 | from appium.webdriver.common.mobileby import MobileBy 34 | from selenium.webdriver.support.ui import WebDriverWait 35 | from selenium.webdriver.support import expected_conditions as EC 36 | 37 | @given("Start the android app automation test") 38 | def startAndroidAppAutomationTest(self): 39 | if os.environ.get("LT_USERNAME") is None: 40 | username = "username" #Enter LT username here if environment variables have not been added 41 | else: 42 | username = os.environ.get("LT_USERNAME") 43 | if os.environ.get("LT_ACCESS_KEY") is None: 44 | accesskey = "accesskey" #Enter LT accesskey here if environment variables have not been added 45 | else: 46 | accesskey = os.environ.get("LT_ACCESS_KEY") 47 | 48 | driver = webdriver.Remote( 49 | command_executor="https://"+username+":"+accesskey+"@beta-hub.lambdatest.com/wd/hub", 50 | desired_capabilities=appConf.app_android_desired_caps 51 | ) 52 | try: 53 | colorElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/color"))) 54 | colorElement.click() 55 | 56 | textElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/Text"))) 57 | textElement.click() 58 | 59 | #Close the application being tested 60 | driver.close_app() 61 | 62 | #Open the application being tested 63 | driver.open_app() 64 | 65 | toastElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/toast"))) 66 | toastElement.click() 67 | 68 | notification = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/notification"))) 69 | notification.click() 70 | 71 | driver.quit() 72 | except: 73 | driver.quit() 74 | ``` 75 | 76 | For more information on the Appium Commands used here, head over to: 77 | * [Launch App | Appium](https://appium.io/docs/en/commands/device/app/launch-app/) 78 | * [Close App | Appium](https://appium.io/docs/en/commands/device/app/close-app/) 79 | 80 | ## Executing The Tests 81 | 82 | Install the required dependencies using the following command: 83 | 84 | ```bash 85 | pip3 install -r requirements.txt 86 | ``` 87 | 88 | Execute the following command to run your test on LambdaTest platform: 89 | 90 | **Android:** 91 | 92 | ```bash 93 | behave --tags @androidApp 94 | ``` 95 | 96 | **IOS:** 97 | 98 | ```bash 99 | behave --tags @iosApp 100 | ``` 101 | 102 | 103 | 104 | 105 | 106 | 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). 107 | 108 | ## Additional Links 109 | 110 | - [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/desired-capabilities-in-appium/) 111 | - [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) 112 | - [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/) 113 | 114 | ## Documentation & Resources :books: 115 | 116 | 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. 117 | 118 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python) 119 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python) 120 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python) 121 | * [LambdaTest Community](http://community.lambdatest.com/) 122 | 123 | ## LambdaTest Community :busts_in_silhouette: 124 | 125 | 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 🌎 126 | 127 | ## What's New At LambdaTest ❓ 128 | 129 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/) 130 | 131 | ## About LambdaTest 132 | 133 | [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. 134 | 135 | ### Features 136 | 137 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 138 | * Real-time cross browser testing on 3000+ environments. 139 | * Test on Real device cloud 140 | * Blazing fast test automation with HyperExecute 141 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 142 | * Smart Visual Regression Testing on cloud 143 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 144 | * Automated Screenshot testing across multiple browsers in a single click. 145 | * Local testing of web and mobile apps. 146 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 147 | * Geolocation testing of web and mobile apps across 53+ countries. 148 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 149 | 150 | [](https://accounts.lambdatest.com/register) 151 | 152 | ## We are here to help you :headphones: 153 | 154 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com) 155 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=LT-appium-python-behave-openApp) -------------------------------------------------------------------------------- /features/AndroidFeature.feature: -------------------------------------------------------------------------------- 1 | Feature: Android real device app and web automation 2 | 3 | @androidApp 4 | Scenario: Android real device app automation 5 | Given Start the android app automation test -------------------------------------------------------------------------------- /features/IOSFeature.feature: -------------------------------------------------------------------------------- 1 | Feature: IOS real device web and app automation 2 | 3 | @iosApp 4 | Scenario: Android real device app automation 5 | Given Start the ios app automation test -------------------------------------------------------------------------------- /pavement.py: -------------------------------------------------------------------------------- 1 | from paver.easy import * 2 | from paver.setuputils import setup 3 | import multiprocessing 4 | import json 5 | import os 6 | 7 | setup( 8 | name="python-behave-proverbial", 9 | packages=['features'], 10 | version="1.0.0", 11 | url="https://www.lambdatest.com/", 12 | author="Lambdatest", 13 | description=("Behave Integration with Lambdatest"), 14 | license="MIT", 15 | author_email="support@lambdatest.com" 16 | ) 17 | 18 | 19 | def run_behave_test(env, index=0): 20 | """ 21 | runs the individual test 22 | :param env: 23 | :param index: 24 | :return: 25 | """ 26 | if env == "jenkins": 27 | sh('INDEX=%s env=%s behave features/test.feature ' % (index, env,)) 28 | else: 29 | sh('INDEX=%s env=%s behave features/test.feature ' % (index, env,)) 30 | 31 | 32 | @task 33 | @consume_args 34 | def run(args): 35 | """ 36 | runs the behave test 37 | :return: 38 | """ 39 | env = args[0] if len(args) > 0 else "" 40 | jobs = [] 41 | pool = get_pool_size() 42 | for i in range(pool): 43 | p = multiprocessing.Process(target=run_behave_test, args=(env, i,)) 44 | jobs.append(p) 45 | p.start() 46 | 47 | 48 | def get_pool_size(): 49 | """ 50 | sets the number of parallel test 51 | :return: 52 | """ 53 | if "LT_BROWSERS" in os.environ: 54 | CONFIG = json.loads(os.environ["LT_BROWSERS"]) 55 | pool = len(CONFIG) 56 | else: 57 | json_file = "config/config.json" 58 | with open(json_file) as data_file: 59 | CONFIG = json.load(data_file) 60 | pool = len(CONFIG) 61 | return pool 62 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Paver==1.3.4 2 | selenium 3 | behave==1.2.6 4 | Appium-Python-Client -------------------------------------------------------------------------------- /steps/AndroidStepDef.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | path = os.getcwd() 4 | sys.path.append(os.path.abspath(os.path.join(path, os.pardir))) 5 | from time import time 6 | from behave import given 7 | from appium import webdriver 8 | import appConfig as appConf 9 | from appium.webdriver.common.mobileby import MobileBy 10 | from selenium.webdriver.support.ui import WebDriverWait 11 | from selenium.webdriver.support import expected_conditions as EC 12 | 13 | @given("Start the android app automation test") 14 | def startAndroidAppAutomationTest(self): 15 | if os.environ.get("LT_USERNAME") is None: 16 | username = "username" #Enter LT username here if environment variables have not been added 17 | else: 18 | username = os.environ.get("LT_USERNAME") 19 | if os.environ.get("LT_ACCESS_KEY") is None: 20 | accesskey = "accesskey" #Enter LT accesskey here if environment variables have not been added 21 | else: 22 | accesskey = os.environ.get("LT_ACCESS_KEY") 23 | 24 | driver = webdriver.Remote( 25 | command_executor="https://"+username+":"+accesskey+"@beta-hub.lambdatest.com/wd/hub", 26 | desired_capabilities=appConf.app_android_desired_caps 27 | ) 28 | try: 29 | colorElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/color"))) 30 | colorElement.click() 31 | 32 | textElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/Text"))) 33 | textElement.click() 34 | 35 | #Close the application being tested 36 | driver.close_app() 37 | 38 | #Open the application being tested 39 | driver.open_app() 40 | 41 | toastElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/toast"))) 42 | toastElement.click() 43 | 44 | notification = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ID,"com.lambdatest.proverbial:id/notification"))) 45 | notification.click() 46 | 47 | driver.quit() 48 | except: 49 | driver.quit() 50 | -------------------------------------------------------------------------------- /steps/IosStepDef.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | path = os.getcwd() 4 | sys.path.append(os.path.abspath(os.path.join(path, os.pardir))) 5 | import appConfig as appConf 6 | from behave import given 7 | from appium import webdriver 8 | import time 9 | from appium.webdriver.common.mobileby import MobileBy 10 | from selenium.webdriver.support.ui import WebDriverWait 11 | from selenium.webdriver.support import expected_conditions as EC 12 | 13 | @given("Start the ios app automation test") 14 | def startIOSAppAutomationTest(self): 15 | if os.environ.get("LT_USERNAME") is None: 16 | username = "username" #Enter LT username here if environment variables have not been added 17 | else: 18 | username = os.environ.get("LT_USERNAME") 19 | if os.environ.get("LT_ACCESS_KEY") is None: 20 | accesskey = "accesskey" #Enter LT accesskey here if environment variables have not been added 21 | else: 22 | accesskey = os.environ.get("LT_ACCESS_KEY") 23 | 24 | driver = webdriver.Remote( 25 | command_executor="https://"+username+":"+accesskey+"@beta-hub.lambdatest.com/wd/hub", 26 | desired_capabilities=appConf.app_ios_desired_caps 27 | ) 28 | try: 29 | colorElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"color"))) 30 | colorElement.click() 31 | 32 | textElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"Text"))) 33 | textElement.click() 34 | 35 | #Close the application being tested 36 | driver.close_app() 37 | 38 | #Open the application being tested 39 | driver.open_app() 40 | 41 | toastElement = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"toast"))) 42 | toastElement.click() 43 | 44 | notification = WebDriverWait(driver,20).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"notification"))) 45 | notification.click() 46 | time.sleep(3) 47 | 48 | driver.quit() 49 | except: 50 | driver.quit() 51 | -------------------------------------------------------------------------------- /steps/appConfig.py: -------------------------------------------------------------------------------- 1 | app_android_desired_caps = { 2 | "deviceName":"Galaxy S20", 3 | "platformName":"Android", 4 | "platformVersion":"10", 5 | "build":"Python Behave - Android", 6 | "name":"Sample Test Android", 7 | "isRealMobile":True, 8 | "network":True, 9 | "visual":True, 10 | "video":True, 11 | 12 | 13 | #Enter the Custom_ID here that was used to upload your application 14 | 15 | "app":"ENTER_CUSTOM_ID_HERE", 16 | } 17 | 18 | app_ios_desired_caps = { 19 | "deviceName":"iPhone 12", 20 | "platformName":"ios", 21 | "platformVersion":"14", 22 | "build":"Python Behave - iOS", 23 | "name":"Sample Test iOS", 24 | "isRealMobile":True, 25 | "network":True, 26 | "visual":True, 27 | "video":True, 28 | 29 | #Enter the Custom_ID here that was used to upload your application 30 | 31 | "app":"ENTER_CUSTOM_ID_HERE", 32 | } 33 | --------------------------------------------------------------------------------