Skip to content

Commit 89d3d29

Browse files
authored
Merge pull request #16 from 4DvAnCeBoY/master
selenium 3 example
2 parents e9badea + 4ba8b2b commit 89d3d29

File tree

7 files changed

+327
-9
lines changed

7 files changed

+327
-9
lines changed

.gitpod.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
22
tasks:
3-
- init: pip install selenium && export PYTHONWARNINGS="ignore:Unverified HTTPS request" # runs during prebuild
3+
- init: pip install -r requirements.txt && export PYTHONWARNINGS="ignore:Unverified HTTPS request" # runs during prebuild
44
command: python lambdatest.py
55

66

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ cd python-selenium-sample
5050

5151
**Step 2:** Download the driver from the link, or you can use **pip** to install it.
5252
```bash
53-
pip install selenium
53+
pip install -r requirements.txt
5454
export PYTHONWARNINGS="ignore:Unverified HTTPS request" //Disable ssl warning
5555
```
5656

@@ -84,7 +84,7 @@ The capabilities in the below code are defined as:
8484

8585
```python
8686
options = ChromeOptions()
87-
options.browser_version = "114.0"
87+
options.browser_version = "latest"
8888
options.platform_name = "macOS High Sierra"
8989
lt_options = {}
9090
lt_options["video"] = True

lambdatest.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
options = ChromeOptions()
1414
options.browser_version = "latest"
15-
options.platform_name = "win10"
15+
options.platform_name = "Windows 10"
1616
lt_options = {}
1717
lt_options["username"] = username
1818
lt_options["accessKey"] = access_key
19-
lt_options["video"] = True
20-
lt_options["resolution"] = "1920x1080"
19+
# lt_options["video"] = True
20+
# lt_options["resolution"] = "1920x1080"
2121
lt_options["network"] = True
22-
lt_options["build"] = "test_build"
23-
lt_options["project"] = "unit_testing"
22+
lt_options["build"] = "Selenium 4 Example"
23+
# lt_options["project"] = "unit_testing"
2424
lt_options["smartUI.project"] = "test"
25-
lt_options["name"] = "basic_unit_selinium"
25+
lt_options["name"] = "Selenium 4 Sample Test"
2626
lt_options["w3c"] = True
2727
lt_options["plugin"] = "python-python"
2828
options.set_capability("LT:Options", lt_options)

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
urllib3==1.26.5
2+
selenium==4.25.0

selenium-3/README.md

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# Run Selenium Tests With Python On LambdaTest
2+
3+
![171934563-4806efd2-1154-494c-a01d-1def95657383 (1)](https://user-images.githubusercontent.com/70570645/172273386-fa9606ac-3e63-4b2e-8978-3142add3e038.png)
4+
5+
6+
<p align="center">
7+
<a href="https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample" target="_bank">Blog</a>
8+
&nbsp; &#8901; &nbsp;
9+
<a href="https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample" target="_bank">Docs</a>
10+
&nbsp; &#8901; &nbsp;
11+
<a href="https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample" target="_bank">Learning Hub</a>
12+
&nbsp; &#8901; &nbsp;
13+
<a href="https://www.lambdatest.com/newsletter/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample" target="_bank">Newsletter</a>
14+
&nbsp; &#8901; &nbsp;
15+
<a href="https://www.lambdatest.com/certifications/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample" target="_bank">Certifications</a>
16+
&nbsp; &#8901; &nbsp;
17+
<a href="https://www.youtube.com/c/LambdaTest" target="_bank">YouTube</a>
18+
</p>
19+
&emsp;
20+
&emsp;
21+
&emsp;
22+
23+
*Learn how to run your Python automation testing scripts on the LambdaTest platform*
24+
25+
[<img height="58" width="200" src="https://user-images.githubusercontent.com/70570645/171866795-52c11b49-0728-4229-b073-4b704209ddde.png">](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 Python](#testing-locally-hosted-or-privately-hosted-projects)
33+
34+
## Prerequisites
35+
36+
Before you can start performing **Python** automation testing with **Selenium**, 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+
42+
### Installing Selenium Dependencies And Tutorial Repo
43+
44+
**Step 1:** Clone the LambdaTest’s python-selenium-sample repository and navigate to the code directory as shown below:
45+
46+
```bash
47+
git clone https://github.com/LambdaTest/python-selenium-sample
48+
cd python-selenium-sample/selenium-3
49+
```
50+
51+
**Step 2:** Download the driver from the link, or you can use **pip** to install it.
52+
```bash
53+
pip install -r requirements.txt
54+
export PYTHONWARNINGS="ignore:Unverified HTTPS request" //Disable ssl warning
55+
```
56+
57+
### Setting Up Your Authentication
58+
59+
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-selenium-sample) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample).
60+
61+
**Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables.
62+
63+
* For **Linux/macOS**:
64+
65+
```bash
66+
export LT_USERNAME="YOUR_USERNAME"
67+
export LT_ACCESS_KEY="YOUR ACCESS KEY"
68+
```
69+
* For **Windows**:
70+
```bash
71+
set LT_USERNAME="YOUR_USERNAME"
72+
set LT_ACCESS_KEY="YOUR ACCESS KEY"
73+
```
74+
75+
## Run Your First Test
76+
77+
>**Test Scenario**: The [lambdatest.py](https://github.com/LambdaTest/python-selenium-sample/blob/master/lambdatest.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.
78+
79+
### Configuration Of Your Test Capabilities
80+
81+
**Step 4:** In the python 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.
82+
83+
The capabilities in the below code are defined as:
84+
85+
```python
86+
capability = {
87+
"browserName": "Chrome",
88+
"browserVersion": "latest",
89+
"LT:Options": {
90+
"username": username,
91+
"accessKey": access_key,
92+
"platformName": "Windows 10",
93+
"build": "Selenium 3 Example"
94+
}
95+
}
96+
```
97+
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-selenium-sample).
98+
99+
### Executing The Test
100+
101+
**Step 5:** You would need to execute the below command in your terminal/cmd.
102+
103+
```bash
104+
python lambdatest.py
105+
```
106+
For python3 use
107+
```bash
108+
python3 lambdatest.py
109+
```
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.
112+
113+
114+
## Testing Locally Hosted Or Privately Hosted Projects
115+
116+
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.
117+
118+
Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information.
119+
120+
Here’s how you can establish LambdaTest Tunnel.
121+
122+
Download the binary file of:
123+
* [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip)
124+
* [LambdaTest Tunnel for macOS](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip)
125+
* [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip)
126+
127+
Open command prompt and navigate to the binary folder.
128+
129+
Run the following command:
130+
131+
```bash
132+
LT -user {user’s login email} -key {user’s access key}
133+
```
134+
So if your user name is [email protected] and key is 123456, the command would be:
135+
136+
```bash
137+
LT -user [email protected] -key 123456
138+
```
139+
Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below :
140+
141+
**Tunnel Capability**
142+
143+
```
144+
"tunnel" : true
145+
```
146+
147+
## Tutorials 📙
148+
149+
Check out our latest tutorials on Python automation testing 👇
150+
151+
* [Why Python Is A Preferred Language For Test Automation?](https://www.lambdatest.com/blog/python-automation-testing/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
152+
* [7 Best Python Testing Frameworks](https://www.lambdatest.com/blog/top-python-frameworks-in-2020-for-selenium-test-automation/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
153+
* [Selenium 4 With Python: All You Need To Know](https://www.lambdatest.com/blog/selenium-with-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
154+
* [How to Set Up Selenium With Python for Modern Web Automation](https://www.lambdatest.com/blog/selenium-webdriver-with-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
155+
* [Using Selenium and Python Hypothesis for Automation Testing](https://www.lambdatest.com/blog/using-selenium-and-python-hypothesis-for-automation-testing/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
156+
* [Selenium Python Tutorial: Getting Started With BDD In Behave](https://www.lambdatest.com/blog/selenium-python-behave-tutorial-bdd/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
157+
* [Selenium Python Tutorial: Getting Started With Pytest](https://www.lambdatest.com/blog/selenium-python-pytest-testing-tutorial/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
158+
* [Selenium Python Tutorial: Running First PyUnit Script](https://www.lambdatest.com/blog/using-pyunit-for-testing-a-selenium-python-test-suite/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
159+
* [Robot Framework with Selenium and Python](https://www.lambdatest.com/blog/robot-framework-tutorial/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
160+
* [Getting Started With Selenium Python [Tutorial]](https://www.lambdatest.com/blog/robot-framework-tutorial/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
161+
* [Running Python Selenium Test in Parallel With PyTest](https://www.lambdatest.com/blog/robot-framework-tutorial/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
162+
* [Parallel Testing In Selenium WebDriver With Python Using Unittest](https://www.lambdatest.com/blog/robot-framework-tutorial/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
163+
* [Automated Browser Testing with Opera and Selenium in Python](https://www.lambdatest.com/blog/automated-browser-testing-with-opera-and-selenium-in-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
164+
* [How To Take A Screenshot Using Python & Selenium?](https://www.lambdatest.com/blog/python-selenium-screenshots/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
165+
* [How To Drag And Drop In Selenium With Python?](https://www.lambdatest.com/blog/drag-and-drop-in-selenium-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
166+
* [How To Switch Tabs In A Browser Using Selenium Python?](https://www.lambdatest.com/blog/python-selenium-switch-tabs/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
167+
* [How To Get Page Source In Selenium Using Python?](https://www.lambdatest.com/blog/how-to-get-page-source-in-selenium-webdriver/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
168+
* [How To Handle Dropdowns In Selenium WebDriver Using [Python?]](https://www.lambdatest.com/blog/handling-dropdown-in-selenium-webdriver-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
169+
* [How To Do Parameterization In Pytest With Selenium?](https://www.lambdatest.com/blog/parameterization-in-pytest-with-selenium/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
170+
* [Page Object Model (POM) In Selenium Python](https://www.lambdatest.com/blog/page-object-model-in-selenium-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
171+
* [How To Read Configuration Files in Python Using Selenium](https://www.lambdatest.com/blog/how-to-read-configuration-files-in-python-using-selenium/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
172+
* [How To Create an Automated Web Bot With Selenium in Python?](https://www.lambdatest.com/blog/automated-web-bot-with-selenium-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
173+
* [How To Perform Web Scraping Using Selenium and Python?](https://www.lambdatest.com/blog/web-scraping-using-selenium-and-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
174+
* [Adding Firefox Extensions With Selenium in Python](https://www.lambdatest.com/blog/adding-firefox-extensions-with-selenium-in-python/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
175+
* [How to Handle JavaScript Alert in Selenium WebDriver Using Python?](https://www.lambdatest.com/blog/how-to-handle-javascript-alert-in-selenium-webdriver/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
176+
* [Use Selenium Wait for Page to Load With Python](https://www.lambdatest.com/blog/selenium-wait-for-page-to-load/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
177+
* [Selenium Python Cheat Sheet for Test Automation](https://www.lambdatest.com/blog/selenium-python-cheat-sheet/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
178+
179+
180+
## Documentation & Resources :books:
181+
182+
183+
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.
184+
185+
* [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
186+
* [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
187+
* [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
188+
189+
## LambdaTest Community :busts_in_silhouette:
190+
191+
The [LambdaTest Community](https://community.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample) 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 🌎
192+
193+
## What's New At LambdaTest ❓
194+
195+
To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/)
196+
197+
## About LambdaTest
198+
199+
[LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample) 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.
200+
201+
### Features
202+
203+
* Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments.
204+
* Real-time cross browser testing on 3000+ environments.
205+
* Test on Real device cloud
206+
* Blazing fast test automation with HyperExecute
207+
* Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale.
208+
* Smart Visual Regression Testing on cloud
209+
* 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more.
210+
* Automated Screenshot testing across multiple browsers in a single click.
211+
* Local testing of web and mobile apps.
212+
* Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems.
213+
* Geolocation testing of web and mobile apps across 53+ countries.
214+
* LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports
215+
216+
217+
[<img height="58" width="200" src="https://user-images.githubusercontent.com/70570645/171866795-52c11b49-0728-4229-b073-4b704209ddde.png">](https://accounts.lambdatest.com/register)
218+
219+
220+
221+
## We are here to help you :headphones:
222+
223+
* Got a query? we are available 24x7 to help. [Contact Us]([email protected]/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
224+
* For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=python-selenium-sample)
225+

selenium-3/lambdatest.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import unittest
2+
import os
3+
from selenium import webdriver
4+
from selenium.webdriver.common.by import By
5+
from selenium.webdriver.chrome.options import Options as ChromeOptions
6+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
7+
import warnings
8+
9+
username = os.getenv("LT_USERNAME") # Replace the username
10+
access_key = os.getenv("LT_ACCESS_KEY") # Replace the access key
11+
12+
13+
capability = {
14+
"browserName": "Chrome",
15+
"browserVersion": "latest",
16+
"LT:Options": {
17+
"username": username,
18+
"accessKey": access_key,
19+
"platformName": "Windows 10",
20+
"build": "Selenium 3 Example",
21+
"name" : "Selenium 3 Sample Test"
22+
}
23+
}
24+
25+
def suppress_resource_warnings():
26+
warnings.filterwarnings("ignore", category=ResourceWarning)
27+
28+
class FirstSampleTest(unittest.TestCase):
29+
driver = None
30+
31+
def setUp(self):
32+
suppress_resource_warnings()
33+
self.driver = webdriver.Remote(
34+
command_executor = "http://{}:{}@hub.lambdatest.com/wd/hub".format(
35+
username, access_key
36+
),
37+
# options=options,
38+
desired_capabilities = capability
39+
)
40+
41+
# """ You can write the test cases here """
42+
def test_demo_site(self):
43+
suppress_resource_warnings()
44+
driver = self.driver
45+
driver.implicitly_wait(10)
46+
driver.set_page_load_timeout(30)
47+
# driver.set_window_size(1920, 1080)
48+
49+
# Url
50+
print("Loading URL")
51+
driver.get(
52+
"https://lambdatest.github.io/sample-todo-app/"
53+
)
54+
55+
# Let's click on a element
56+
driver.find_element(By.NAME, "li1").click()
57+
location = driver.find_element(By.NAME, "li2")
58+
location.click()
59+
print("Clicked on the second element")
60+
61+
# Let's add a checkbox
62+
driver.find_element(By.ID, "sampletodotext").send_keys("LambdaTest")
63+
add_button = driver.find_element(By.ID, "addbutton")
64+
add_button.click()
65+
print("Added LambdaTest checkbox")
66+
67+
# print the heading
68+
search = driver.find_element(By.CSS_SELECTOR, ".container h2")
69+
assert search.is_displayed(), "heading is not displayed"
70+
print(search.text)
71+
search.click()
72+
driver.implicitly_wait(3)
73+
74+
# Let's download the invoice
75+
heading = driver.find_element(By.CSS_SELECTOR, ".container h2")
76+
if heading.is_displayed():
77+
heading.click()
78+
driver.execute_script("lambda-status=passed")
79+
print("Tests are run successfully!")
80+
else:
81+
driver.execute_script("lambda-status=failed")
82+
83+
# tearDown runs after each test case
84+
def tearDown(self):
85+
self.driver.quit()
86+
87+
88+
if __name__ == "__main__":
89+
unittest.main()

selenium-3/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
urllib3==1.26.5
2+
selenium==3.141.0

0 commit comments

Comments
 (0)