CAPABILITY GENERATOR

LambdaTest DocumentationAutomation Testing ● Python with Selenium – Running Python Automation Scripts on LambdaTest Selenium Grid

Python with Selenium – Running Python Automation Scripts 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 Java 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 Java-based automation test scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:

  1. How to run a sample Selenium with Python on LambdaTest Automation.
  2. How to set up an environment for testing your locally hosted websites or web pages.
  3. How to configure Python 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 For Running Python With Selenium


  • 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:
  • Download the driver from the link, or you can use pip to install it.
  • If you wish to test locally hosted or privately hosted projects, you would also need Lambda Tunnel binary file.

Getting Started With Python & LambdaTest


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. The JavaScript below tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc. You can also find this at our Git repository Python-UnitTest-Selenium.

Example of the Base Test

You can observe that basics for setting up a test to run on LambdaTest is simple.

Code Example

If you want to run the test remotely on the LambdaTest.

Execute The Test


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

Understanding & Configuring Python Desired Capabilities


As we said earlier the first step is to configure your test scripts to connect with LambdaTest Selenium automation gird. In the above mentioned Python code, the first thing you would notice is the invoking of remote webdriver instead of the native browser webdrivers. So for example if you are planning to run on Firefox browser in your local machine, you would be using Firefox browser driver like this:

However to run on LambdaTest Selenium grid, you would have to change it remote webdriver and at the same time pass capabilities related to browser, browser versions etc. Here is how it would look like:

You can define capabilities for invoking a specific Remote Web Chrome Driver using the below command.

Our Capabilities Generator will automatically provide you the program, based on your selections of test configurations. For Instance, if you pick the below configurations:

FIELDS
SELECTED VALUES
Operating System
Windows 10
Resolution
1920×1080
Browser
Google Chrome
Browser Version
71.0
Selenium Version
3.13.0

Then, LambdaTest Capabilities Generator will automatically generate the below program:

The most important capabilities to understand here are ‘browserName’, ‘versions’, and ‘platform’. They define which browser environment you wish to run the test on. Rest of the capabilities are important in test management and debugging. We have a inbuilt capabilities generator tool as well that you use to generate capabilities code for your test suite.

You can also check out our documentation on capabilities generator to understand more about individual capabilities.

Advanced LambdaTest Capabilities


In addition to default Selenium Grid capabilities, LambdaTest also has platform-specific capabilities like a video recording of test runs, console logs of each test run, network logs of each test run, custom profiles for specific browsers, etc. Do check out our documentation on LambdaTest Advanced capabilities as well.

Testing Locally Hosted or Privately Hosted Projects


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 test your locally hosted files before you make them live over the internet. You could even perform cross browser testing from different IP addresses belonging to various geographic locations. You can also use LambdaTest Tunnel to test web-apps and websites that are permissible inside your corporate firewall. The SSH tunnel provided by LambdaTest acts as a proxy server for hosting your web pages from your local machine to Virtual machines running on LambdaTest cloud servers. 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.

You can start tunnel by executing the below command in your cmd(Command Prompt) / terminal. Make sure that your pointer is routed in the same directory where you extract the downloaded Lambda Tunnel zip file.

So for example, if your user login email is example@lambdatest.com and your user key is 123asd123, then the command would be:

Once, the tunnel is successfully set up. You can add the below code to your capabilities for testing internal servers on your network.

You can also add the capability using LambdaTest Capabilities Generator.

Tunnel Reference: For a full reference of all tunnel features we support, visit our tunnel documentation.

Mark Tests As Pass Or Fail


While performing automation testing using Selenium with Python on LambdaTest Selenium grid, it is imperative to flag an automation test as either pass or fails. This is completely dependent on your testing requirement with respect to the validation of expected behaviour. You can mark whether an automation test passed or failed by adding the below code fragment in your automation script:

Queuing


To prevent over-exploitation of the platform we have added a capacity constraint on the number of tests that can be queued at our platform. Your maximum queue capacity will be dependent upon the number of concurrent sessions your LambdaTest account is eligible for. Below formula will help you calculate the maximum capacity for your queue.

Maximum Queuing Capacity

Maximum number of parallel tests that can be queued = n + 150

Here, n = number of concurrent sessions.

For example, if you have opted for pricing where you can avail up to 10 concurrent sessions. Then the maximum number of parallel test cases that can be queued will be equal to 10 + 150 i.e. 160 parallel test cases. Queuing allows you to manage your test pipeline in a hassle-free manner. LambdaTest would automatically schedule and execute test cases for you.

LambdaTest Queuing Policy

Here you can get more information about LambdaTest Queuing Policy.

Queuing Timeout

There is also a limit on how long a test case can stay in the queue. If your test case stays in the queue for more than 15 minutes, the test case would be timed out and would not be executed.