LambdaTest ● Automation Testing ● Quick Guide To Run Python Tests on LambdaTest Selenium Grid

Quick Guide To Run Python Tests 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 leverage LambdaTest Selenium grid to not only decrease the build time of Python automation scripts, but also increase your test coverage and check for more range of browsers for cross browser compatibility. This post will help you in getting started with configuring and running your Python based automation test scripts on LambdaTest Selenium cloud platform.

Prerequisites

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 the below command:

curl https://bootstrap.pypa.io/get-pip.py | python 1 curl https : //bootstrap.pypa.io/get-pip.py | python

Installing Selenium Dependencies For Python

Make sure that you have installed Selenium dependencies before executing your tests. You can use pip to install Selenium using following command:

pip install selenium export PYTHONWARNINGS="ignore:Unverified HTTPS request" //Disable SSL warning 1 2 pip install selenium export PYTHONWARNINGS = "ignore:Unverified HTTPS request" //Disable SSL warning

Sample Python and Selenium Test To Get You started

Now we are all set to execute the first Python test on LambdaTest Selenium Grid. Checkout the sample code below. This is a simple python code that uses LambdaTest Selenium grid to open up Google. You can also download it from our GitHub repo here.

Sample Python and Selenium Automation Script

python google-search-lambdatest.py """ LambdaTest Selenium automation sample example Configuration ---------- username: Username can be found at automation dashboard accessToken: AccessToken can be generated from automation dashboard or profile section Result ------- Execute Python Automation Tests on LambdaTest Distributed Selenium Grid """ import unittest import time from selenium import webdriver from selenium.webdriver.common.keys import Keys class LTAutomate(unittest.TestCase): """ Setup remote driver Params ---------- platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks) browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge) version : Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/ Result ------- """ def setUp(self): # username: Username can be found at automation dashboard username="{username}" # accessToken: AccessToken can be generated from automation dashboard or profile section accessToken="{accessToken}" # gridUrl: gridUrl can be found at automation dashboard gridUrl = "hub.lambdatest.com/wd/hub" desired_cap = { 'platform' : "win10", 'browserName' : "chrome", 'version' : "67.0", # Resolution of machine "resolution": "1024x768", "name": "LambdaTest python google search test ", "build": "LambdaTest python google search build", "network": True, "video": True, "visual": True, "console": True, } # URL: https://{username}:{accessToken}@hub.lambdatest.com/wd/hub url = "https://"+username+":"+accessToken+"@"+gridUrl print("Initiating remote driver on platform: "+desired_cap["platform"]+" browser: "+desired_cap["browserName"]+" version: "+desired_cap["version"]) self.driver = webdriver.Remote( desired_capabilities=desired_cap, command_executor= url ) """ Setup remote driver Params ---------- Execute test: navigate google.com search LambdaTest Result ------- print title """ def test_search_in_google(self): driver = self.driver print("Driver initiated successfully. Navigate url") driver.get("https://www.google.com/ncr") print("Searching lambdatest on google.com ") time.sleep(8) elem = driver.find_element_by_name("q") elem.send_keys("lambdatest.com") elem.submit() print("Printing title of current page :"+driver.title) driver.execute_script("lambda-status=passed") print("Requesting to mark test : pass") """ Quit selenium driver """ def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main() 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 python google - search - lambdatest . py "" " LambdaTest Selenium automation sample example Configuration ---------- username: Username can be found at automation dashboard accessToken: AccessToken can be generated from automation dashboard or profile section Result ------- Execute Python Automation Tests on LambdaTest Distributed Selenium Grid " "" import unittest import time from selenium import webdriver from selenium . webdriver . common . keys import Keys class LTAutomate ( unittest . TestCase ) : "" " Setup remote driver Params ---------- platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks) browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge) version : Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/ Result ------- " "" def setUp ( self ) : # username: Username can be found at automation dashboard username = "{username}" # accessToken: AccessToken can be generated from automation dashboard or profile section accessToken = "{accessToken}" # gridUrl: gridUrl can be found at automation dashboard gridUrl = "hub.lambdatest.com/wd/hub" desired_cap = { 'platform' : "win10" , 'browserName' : "chrome" , 'version' : "67.0" , # Resolution of machine "resolution" : "1024x768" , "name" : "LambdaTest python google search test " , "build" : "LambdaTest python google search build" , "network" : True , "video" : True , "visual" : True , "console" : True , } # URL: https://{username}:{accessToken}@hub.lambdatest.com/wd/hub url = "https://" + username + ":" + accessToken + "@" + gridUrl print ( "Initiating remote driver on platform: " + desired_cap [ "platform" ] + " browser: " + desired_cap [ "browserName" ] + " version: " + desired_cap [ "version" ] ) self . driver = webdriver . Remote ( desired_capabilities = desired_cap , command_executor = url ) "" " Setup remote driver Params ---------- Execute test: navigate google.com search LambdaTest Result ------- print title " "" def test_search_in_google ( self ) : driver = self . driver print ( "Driver initiated successfully. Navigate url" ) driver . get ( "https://www.google.com/ncr" ) print ( "Searching lambdatest on google.com " ) time . sleep ( 8 ) elem = driver . find_element_by_name ( "q" ) elem . send_keys ( "lambdatest.com" ) elem . submit ( ) print ( "Printing title of current page :" + driver . title ) driver . execute_script ( "lambda-status=passed" ) print ( "Requesting to mark test : pass" ) "" " Quit selenium driver " "" def tearDown ( self ) : self . driver . quit ( ) if __name__ == "__main__" : unittest . main ( )

You can execute the test from your terminal through this command.

python google-search-lambdatest.py 1 python google - search - lambdatest . py

The first thing to note here, is the use of remote WebDriver. Also checkout how the test passed on the enthronement configuration via desired_cap class.

To use your choice of environment for the test you can leverage our Capability Generator tool.