├── .gitignore
├── .gitpod.yml
├── README.md
├── dashboard.png
├── lambdatest_test.py
└── requirements.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | __pycache__
--------------------------------------------------------------------------------
/.gitpod.yml:
--------------------------------------------------------------------------------
1 | # This configuration file was automatically generated by Gitpod.
2 | # Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
3 | # and commit this file to your remote git repository to share the goodness with others.
4 |
5 | tasks:
6 | - init: pip install -r requirements.txt
7 | command: nosetests lambdatest_test.py
8 | vscode:
9 | extensions:
10 | - ms-python.python
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Run Selenium Tests With UnitTest On LambdaTest
2 |
3 | 
4 |
5 |
6 |
7 | Blog
8 | ⋅
9 | Docs
10 | ⋅
11 | Learning Hub
12 | ⋅
13 | Newsletter
14 | ⋅
15 | Certifications
16 | ⋅
17 | YouTube
18 |
19 |
20 |
21 |
22 |
23 | *Learn how to run your Python automation testing scripts using UnitTest on the LambdaTest platform*
24 |
25 | [
](https://accounts.lambdatest.com/register)
26 |
27 |
28 | ## Table Of Contents
29 |
30 | * [Pre-requisites](#pre-requisites)
31 | * [Run Your First Test](#run-your-first-test)
32 | * [Local Testing With UnitTest](#testing-locally-hosted-or-privately-hosted-projects)
33 |
34 | ## Prerequisites To Perform Unit testing In Python with Selenium
35 |
36 | Before you can start performing **Python** automation testing using **UnitTest**, you would need to:
37 |
38 | * Install the latest Python build from the [official website](https://www.python.org/downloads/). We recommend using the latest version.
39 | * Make sure **pip** is installed in your system. You can install **pip** from [here](https://pip.pypa.io/en/stable/installation/).
40 | * Download the latest **Selenium Client** and its **WebDriver bindings** from the [official website](https://www.selenium.dev/downloads/). Latest versions of **Selenium Client** and **WebDriver** are ideal for running your automation script on LambdaTest Selenium cloud grid.
41 | * Install **virtualenv** which is the recommended way to run your tests. 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.
42 |
43 | ```bash
44 | pip install virtualenv
45 | ```
46 | ### Installing Selenium Dependencies And Tutorial Repo
47 |
48 | **Step 1:** Clone the LambdaTest’s Python-UnitTest-Selenium repository and navigate to the code directory as shown below:
49 | ```bash
50 | git clone https://github.com/LambdaTest/Python-UnitTest-Selenium
51 | cd Python-UnitTest-Selenium
52 | ```
53 | **Step 2:** Create a virtual environment in your project folder the environment name is arbitrary.
54 | ```bash
55 | virtualenv venv
56 | ```
57 | **Step 3:** Activate the environment.
58 | ```bash
59 | source venv/bin/activate
60 | ```
61 | **Step 4:** Install the [required packages](https://github.com/LambdaTest/Python-UnitTest-Selenium/blob/master/requirements.txt) from the cloned project directory:
62 |
63 | ```bash
64 | pip install -r requirements.txt
65 | ```
66 | ### Setting Up Your Authentication
67 |
68 | Make sure you have your LambdaTest credentials with you to run test automation scripts. You can get these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium).
69 |
70 | **Step 5:** Set LambdaTest **Username** and **Access Key** in environment variables.
71 |
72 | * For **Linux/macOS**:
73 |
74 | ```bash
75 | export LT_USERNAME="YOUR_USERNAME"
76 | export LT_ACCESS_KEY="YOUR ACCESS KEY"
77 | ```
78 | * For **Windows**:
79 | ```bash
80 | set LT_USERNAME="YOUR_USERNAME"
81 | set LT_ACCESS_KEY="YOUR ACCESS KEY"
82 | ```
83 |
84 | ## Run Your First Test
85 |
86 | >**Test Scenario**: The [lambdatest_test.py](https://github.com/LambdaTest/pytest-selenium-sample/blob/master/tests/lt_sample_todo.py) sample script tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc.
87 |
88 |
89 | ### Configuration of Your Test Capabilities
90 |
91 | **Step 6:** In the test script, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:
92 |
93 | ```python
94 | capabilities = {
95 | "build": "UnitTest-Selenium-Sample",
96 | "name": "UnitTest-Selenium-Test",
97 | "platformName": "Windows 11",
98 | "browserName": "Chrome",
99 | "browserVersion": "latest",
100 | }
101 | ```
102 | You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium).
103 |
104 |
105 | ### Executing The Test
106 |
107 | **Step 7:** Please execute the command below to run your tests:
108 | ```bash
109 | nosetests lambdatest_test.py
110 | ```
111 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard. [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium) will help you view all your text logs, screenshots and video recording for your entire automation tests.
112 |
113 | ## Testing Locally Hosted Or Privately Hosted Projects
114 |
115 | You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid using LambdaTest Tunnel. All you would have to do is set up an SSH tunnel using tunnel and pass toggle `tunnel = True` via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are live.
116 |
117 | Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information.
118 |
119 | Here’s how you can establish LambdaTest Tunnel.
120 |
121 | Download the binary file of:
122 | * [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip)
123 | * [LambdaTest Tunnel for macOS](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip)
124 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip)
125 |
126 | Open command prompt and navigate to the binary folder.
127 |
128 | Run the following command:
129 |
130 | ```bash
131 | LT -user {user’s login email} -key {user’s access key}
132 | ```
133 | So if your user name is lambdatest@example.com and key is 123456, the command would be:
134 |
135 | ```bash
136 | LT -user lambdatest@example.com -key 123456
137 | ```
138 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below :
139 |
140 | **Tunnel Capability**
141 |
142 | ```
143 | "tunnel" : true
144 | ```
145 |
146 | ## Documentation & Resources :books:
147 |
148 |
149 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing.
150 |
151 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium)
152 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium)
153 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium)
154 |
155 | ## LambdaTest Community :busts_in_silhouette:
156 |
157 | The [LambdaTest Community](https://community.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎
158 |
159 | ## What's New At LambdaTest ❓
160 |
161 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/)
162 |
163 | ## About LambdaTest
164 |
165 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs.
166 |
167 | ### Features
168 |
169 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments.
170 | * Real-time cross browser testing on 3000+ environments.
171 | * Test on Real device cloud
172 | * Blazing fast test automation with HyperExecute
173 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale.
174 | * Smart Visual Regression Testing on cloud
175 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more.
176 | * Automated Screenshot testing across multiple browsers in a single click.
177 | * Local testing of web and mobile apps.
178 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems.
179 | * Geolocation testing of web and mobile apps across 53+ countries.
180 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports
181 |
182 |
183 | [
](https://accounts.lambdatest.com/register)
184 |
185 |
186 |
187 | ## We are here to help you :headphones:
188 |
189 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium)
190 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-unittest-selenium)
191 |
192 |
--------------------------------------------------------------------------------
/dashboard.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LambdaTest/Python-UnitTest-Selenium/1ac19941fe61a990d6965d2f2093371671fce2f2/dashboard.png
--------------------------------------------------------------------------------
/lambdatest_test.py:
--------------------------------------------------------------------------------
1 | import os
2 | import unittest
3 | import sys
4 | from selenium import webdriver
5 | from selenium.webdriver.chrome.options import Options as ChromeOptions
6 | from selenium.webdriver.edge.options import Options as EdgeOptions
7 | from selenium.webdriver.common.by import By
8 |
9 | username = os.environ.get("LT_USERNAME")
10 | access_key = os.environ.get("LT_ACCESS_KEY")
11 |
12 |
13 | class FirstSampleTest(unittest.TestCase):
14 |
15 | # setUp runs before each test case
16 | def setUp(self):
17 | lt_options = {
18 | "user": username,
19 | "accessKey": access_key,
20 | "build": "UnitTest-Selenium-Sample",
21 | "name": "UnitTest-Selenium-Test",
22 | "platformName": "MacOS Ventura",
23 | "w3c": True,
24 | "browserName": "Edge",
25 | "browserVersion": "latest-2",
26 | "selenium_version": "4.8.0"
27 | }
28 |
29 | browser_options = EdgeOptions()
30 | browser_options.set_capability('LT:Options', lt_options)
31 |
32 | # Steps to run Smart UI project (https://beta-smartui.lambdatest.com/)
33 | # Step - 1 : Change the hub URL to @beta-smartui-hub.lambdatest.com/wd/hub
34 | # Step - 2 : Add "smartUI.project": "" as a capability above
35 | # Step - 3 : Run "driver.execute_script("smartui.takeScreenshot")" command wherever you need to take a screenshot
36 | # Note: for additional capabilities navigate to https://www.lambdatest.com/support/docs/test-settings-options/
37 |
38 | self.driver = webdriver.Remote(
39 | command_executor="http://hub.lambdatest.com:80/wd/hub",
40 | options=browser_options)
41 |
42 |
43 | # tearDown runs after each test case
44 |
45 | def tearDown(self):
46 | self.driver.quit()
47 |
48 | def test_unit_user_should_able_to_add_item(self):
49 | # try:
50 | driver = self.driver
51 |
52 | # Url
53 | driver.get("https://lambdatest.github.io/sample-todo-app/")
54 |
55 | # Click on check box
56 | check_box_one = driver.find_element(By.NAME,"li1")
57 | check_box_one.click()
58 |
59 | # Click on check box
60 | check_box_two = driver.find_element(By.NAME,"li2")
61 | check_box_two.click()
62 |
63 | #Take Smart UI screenshot
64 | #driver.execute_script("smartui.takeScreenshot")
65 |
66 | # Enter item in textfield
67 | textfield = driver.find_element(By.ID,"sampletodotext")
68 | textfield.send_keys("Yey, Let's add it to list")
69 |
70 | # Click on add button
71 | add_button = driver.find_element(By.ID,"addbutton")
72 | add_button.click()
73 |
74 | # Verified added item
75 | added_item = driver.find_element(By.XPATH,
76 | "//span[@class='done-false']").text
77 | print(added_item)
78 |
79 |
80 | if __name__ == "__main__":
81 | unittest.main()
82 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | ConfigParser
2 | selenium<=4.9.0
3 | pytest
4 | nose
5 | pytest-xdist
6 |
--------------------------------------------------------------------------------