Skip to main content

PyTest Automation :Run PyTest Automation Testing on LambdaTest Selenium Grid

LambdaTest Selenium Automation 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 run your Python Selenium automated test cases 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 Selenium with pytest automation testing on LambdaTest platform.
  2. How to set up PyTest automation testing environment for testing your locally hosted websites or web pages.
  3. How to configure Python automation desired capabilities for executing a Selenium test.
  4. How to run your test cases in parallel to reduce build times
  5. Advanced features of LambdaTest.

Prerequisites to Perform PyTest automation testing With LambdaTest Selenium Grid#

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

1. Global Dependencies

  • A Git or GitHub repository.
  • Make sure that you have the latest Python build installed. Download and install Python from here.
  • To install python with homebrew use the command below.
$ brew install python

2. LambdaTest Authentication Credential

Be aware of your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables. You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button.

  • For Linux/Mac:
$ export LT_USERNAME= "undefined"$ export LT_ACCESS_KEY= "undefined"
  • For Windows:
$ set LT_USERNAME= "undefined"$ set LT_ACCESS_KEY= "undefined"

You can also change the Environment Variable, via 'control panel'. Let's explore how:

  1. Click on the search bar, enter Environment Variables.
  2. Click Edit the environment variables.
  3. This will open the System Properties dialog.
  4. Click Environment Variables.
  5. This will open the Environment Variables dialog.
  6. In the User variables section, click New.
  7. This will open the New System Variable dialog.
  8. For Variable name, enter LT_USERNAME.
  9. For Variable value, enter your LambdaTest username.Click OK.
  10. Repeat 4 โ€“ 8 to set up the LT_ACCESS_KEY.

3. Project

  • The recommended way to run your tests would be in virtualenv. 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 specified in the requirements.txt file.

    pip install virtualenv
  • Create a virtual environment in your project folder the environment name is arbitrary.

    virtualenv venv
  • Activate the environment:

    source venv/bin/activate
  • Install the required packages:

    pip install -r requirements.txt

Running Your First pytest automation testing script on LambdaTest Selenium Grid#

The 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 with pytest automation. You can also find this code from our pytest GitHub repository.

import pytestfrom os import environ
from selenium import webdriverfrom selenium.common.exceptions import WebDriverExceptionfrom selenium.webdriver.remote.remote_connection import RemoteConnection

@pytest.fixture(scope='function')def driver(request):
    desired_caps = {}
    browser = {        "platform": "Windows 10",        "browserName": "chrome",        "version": "73"    }
    desired_caps.update(browser)    test_name =    build = environ.get('BUILD', "Sample PY Build")    tunnel_id = environ.get('TUNNEL', False)    username = environ.get('LT_USERNAME', None)    access_key = environ.get('LT_ACCESS_KEY', None)
    selenium_endpoint = "http://{}:{}".format(username, access_key)    desired_caps['build'] = build    desired_caps['name'] = test_name    executor = RemoteConnection(selenium_endpoint, resolve_ip=False)    browser = webdriver.Remote(        command_executor=executor,        desired_capabilities=desired_caps    )    yield browser

    def fin():        if request.node.rep_call.failed:            browser.execute_script("lambda-status=failed")        else:            browser.execute_script("lambda-status=passed")            browser.quit()    request.addfinalizer(fin)
@pytest.hookimpl(tryfirst=True, hookwrapper=True)def pytest_runtest_makereport(item, call):    # this sets the result as a test attribute for LambdaTest reporting.    # execute all other hooks to obtain the report object    outcome = yield    rep = outcome.get_result()
    # set an report attribute for each phase of a call, which can    # be "setup", "call", "teardown"    setattr(item, "rep_" + rep.when, rep)

Below is a sample code for test file.

import pytestimport sys
@pytest.mark.usefixtures('driver')class TestLink:
    def test_title(self, driver):        """        Verify click and title of page        :return: None        """        driver.get('')        driver.find_element_by_name("li1").click()        driver.find_element_by_name("li2").click()
        title = "Sample page -"        assert title == driver.title

    def test_item(self, driver):        """        Verify item submission        :return: None        """        driver.get('')        sample_text = "Happy Testing at LambdaTest"        email_text_field = driver.find_element_by_id("sampletodotext")        email_text_field.send_keys(sample_text)
        driver.find_element_by_id("addbutton").click()                li6 = driver.find_element_by_name("li6")        sys.stderr.write(li6)

Executing Parallel Testing Using pytest automation With Selenium Grid#

Below is the command using which you can run parallel test with pytest on LambdaTest Selenium Grid.

pytest -s -n=2 tests\

In the above command, we have used -s to disable the output capture & -n is used to specify the number of parallel test execution.

Test Your Locally Hosted Selenide Tests Using PyTest Automation With LambdaTest#

To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name LambdaTest Tunnel. With LambdaTest Tunnel, you can execute a test of your locally hosted Selenide tests on cloud to perform automated cross browser testing on 2000+ browsers offered by Selenium Grid on LambdaTest. So you make sure how well your changes look, even before your customers. Curious to know more about LambdaTest Tunnel?

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:

Important note: Some Safari & IE browsers, doesn't support automatic resolution of the URL string "localhost". Therefore if you test on URLs like https://localhost/ or https://localhost:8080 etc, you would get an error in these browsers. A possible solution is to use "" or replace the string "localhost" with machine IP address. For example if you wanted to test https://localhost/dashboard or, and your machine IP is you can instead test on or

Fast track your automated cross browser testing process with pytest automation testing + LambdaTest integration.

Got any questions? Feel free to share with our experts through 24/7 chat support or you can also drop a mail to [email protected]. Happy testing! ๐Ÿ™‚

Last updated on