Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 107e3d2

Browse files
Merge pull request #2 from Apoorvlt/master
Python-UnitTest-Selenium
2 parents 69ec723 + de0cf47 commit 107e3d2

File tree

3 files changed

+256
-81
lines changed

3 files changed

+256
-81
lines changed

‎README.md

Lines changed: 143 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,159 @@
22
![LambdaTest Logo](https://www.lambdatest.com/static/images/logo.svg)
33
---
44

5-
### Environment Setup
6-
7-
1. Global Dependencies
8-
9-
-Windows
10-
* Download the latest python installer for Windows: http://sourceforge.net/projects/pywin32/files/pywin32/
11-
* Run the installer and follow the setup wizard to install Python
12-
13-
-Linux/Mac
14-
* Run python --version to see which python version is currently installed, make sure it is 2.5.X or above.
15-
* OS X, Ubuntu and most other Linux distro's come with Python pre-installed.
16-
17-
2. Lambdatest Credentials
18-
* Set LambdaTest username and access key in environment variables. It can be obtained from [LambdaTest dashboard](https://automation.lambdatest.com/)
19-
example:
20-
- For linux/mac
21-
```
22-
export LT_USERNAME="YOUR_USERNAME"
23-
export LT_ACCESS_KEY="YOUR ACCESS KEY"
24-
25-
```
26-
- For Windows
27-
```
28-
set LT_USERNAME="YOUR_USERNAME"
29-
set LT_ACCESS_KEY="YOUR ACCESS KEY"
30-
31-
```
32-
3. Setup
33-
* Clone [Python-UnitTest-Selenium](https://github.com/LambdaTest/Python-UnitTest-Selenium.git) from GitHub.
34-
* Navigate to the cloned directory
35-
* Install project dependencies by running command `pip install -r requirements.txt`
5+
![logo](https://github.com/Apoorvlt/test/blob/master/logo.PNG)
6+
7+
## Prerequisites for Python Behave tutorial
8+
9+
### 1. Python Installation
10+
11+
* [Download Python](https://www.python.org/downloads/) and click on Add to path and install.
12+
13+
* To check if python installed correctly you need to go to terminal type python in command prompt. It will show you the current version you have downloaded.
14+
15+
### 2. LambdaTest Credentials
16+
* To use Pytest with LambdaTest, make sure you have the 2 environment variables LT_USERNAME and LT_ACCESS_KEY set. To obtain a username and access_key, sign up for free [here](https://lambdatest.com). After signing up you can find your username and access key [here](https://accounts.lambdatest.com/detail/profile).
17+
* In the terminal export your LambdaTest Credentials as environmental variables:
18+
19+
* For Mac/Linux
20+
```
21+
$ export LT_USERNAME=<your LambdaTest username>
22+
$ export LT_ACCESS_KEY=<your LambdaTest access key>
23+
```
24+
25+
* For Windows
26+
```
27+
set LT_USERNAME=<your LambdaTest username>
28+
set LT_ACCESS_KEY=<your LambdaTest access key>
29+
```
30+
31+
### 3. Setup
32+
33+
* Clone [Python-UnitTest-Selenium](https://github.com/LambdaTest/Python-UnitTest-Selenium.git) from GitHub.
34+
* Navigate to the cloned directory
35+
* Install project dependencies by running command:
36+
37+
```
38+
pip install -r requirements.txt
39+
```
40+
41+
Requirements.txt file includes the following:
42+
43+
```
44+
ConfigParser
45+
selenium>2.5
46+
pytest
47+
nose
48+
pytest-xdist
49+
```
50+
## Test Scenario
51+
52+
### Single Test
53+
54+
In our demonstration, we will be creating a script that uses the Selenium WebDriver to click check boxes and add button. If assert returns true, it indicates that the test case passed successfully and will show up in the automation logs dashboard else if assert returns false, the test case fails, and the errors will be displayed in the automation logs.
55+
56+
You have successfully configured your project and are ready to execute your first UnitTest selenium testing script. Here is the file for UnitTest selenium Testing. Lets call it <code>lambdatest_test.py</code>.
57+
58+
```
59+
import unittest, time, re
60+
import base_test
61+
from selenium import webdriver
62+
63+
from selenium.webdriver.common.keys import Keys
64+
65+
class LambdaTest(base_test.BaseTest):
66+
67+
def test_unit_user_should_able_to_add_item(self):
68+
69+
# try:
70+
71+
driver = self.driver
72+
73+
# Url
74+
75+
driver.get(self.base_url)
76+
77+
# Click on check box
78+
79+
check_box_one = driver.find_element_by_name("li1")
80+
81+
check_box_one.click()
82+
83+
# Click on check box
84+
85+
check_box_two = driver.find_element_by_name("li2")
86+
87+
check_box_two.click()
88+
89+
# Enter item in textfield
90+
91+
textfield = driver.find_element_by_id("sampletodotext")
92+
93+
textfield.send_keys("Yey, Let's add it to list")
94+
95+
# Click on add button
96+
97+
add_button = driver.find_element_by_id("addbutton")
98+
99+
add_button.click()
100+
101+
# Verified added item
102+
103+
added_item = driver.find_element_by_xpath("/html/body/div/div/div/ul/li[6]/span").text
104+
105+
print (added_item)
106+
107+
#Assertion
108+
if "Yey, Let's add it to list" in added_item:
109+
driver.execute_script("lambda-status=passed")
110+
111+
else:
112+
driver.execute_script("lambda-status=failed")
113+
114+
115+
116+
117+
118+
if __name__ == "__main__":
119+
120+
unittest.main()
121+
36122
37-
4. Running Tests
38-
* To Start Test:
39-
- Navigate to Python-UnitTest-Selenium
40-
- Run following command
41-
* Execution
42-
```
43-
$ python lambdatest_test.py or nosetests test_sample.py
44-
```
45-
46-
##### Routing traffic through your local machine
47-
- Set tunnel value to `true` in test capabilities
123+
```
124+
#### To run file :
125+
126+
```
127+
python lambdatest_test.py or nosetests lambdatest_test.py
128+
```
129+
130+
131+
## Routing traffic through your local machine using Lambdatest
132+
- Set tunnel value to `True` in test capabilities
48133
> OS specific instructions to download and setup tunnel binary can be found at the following links.
49134
> - [Windows](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Windows)
50135
> - [Mac](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+MacOS)
51136
> - [Linux](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Linux)
52137
138+
139+
140+
Below we see a screenshot that depicts our UnitTest code is running over different browsers i.e Chrome, Firefox and Safari on the LambdaTest Selenium Grid Platform. The results of the test script execution along with the logs can be accessed from the LambdaTest Automation dashboard.
141+
142+
143+
![alttext](https://github.com/Apoorvlt/test/blob/master/unitcap.PNG)
144+
145+
146+
53147
### Important Note:
54-
Some Safari & IE browsers, doesn't support automatic resolution of the URL string "localhost". Therefore if you test on URLs like "http://localhost/" or "http://localhost:8080" etc, you would get an error in these browsers. A possible solution is to use "localhost.lambdatest.com" or replace the string "localhost" with machine IP address. For example if you wanted to test "http://localhost/dashboard" or, and your machine IP is 192.168.2.6 you can instead test on "http://192.168.2.6/dashboard" or "http://localhost.lambdatest.com/dashboard".
148+
---
149+
- Some Safari & IE browsers, doesn't support automatic resolution of the URL string "localhost". Therefore if you test on URLs like "http://localhost/" or "http://localhost:8080" etc, you would get an error in these browsers. A possible solution is to use "localhost.lambdatest.com" or replace the string "localhost" with machine IP address. For example if you wanted to test "http://localhost/dashboard" or, and your machine IP is 192.168.2.6 you can instead test on "http://192.168.2.6/dashboard" or "http://localhost.lambdatest.com/dashboard".
55150

56151
## About LambdaTest
57-
58152
[LambdaTest](https://www.lambdatest.com/) is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your [selenium automation testing](https://www.lambdatest.com/selenium-automation) to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.
59153

60154
### Resources
61155

62-
##### [SeleniumHQ Documentation](http://www.seleniumhq.org/docs/)
63-
##### [UnitTest Documentation](https://docs.python.org/2/library/unittest.html)
156+
##### [Selenium Documentation](http://www.seleniumhq.org/docs/)
157+
158+
##### [Python Documentation](https://docs.python.org/2.7/)
159+
160+
##### [Pytest Documentation](http://pytest.org/latest/contents.html)

‎base_test.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from selenium import webdriver
2+
import unittest
3+
import os
4+
from configparser import ConfigParser
5+
6+
caps={}
7+
8+
class BaseTest(unittest.TestCase):
9+
# ======================================================================
10+
# Setup and tear down functions
11+
# ======================================================================
12+
13+
@classmethod
14+
def setUpClass(self):
15+
16+
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
17+
18+
self.config = ConfigParser()
19+
20+
my_file = ROOT_DIR + '\config.cfg'
21+
22+
self.config.read(my_file)
23+
24+
self.init_capabilities(self)
25+
26+
self.base_url = "https://lambdatest.github.io/sample-todo-app/"
27+
28+
remote_url= "https://"+self.user_name+":"+self.app_key+"@hub.lambdatest.com/wd/hub"
29+
30+
caps['name'] = "LambdaTestSampleApp"
31+
32+
caps['build'] = "PythonUnitTestSample"
33+
34+
caps['browserName'] = "Chrome"
35+
36+
caps['version'] = "71.0"
37+
38+
caps['platform'] = "Windows 10"
39+
40+
caps['network'] = True
41+
42+
caps['visual']= True
43+
44+
caps['video']= True
45+
46+
caps['console']= True
47+
48+
49+
self.driver = webdriver.Remote(command_executor=remote_url,desired_capabilities=caps)
50+
51+
self.driver.implicitly_wait(30)
52+
53+
self.driver.maximize_window()
54+
55+
# open application url
56+
57+
self.driver.get(self.base_url)
58+
59+
@classmethod
60+
61+
def tearDownClass(self):
62+
63+
print ( "closed browser" )
64+
65+
self.driver.quit()
66+
67+
def init_capabilities(self):
68+
69+
70+
self.user_name = os.environ.get("LT_USERNAME")
71+
self.app_key = os.environ.get("LT_ACCESS_KEY")
72+
73+

‎lambdatest_test.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
1-
import os
2-
import unittest
3-
import sys
1+
import unittest, time, re
2+
import base_test
43
from selenium import webdriver
5-
6-
username = os.environ.get("LT_USERNAME")
7-
access_key = os.environ.get("LT_ACCESS_KEY")
8-
9-
class FirstSampleTest(unittest.TestCase):
10-
11-
# setUp runs before each test case
12-
def setUp(self):
13-
desired_caps = {
14-
"build": 'unittest sample build',
15-
"name": 'Py-unittest',
16-
"platform": 'Windows 10',
17-
"browserName": 'firefox',
18-
"version": '73'
19-
}
20-
self.driver = webdriver.Remote(
21-
command_executor="http://{}:{}@hub.lambdatest.com:80/wd/hub".format(username, access_key),
22-
desired_capabilities= desired_caps)
23-
24-
25-
# tearDown runs after each test case
26-
def tearDown(self):
27-
self.driver.quit()
28-
4+
5+
from selenium.webdriver.common.keys import Keys
6+
7+
class LambdaTest(base_test.BaseTest):
8+
299
def test_unit_user_should_able_to_add_item(self):
10+
3011
# try:
12+
3113
driver = self.driver
32-
14+
3315
# Url
34-
driver.get("https://lambdatest.github.io/sample-todo-app/")
35-
16+
17+
driver.get(self.base_url)
18+
3619
# Click on check box
20+
3721
check_box_one = driver.find_element_by_name("li1")
22+
3823
check_box_one.click()
39-
24+
4025
# Click on check box
26+
4127
check_box_two = driver.find_element_by_name("li2")
28+
4229
check_box_two.click()
43-
30+
4431
# Enter item in textfield
32+
4533
textfield = driver.find_element_by_id("sampletodotext")
34+
4635
textfield.send_keys("Yey, Let's add it to list")
47-
36+
4837
# Click on add button
38+
4939
add_button = driver.find_element_by_id("addbutton")
40+
5041
add_button.click()
51-
42+
5243
# Verified added item
53-
added_item = driver.find_element_by_xpath("//span[@class='done-false']").text
44+
45+
added_item = driver.find_element_by_xpath("/html/body/div/div/div/ul/li[6]/span").text
46+
5447
print (added_item)
5548

49+
#Assertion
50+
if "Yey, Let's add it to list" in added_item:
51+
driver.execute_script("lambda-status=passed")
52+
53+
else:
54+
driver.execute_script("lambda-status=failed")
55+
56+
57+
58+
59+
5660
if __name__ == "__main__":
61+
5762
unittest.main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /