├── .gitignore ├── .gitpod.yml ├── README.md ├── config └── config.json ├── features ├── environment.py ├── steps │ └── steps.py └── test.feature ├── pavement.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | output/* -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | # List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/ 2 | tasks: 3 | - init: 'pip3 install -r requirements.txt' # runs during prebuild 4 | command: 'paver run' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Run Selenium Tests With Behave On LambdaTest 2 | 3 | ![171934563-4806efd2-1154-494c-a01d-1def95657383 (1)](https://user-images.githubusercontent.com/70570645/172273386-fa9606ac-3e63-4b2e-8978-3142add3e038.png) 4 | 5 |

6 | Blog 7 |   ⋅   8 | Docs 9 |   ⋅   10 | Learning Hub 11 |   ⋅   12 | Newsletter 13 |   ⋅   14 | Certifications 15 |   ⋅   16 | YouTube 17 |

18 |   19 |   20 |   21 | 22 | *Learn how to run your Python automation testing scripts using Behave on the LambdaTest platform* 23 | 24 | [](https://accounts.lambdatest.com/register) 25 | 26 | 27 | ## Table Of Contents 28 | 29 | * [Pre-requisites](#pre-requisites) 30 | * [Run Your First Test](#run-your-first-test) 31 | * [Local Testing With Behave](#testing-locally-hosted-or-privately-hosted-projects) 32 | 33 | ## Prerequisites For Running Robot Selenium Tests 34 | * * * 35 | Before you can start performing Python automation testing using Robot, you would need to: 36 | 37 | * Install the latest Python build from the [official website](https://www.python.org/downloads/). We recommend using the latest version i.e python 3. 38 | * Make sure **pip 3** is installed in your system. You can install **pip** from [here](https://pip.pypa.io/en/stable/installation/). 39 | * Download the latest **Selenium Client** and its **WebDriver bindings** from the [official website](https://www.selenium.dev/downloads/). Latest versions of **Selenium Client** and **WebDriver** are ideal for running your automation script on LambdaTest Selenium cloud grid. 40 | * Install **virtualenv** which is the recommended way to run your tests. It will isolate the build from other setups you may have running and ensure that the tests run with the specified versions of the modules. 41 | ```bash 42 | pip install virtualenv 43 | ``` 44 | ### Installing Selenium Dependencies And Tutorial Repo 45 | 46 | **Step 1:** Clone the LambdaTest’s Python-Behave-Selenium repository and navigate to the code directory as shown below: 47 | 48 | ```bash 49 | git clone https://github.com/LambdaTest/Python-Behave-Selenium 50 | cd Python-Behave-Selenium 51 | ``` 52 | 53 | **Step 2:** Create a virtual environment in your project folder the environment name is arbitrary. 54 | 55 | ```bash 56 | virtualenv venv 57 | ``` 58 | 59 | **Step 3:** Activate the environment. 60 | 61 | ```bash 62 | source venv/bin/activate 63 | ``` 64 | 65 | **Step 4:** Install the [required packages](https://github.com/LambdaTest/Python-Behave-Selenium/blob/master/requirements.txt) from the cloned project directory: 66 | 67 | ```bash 68 | pip install -r requirements.txt 69 | ``` 70 | ### Setting Up Your Authentication 71 | 72 | Make sure you have your LambdaTest credentials with you to run test automation scripts. You can get these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login). 73 | 74 | **Step 5:** Set LambdaTest **Username** and **Access Key** in environment variables. 75 | 76 | * For **Linux/macOS**: 77 | 78 | ```bash 79 | export LT_USERNAME="YOUR_USERNAME" 80 | export LT_ACCESS_KEY="YOUR ACCESS KEY" 81 | ``` 82 | * For **Windows**: 83 | ```bash 84 | set LT_USERNAME="YOUR_USERNAME" 85 | set LT_ACCESS_KEY="YOUR ACCESS KEY" 86 | ``` 87 | 88 | 89 | ## Run Your First Test 90 | 91 | >**Test Scenario**: The below Python Behave script tests a simple to-do application with basic functionalities like mark items as done, add items in list, calculate total pending items etc. 92 | 93 | ```python 94 | from selenium import webdriver 95 | import os 96 | from configparser import ConfigParser 97 | 98 | caps={} 99 | 100 | def before_all(context): 101 | config = ConfigParser() 102 | print ((os.path.join(os.getcwd(), 'config.cfg'))) 103 | my_file = (os.path.join(os.getcwd(), 'config.cfg')) 104 | config.read(my_file) 105 | 106 | if os.getenv('LT_USERNAME', 0) == 0: 107 | context.user_name = config.get('Environment', 'UserName') 108 | if os.getenv('LT_ACCESS_KEY', 0) == 0: 109 | context.access_key = config.get('Environment', 'AccessKey' ) 110 | if os.getenv('LT_OPERATING_SYSTEM', 0) == 0: 111 | context.os = config.get('Environment', 'OS' ) 112 | if os.getenv('LT_BROWSER', 0) == 0: 113 | context.browser = config.get('Environment', 'Browser' ) 114 | if os.getenv('LT_BROWSER_VERSION', 0) == 0: 115 | context.browser_version = config.get('Environment', 'BrowserVersion' ) 116 | 117 | remote_url= "https://"+context.user_name+":"+context.app_key+"@hub.lambdatest.com/wd/hub" 118 | caps['name'] = "Behave Sample Test" 119 | caps['build'] = "Behave Selenium Sample" 120 | caps['browserName'] = context.browser 121 | caps['version'] = context.browser_version 122 | print ( caps ) 123 | print ( remote_url ) 124 | context.driver = webdriver.Remote(command_executor=remote_url,desired_capabilities=caps) 125 | 126 | @given('I go to sample-todo-app to add item') 127 | def step(context): 128 | before_all(context) 129 | context.driver.get('https://lambdatest.github.io/sample-todo-app/') 130 | 131 | @then('I Click on first checkbox and second checkbox') 132 | def click_on_checkbox_one(context): 133 | context.driver.find_element_by_name('li1').click() 134 | context.driver.find_element_by_name('li2').click() 135 | 136 | @when('I enter item to add') 137 | def enter_item_name(context): 138 | context.driver.find_element_by_id('sampletodotext').send_keys("Yey, Let's add it to list") 139 | 140 | @when('I click add button') 141 | def click_on_add_button(context): 142 | context.driver.find_element_by_id('addbutton').click() 143 | 144 | @then('I should verify the added item') 145 | def see_login_message(context): 146 | context.driver.implicitly_wait(10) 147 | added_item = context.driver.find_element_by_xpath("//span[@class='done-false']").text 148 | print(added_item) 149 | print(added_item) 150 | if added_item in "Yey, Let's add it to list": 151 | return True 152 | else: 153 | return False 154 | 155 | def after_all(context): 156 | context.browser.quit() 157 | ``` 158 | ### Configuration Of Your Test Capabilities 159 | 160 | **Step 6:** In `config/config.json`, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as: 161 | ```python 162 | [ 163 | { 164 | "platform": "Windows 10", 165 | "browserName": "chrome", 166 | "version": "latest", 167 | "build": "Behave Selenium Sample", 168 | "name": "Behave Sample Test" 169 | } 170 | ] 171 | ``` 172 | > You can generate capabilities for your test requirements with the help of [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/). 173 | 174 | ### Executing The Test 175 | 176 | **Step 7:** The tests can be executed in the terminal using the following command. 177 | ```bash 178 | behave features/test.feature 179 | ``` 180 | 181 | **Step 8:** The tests can be executed in the terminal parallel using behavex via tags. 182 | ```bash 183 | behavex -t @Firefox,@Chrome,@Edge --parallel-processes 3 184 | ``` 185 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard. [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build) will help you view all your text logs, screenshots and video recording for your entire automation tests. 186 | 187 | ## Testing Locally Hosted or Privately Hosted Projects 188 | *** 189 | You can test your locally hosted or privately hosted projects with [LambdaTest Selenium grid cloud](https://www.lambdatest.com/selenium-automation) using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app and pass toggle `tunnel = True` via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live. 190 | 191 | >Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information. 192 | 193 | Here’s how you can establish LambdaTest Tunnel. 194 | 195 | Download the binary file of: 196 | 197 | * [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip) 198 | * [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip) 199 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip) 200 | 201 | Open command prompt and navigate to the binary folder. 202 | 203 | Run the following command: 204 | ```bash 205 | LT -user {user’s login email} -key {user’s access key} 206 | ``` 207 | So if your user name is lambdatest@example.com and key is 123456, the command would be: 208 | ```bash 209 | LT -user lambdatest@example.com -key 123456 210 | ``` 211 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below : 212 | 213 | **Tunnel Capability** 214 | 215 | ```bash 216 | "tunnel" : true 217 | ``` 218 | 219 | ## Documentation & Resources :books: 220 | 221 | 222 | 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. 223 | 224 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=python-behave-selenium) 225 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=python-behave-selenium) 226 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=python-behave-selenium) 227 | 228 | ## LambdaTest Community :busts_in_silhouette: 229 | 230 | 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 🌎 231 | 232 | ## What's New At LambdaTest ❓ 233 | 234 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/) 235 | 236 | ## About LambdaTest 237 | 238 | [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. 239 | 240 | ### Features 241 | 242 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 243 | * Real-time cross browser testing on 3000+ environments. 244 | * Test on Real device cloud 245 | * Blazing fast test automation with HyperExecute 246 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 247 | * Smart Visual Regression Testing on cloud 248 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 249 | * Automated Screenshot testing across multiple browsers in a single click. 250 | * Local testing of web and mobile apps. 251 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 252 | * Geolocation testing of web and mobile apps across 53+ countries. 253 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 254 | 255 | 256 | [](https://accounts.lambdatest.com/register) 257 | 258 | 259 | 260 | ## We are here to help you :headphones: 261 | 262 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com) 263 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-behave-selenium) 264 | -------------------------------------------------------------------------------- /config/config.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "platform": "Windows 10", 4 | "browserName": "chrome", 5 | "version": "latest", 6 | "build": "Behave Selenium Sample", 7 | "name": "Behave Sample Test" 8 | } 9 | ] -------------------------------------------------------------------------------- /features/environment.py: -------------------------------------------------------------------------------- 1 | from behave.model_core import Status 2 | from selenium import webdriver 3 | from selenium.webdriver.chrome.options import Options as ChromeOptions 4 | from selenium.webdriver.firefox.options import Options as FirefoxOptions 5 | from selenium.webdriver.edge.options import Options as EdgeOptions 6 | import os 7 | import json 8 | 9 | INDEX = int(os.environ['INDEX']) if 'INDEX' in os.environ else 0 10 | if os.environ.get("env") == "jenkins": 11 | desired_cap_dict = os.environ["LT_BROWSERS"] 12 | CONFIG = json.loads(desired_cap_dict) 13 | else: 14 | json_file = "config/config.json" 15 | with open(json_file) as data_file: 16 | CONFIG = json.load(data_file) 17 | 18 | username = os.environ["LT_USERNAME"] 19 | authkey = os.environ["LT_ACCESS_KEY"] 20 | 21 | 22 | 23 | def before_scenario(context, scenario): 24 | try: 25 | desired_cap = setup_desired_cap(CONFIG[INDEX]) 26 | 27 | if 'Chrome' in scenario.tags: 28 | options = ChromeOptions() 29 | options.browser_version = desired_cap.get("version", "latest") 30 | options.platform_name = "Windows 11" 31 | elif 'Firefox' in scenario.tags: 32 | options = FirefoxOptions() 33 | options.browser_version = desired_cap.get("version", "latest") 34 | options.platform_name = "Windows 10" 35 | elif 'Edge' in scenario.tags: 36 | options = EdgeOptions() 37 | options.browser_version = desired_cap.get("version", "latest") 38 | options.platform_name = "Windows 8" 39 | else: 40 | raise ValueError("Unsupported browser tag") 41 | 42 | options.set_capability('build', desired_cap.get('build')) 43 | options.set_capability('name', desired_cap.get('name')) 44 | 45 | # Print options for debugging 46 | print("Browser Options:", options.to_capabilities()) 47 | 48 | context.browser = webdriver.Remote( 49 | command_executor=f"https://{username}:{authkey}@hub.lambdatest.com/wd/hub", 50 | options=options 51 | ) 52 | except Exception as e: 53 | print(f"Error in before_scenario: {str(e)}") 54 | context.scenario.skip(reason=f"Failed to initialize browser: {str(e)}") 55 | 56 | def after_scenario(context, scenario): 57 | if hasattr(context, 'browser'): 58 | try: 59 | if scenario.status == Status.failed: 60 | context.browser.execute_script("lambda-status=failed") 61 | else: 62 | context.browser.execute_script("lambda-status=passed") 63 | except Exception as e: 64 | print(f"Error setting lambda status: {str(e)}") 65 | finally: 66 | context.browser.quit() 67 | 68 | 69 | def setup_desired_cap(desired_cap): 70 | """ 71 | Sets the capability according to LT 72 | :param desired_cap: 73 | :return: 74 | """ 75 | # Create a new dictionary to avoid modifying the original 76 | cleaned_cap = {} 77 | 78 | for key, value in desired_cap.items(): 79 | if key == 'connect': 80 | # Force 'connect' to be None if it's not a valid timeout value 81 | if not isinstance(value, (int, float)) or value is None: 82 | cleaned_cap[key] = None 83 | else: 84 | cleaned_cap[key] = value 85 | else: 86 | cleaned_cap[key] = value 87 | 88 | return cleaned_cap -------------------------------------------------------------------------------- /features/steps/steps.py: -------------------------------------------------------------------------------- 1 | """ 2 | Selenium steps to configure behave test scenarios 3 | """ 4 | import time 5 | from behave import * 6 | from selenium.webdriver.common.by import By 7 | from selenium.webdriver.support.ui import WebDriverWait 8 | from selenium.webdriver.support import expected_conditions as EC 9 | 10 | def wait_for_element(context, by, value, timeout=10): 11 | return WebDriverWait(context.browser, timeout).until( 12 | EC.presence_of_element_located((by, value)) 13 | ) 14 | 15 | @when('visit url "{url}"') 16 | def step(context, url): 17 | context.browser.get(url) 18 | 19 | @when('check if title is "{title}"') 20 | def step(context, title): 21 | assert context.browser.title == title 22 | 23 | @when('field with name "First Item" is present check the box') 24 | def step(context): 25 | element = wait_for_element(context, By.NAME, "li1") 26 | element.click() 27 | 28 | @when('field with name "Second Item" is present check the box') 29 | def step(context): 30 | element = wait_for_element(context, By.NAME, "li3") 31 | element.click() 32 | 33 | @when('select the textbox add "{text}" in the box') 34 | def step(context, text): 35 | textbox = wait_for_element(context, By.ID, "sampletodotext") 36 | textbox.click() 37 | textbox.clear() 38 | textbox.send_keys(text) 39 | 40 | @then('click the "{button}"') 41 | def step(context, button): 42 | element = wait_for_element(context, By.ID, button) 43 | element.click() -------------------------------------------------------------------------------- /features/test.feature: -------------------------------------------------------------------------------- 1 | Feature: Automate a website 2 | 3 | @Chrome 4 | Scenario: perform click events with Chrome 5 | When visit url "https://lambdatest.github.io/sample-todo-app" 6 | When check if title is "Sample page - lambdatest.com" 7 | When field with name "First Item" is present check the box 8 | When field with name "Second Item" is present check the box 9 | When select the textbox add "Let's add new to do item for chrome" in the box 10 | Then click the "addbutton" 11 | 12 | @Firefox 13 | Scenario: perform click events with Firefox 14 | When visit url "https://lambdatest.github.io/sample-todo-app" 15 | When check if title is "Sample page - lambdatest.com" 16 | When field with name "First Item" is present check the box 17 | When field with name "Second Item" is present check the box 18 | When select the textbox add "Let's add new to do item for Firefox" in the box 19 | Then click the "addbutton" 20 | 21 | @Edge 22 | Scenario: perform click events with Edge 23 | When visit url "https://lambdatest.github.io/sample-todo-app" 24 | When check if title is "Sample page - lambdatest.com" 25 | When field with name "First Item" is present check the box 26 | When field with name "Second Item" is present check the box 27 | When select the textbox add "Let's add new to do item for Edge" in the box 28 | Then click the "addbutton" -------------------------------------------------------------------------------- /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-todo", 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>4.23.0 3 | behave==1.2.6 4 | behavex==1.6.0 --------------------------------------------------------------------------------