Skip to main content

Behave With Selenium: Run Behave Automation Testing Scripts On Selenium Grid


LambdaTest Selenium Grid is a cloud based scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now execute Selenium automated testing with Behave on a scalable Selenium infrastructure that is running real browsers and real operating systems. This post will help you in getting started with configuring and running your Python based automation test scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:

  1. How to run a sample using Behave with Selenium using LambdaTest Automation
  2. How to set up an environment for testing locally hosted websites or web pages.
  3. How to configure Behave desired capabilities to perform Behave automation testing
  4. How to run your test cases in parallel to reduce build times
  5. Advanced features of LambdaTest.

Prerequisites For Running Behave With Selenium#


All the code samples in this documentation can be found in the Behave LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

  • Have Selenium WebDriver client driver installed inside your local Python environment.
  • Make sure that you have the latest Python build installed. You can download the latest Python build from https://www.python.org/downloads/ .
  • You would need Python package manager, pip, as well. Usually latest python installers come prepacked with pip. However if you don’t have pip You can install it using command:
curl https://bootstrap.pypa.io/get-pip.py | python
  • Download the driver from the link, or you can use pip to install it
pip install selenium
  • Make sure you have installed Behave automation testing framework in your system. In case you don’t! Enter the below command:
$ sudo pip install behave
  • If you wish to test locally hosted or privately hosted projects, you would also need LambdaTest Tunnel binary file.

Follow our documentation on LambdaTest Tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.

Download the binary file of:

Getting Started With Behave automation framework and LambdaTest#


he first step in using LambdaTest platform is to understand LambdaTest’s Selenium Grid capabilities. Our Selenium Grid uses remote webdriver instead of normal Selenium client browser drivers so if you are migrating from locally run Selenium, you would have to invoke LambdaTest Selenium remote webdriver. Next you need to specify in your code, which browser, browser versions, operating systems, and resolution you wish to run your test on, along with defining LambdaTest specific capabilities. You can checkout LambdaTest Capabilities Generator tool to understand more about how you can define running browser environments and leverage advanced LambdaTest capabilities.

Let’ start with a simple Selenium Remote Webdriver test first. The Python script below tests a simple to-do application with basic functionalities like mark items as done, add items in list, calculate total pending items etc. You can also find this at our GitHub repository

Feature: Test to add item Scenario: Test sample-todo-app  Given I go to sample-todo-app to add item  Then I Click on first checkbox and second checkbox  When I enter item to add  When I click add button  Then I should verify the added item

Now, we have to create step definition file.

from selenium import webdriverimport osfrom configparser import ConfigParser caps={} def before_all(context):       config = ConfigParser()    print ((os.path.join(os.getcwd(), 'config.cfg')))    my_file = (os.path.join(os.getcwd(), 'config.cfg'))    config.read(my_file)         if os.getenv('LT_USERNAME', 0) == 0:        context.user_name = config.get('Environment', 'UserName')    if os.getenv('LT_APPKEY', 0)  == 0:        context.app_key = config.get('Environment', 'AppKey' )    if os.getenv('LT_OPERATING_SYSTEM', 0) == 0:        context.os = config.get('Environment', 'OS' )    if os.getenv('LT_BROWSER', 0)  == 0:        context.browser = config.get('Environment', 'Browser' )    if os.getenv('LT_BROWSER_VERSION', 0)  == 0:        context.browser_version = config.get('Environment', 'BrowserVersion' )     remote_url= "https://"+context.user_name+":"+context.app_key+"@hub.lambdatest.com/wd/hub"    caps['name'] = "Behave Sample Test"    caps['build'] = "Behave Selenium Sample"    caps['browserName'] = context.browser          caps['version'] = context.browser_version    print ( caps )    print ( remote_url )    context.driver = webdriver.Remote(command_executor=remote_url,desired_capabilities=caps) @given('I go to sample-todo-app to add item')def step(context):    before_all(context)    context.driver.get('https://lambdatest.github.io/sample-todo-app/') @then('I Click on first checkbox and second checkbox')def click_on_checkbox_one(context):    context.driver.find_element_by_name('li1').click()    context.driver.find_element_by_name('li2').click() @when('I enter item to add')def enter_item_name(context):    context.driver.find_element_by_id('sampletodotext').send_keys("Yey, Let's add it to list") @when('I click add button')def click_on_add_button(context):    context.driver.find_element_by_id('addbutton').click() @then('I should verify the added item')def see_login_message(context):    context.driver.implicitly_wait(10)    added_item = context.driver.find_element_by_xpath("//span[@class='done-false']").text    print(added_item)    print(added_item)    if added_item in "Yey, Let's add it to list":        return True    else:        return False def after_all(context):    context.browser.quit()

Here is config.cfg file.

[Environment]UserName= "undefined"AppKey= "undefined"OS = win10Browser=chromeBrowserVersion= 63.0

Now Perform Tests using Behave with Selenium#


You would need to execute the below command in your terminal/cmd.

behave features\todo.feature
Last updated on