Applies to CrossBrowserTesting SaaS, last modified on January 19, 2022

Behavior Driver Development is growing in popularity, and performing BDD with Python is no exception. Behave is a popular BDD framework for performing tests, and, because Behave is built on Python's Selenium language bindings, performing Behavioural Driven testing on CBT is easy. We'll walk your through getting started here.

Get set up

To get started, we will need to ensure that Behave is installed. The easiest means of doing so is with PIP:

pip install behave

Alternatively you can read installation documenation on the Behave website.

We also need to install Requests so we can perform interactions with our API like taking a snapshot and setting the score:

pip install requests

Lastly, we will need to install Selenium:

pip install selenium

Write tests

Once that is complete, we are ready to start writing our first test with Behave. Tests start with writing "Feature" files that use plain english to describe the steps of your test. Features use keywords to form the actual steps being taken in the test:

  • Given we put the system in a known state before the user (or external system) starts interacting with the system (in the When steps). Avoid talking about user interaction in givens.
  • When we take key actions the user (or external system) performs. This is the interaction with your system which should (or perhaps should not) cause some state to change.
  • Then we observe outcomes.

Create a new directory with a single file we will call 'Example.feature'. You can then copy the following feature into your feature file:

Feature: Test a login form

Scenario: Test Login
  Given I go to my login form
  Then the title should be "Login Form -"
  When I enter my credentials
  When I click login
  Then I should see the login message

Next, we will need to modify the environment we are testing on so we can perform our test in the cloud. Add another file to your directory called, and copy the following script into it:


from selenium import webdriver
from import expected_conditions as EC
from import By
from import WebDriverWait
import requests
import subprocess

username = ""# change this to the username associated with your account
authkey = "yourauthkey"# change this the authkey found on the 'Manage Account' section of our site
def before_feature(context, feature):
    caps = {}
    caps['name'] = 'Behave Example'
    caps['build'] = '1.0'
    caps['browserName'] = "Chrome"# pulls the latest version of Chrome by default
    caps['platform'] = "Windows 10"# to specify a version, add caps['version'] = "desired version"
    caps['screen_resolution'] = '1366x768'
    caps['record_video'] = 'true'
    caps['record_network'] = 'false'
    caps['take_snapshot'] = 'true'

    context.api_session = requests.Session()
    context.api_session.auth = (username, authkey)

    # cmd = "cbt_tunnels --username " + username + " --authkey " + authkey + " --ready tunnel_ready asadmin"

    # context.tunnel_proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

    context.driver = webdriver.Remote(
        command_executor=""%(username, authkey)

def after_feature(context, feature):
    # subprocess.Popen.terminate(context.tunnel_proc)

As you can see from our example, we are creating a Remote WebDriver that points to our hub. You can alternatively start a local connection so you can access locally hosted content. Just ensure that you have installed cbt_tunnels and then uncomment out the lines of code necessary for running the subprocess.

Now its time to actually define the steps from our Example.feature file in code. Create a new directory within your current one called Steps. This is where Behave will initially look for the code for your tests. Within that directory, create a file called, and copy to following code into that file:


@given('I go to my login form')
def go_to_login_form(context):

@then('the title should be {text}')
def verify_title(context, text):
    title = context.driver.title
        assert "Login Form -" == title
    except AssertionError as e:
        set_score(context, 'fail')

@when('I enter my credentials')
def enter_credentials(context):

@when('I click login')
def click_login(context):

@then('I should see the login message')
def see_login_message(context):
    elem = context.driver.find_element_by_xpath('//*[@id=\"logged-in-message\"]/h2')
    welcomeText = elem.text
        assert "Welcome" == welcomeText
        set_score(context, 'pass')
    except AssertionError as e:
        set_score(context, 'fail')

def set_score(context, test_result):
    context.api_session.put('' + context.driver.session_id,
            data={'action':'set_score', 'score': test_result})

You should note that we use, the Given-When-Then prefixes before every corresponding function. Steps are narrowed down to individual functions and the whole feature acts as a single unit test. At the end we set the score to the final value of our score variable, and our test is complete.

This is just the beginning of what you can do with Behave and Selenium! There is really no limits to what is possible with Selenium, and Behave makes it much easier, especially if you are working with QA's that are not programmers.

For examples and source code to this tutorial, check out our  Behave GitHub Repository

If you have any trouble getting setup, do not hesitate to reach out to us. We are happy to help.

See Also

Test Frameworks and Tools
About Selenium Testing
Selenium and Python

Highlight search results