├── README.md └── lambdatest.py /README.md: -------------------------------------------------------------------------------- 1 | # How to start tunnel for automation test in Python-selenium on [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Python-selenium-tunnel) 2 | 3 | If you want to run your an automation test in Python-selenium on LambdaTest using the tunnel and want to start the tunnel automatically in the script, you can use the follwing steps. You can refer to sample test repo [here](https://github.com/LambdaTest/python-selenium-sample). 4 | 5 | ## Steps 6 | 7 | ### Step 1: Download the LT tunnel binary from LT Dashboard 8 | 9 | Go to your LambdaTest dashboard and click the configure tunnel button on the top right corner and use the download link to download the binary file. 10 | 11 | ### Step 2: Code to run LT tunnel before test 12 | 13 | The tunnel should be started before test case execution and end after completion. To achieve this, tunnel can be started in the main function like so: 14 | 15 | 16 | ```python 17 | import subprocess 18 | 19 | if __name__ == "__main__": 20 | 21 | #start tunnel process 22 | tunnel_process = subprocess.Popen(["./LT","--user",username,"--key",access_key],stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT) 23 | 24 | #run testcases 25 | unittest.main() 26 | 27 | #end tunnel 28 | tunnel_process.terminate() 29 | ``` 30 | ### Step 3: Set tunnel capability to True 31 | 32 | Add the `"tunnel": True` capability in your test file: 33 | 34 | ```python 35 | desired_caps = { 36 | 'LT:Options': { 37 | "build": "Python Demo", # Change your build name here 38 | "name": "Python Demo Test", # Change your test name here 39 | "platformName": "Windows 11", 40 | "selenium_version": "4.0.0", 41 | "tunnel": True 42 | }, 43 | "browserName": "Chrome", 44 | "browserVersion": "98.0", 45 | } 46 | 47 | ``` 48 | 49 | ### Step 4: Run your test 50 | 51 | ```bash 52 | python lambdatest.py 53 | ``` 54 | 55 | 56 | # Links: 57 | 58 | [LambdaTest Community](http://community.lambdatest.com/) 59 | 60 | -------------------------------------------------------------------------------- /lambdatest.py: -------------------------------------------------------------------------------- 1 | import os 2 | import unittest 3 | import sys 4 | from selenium import webdriver 5 | 6 | username = os.environ.get("LT_USERNAME") 7 | access_key = os.environ.get("LT_ACCESS_KEY") 8 | 9 | 10 | class FirstSampleTest(unittest.TestCase): 11 | 12 | # setUp runs before each test case 13 | def setUp(self): 14 | desired_caps = { 15 | 'LT:Options': { 16 | "user": username, 17 | "accessKey": access_key, 18 | "build": "UnitTest-Selenium-Sample", 19 | "name": "UnitTest-Selenium-Test", 20 | "platformName": "Windows 11", 21 | "selenium_version": "4.0.0", 22 | "tunnel": True, 23 | }, 24 | "browserName": "Chrome", 25 | "browserVersion": "latest", 26 | } 27 | 28 | self.driver = webdriver.Remote( 29 | command_executor="http://hub.lambdatest.com:80/wd/hub", 30 | desired_capabilities=desired_caps) 31 | 32 | 33 | # tearDown runs after each test case 34 | 35 | def tearDown(self): 36 | self.driver.quit() 37 | 38 | def scroll_bottom(): 39 | """ 40 | Verify scrolling to bottom 41 | :return: None 42 | """ 43 | driver.get('https://www.lambdatest.com/') 44 | driver.maximize_window() 45 | sleep(2) 46 | success = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 47 | sleep(5) 48 | 49 | 50 | if __name__ == "__main__": 51 | 52 | #start tunnel process 53 | tunnel_process = subprocess.Popen(["./LT","--user",username,"--key",access_key],stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT) 54 | 55 | #run testcases 56 | unittest.main() 57 | 58 | #end tunnel 59 | tunnel_process.terminate() 60 | --------------------------------------------------------------------------------