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 |
$ brew install python |
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.
1 2 |
$ export LT_USERNAME=<your lambdatest username> $ export LT_ACCESS_KEY=<your lambdatest access_key> |
1 2 |
$ set LT_USERNAME=<your lambdatest username> $ set LT_ACCESS_KEY=<your lambdatest access_key> |
You can also change the Environment Variable, via ‘control panel’. Let’s explore how:
1 |
pip install virtualenv |
1 |
virtualenv venv |
1 |
source venv/bin/activate |
1 |
pip install -r requirements.txt |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import pytest from os import environ from selenium import webdriver from selenium.common.exceptions import WebDriverException from 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 = request.node.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://{}:{}@hub.lambdatest.com/wd/hub".format(username, access_key) desired_caps['build'] = build desired_caps['name'] = test_name desired_caps['video']= True desired_caps['visual']= True desired_caps['network']= True desired_caps['console']= True 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import pytest import sys @pytest.mark.usefixtures('driver') class TestLink: def test_title(self, driver): """ Verify click and title of page :return: None """ driver.get('https://lambdatest.github.io/sample-todo-app/') driver.find_element_by_name("li1").click() driver.find_element_by_name("li2").click() title = "Sample page - lambdatest.com" assert title == driver.title def test_item(self, driver): """ Verify item submission :return: None """ driver.get('https://lambdatest.github.io/sample-todo-app/') 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) |
Below is the command using which you can run parallel test with pytest on LambdaTest Selenium Grid.
1 |
pytest -s -n=2 tests\lt_sample_todo.py |
In the above command, we have used -s to disable the output capture & -n is used to specify the number of parallel test execution.
To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda 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 Lambda Tunnel?
Follow our documentation on Lambda 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 “http://localhost/” or “http://localhost:8080” etc, you would get an error in these browsers. A possible solution is to use “localhost.lambdatest.com” or replace the string “localhost” with machine IP address. For example if you wanted to test “http://localhost/dashboard” or, and your machine IP is 192.168.2.6 you can instead test on “http://192.168.2.6/dashboard” or “http://localhost.lambdatest.com/dashboard”.
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 support@lambdatest.com.
Happy testing!