Skip to content

Revert "Python-UnitTest-Selenium" #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 46 additions & 143 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,159 +2,62 @@
![LambdaTest Logo](https://www.lambdatest.com/static/images/logo.svg)
---

![logo](https://github.com/Apoorvlt/test/blob/master/logo.PNG)

## Prerequisites for Python Behave tutorial

### 1. Python Installation

* [Download Python](https://www.python.org/downloads/) and click on Add to path and install.

* 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.

### 2. LambdaTest Credentials
* 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).
* In the terminal export your LambdaTest Credentials as environmental variables:

* For Mac/Linux
```
$ export LT_USERNAME=<your LambdaTest username>
$ export LT_ACCESS_KEY=<your LambdaTest access key>
```

* For Windows
```
set LT_USERNAME=<your LambdaTest username>
set LT_ACCESS_KEY=<your LambdaTest access key>
```

### 3. Setup

* Clone [Python-UnitTest-Selenium](https://github.com/LambdaTest/Python-UnitTest-Selenium.git) from GitHub.
* Navigate to the cloned directory
* Install project dependencies by running command:

```
pip install -r requirements.txt
```

Requirements.txt file includes the following:

```
ConfigParser
selenium>2.5
pytest
nose
pytest-xdist
```
## Test Scenario

### Single Test

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.

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>.

```
import unittest, time, re
import base_test
from selenium import webdriver

from selenium.webdriver.common.keys import Keys

class LambdaTest(base_test.BaseTest):

def test_unit_user_should_able_to_add_item(self):

# try:

driver = self.driver

# Url

driver.get(self.base_url)

# Click on check box

check_box_one = driver.find_element_by_name("li1")

check_box_one.click()

# Click on check box

check_box_two = driver.find_element_by_name("li2")

check_box_two.click()

# Enter item in textfield

textfield = driver.find_element_by_id("sampletodotext")

textfield.send_keys("Yey, Let's add it to list")

# Click on add button

add_button = driver.find_element_by_id("addbutton")

add_button.click()

# Verified added item

added_item = driver.find_element_by_xpath("/html/body/div/div/div/ul/li[6]/span").text

print (added_item)

#Assertion
if "Yey, Let's add it to list" in added_item:
driver.execute_script("lambda-status=passed")

else:
driver.execute_script("lambda-status=failed")





if __name__ == "__main__":

unittest.main()

### Environment Setup

1. Global Dependencies

-Windows
* Download the latest python installer for Windows: http://sourceforge.net/projects/pywin32/files/pywin32/
* Run the installer and follow the setup wizard to install Python

-Linux/Mac
* Run python --version to see which python version is currently installed, make sure it is 2.5.X or above.
* OS X, Ubuntu and most other Linux distro's come with Python pre-installed.

2. Lambdatest Credentials
* Set LambdaTest username and access key in environment variables. It can be obtained from [LambdaTest dashboard](https://automation.lambdatest.com/)
example:
- For linux/mac
```
export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR ACCESS KEY"

```
#### To run file :

```
python lambdatest_test.py or nosetests lambdatest_test.py
```


## Routing traffic through your local machine using Lambdatest
- Set tunnel value to `True` in test capabilities
```
- For Windows
```
set LT_USERNAME="YOUR_USERNAME"
set LT_ACCESS_KEY="YOUR ACCESS KEY"

```
3. Setup
* Clone [Python-UnitTest-Selenium](https://github.com/LambdaTest/Python-UnitTest-Selenium.git) from GitHub.
* Navigate to the cloned directory
* Install project dependencies by running command `pip install -r requirements.txt`

4. Running Tests
* To Start Test:
- Navigate to Python-UnitTest-Selenium
- Run following command
* Execution
```
$ python lambdatest_test.py or nosetests test_sample.py
```

##### Routing traffic through your local machine
- Set tunnel value to `true` in test capabilities
> OS specific instructions to download and setup tunnel binary can be found at the following links.
> - [Windows](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Windows)
> - [Mac](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+MacOS)
> - [Linux](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Linux)



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.


![alttext](https://github.com/Apoorvlt/test/blob/master/unitcap.PNG)



### Important Note:
---
- 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".
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".

## About LambdaTest

[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.

### Resources

##### [Selenium Documentation](http://www.seleniumhq.org/docs/)

##### [Python Documentation](https://docs.python.org/2.7/)

##### [Pytest Documentation](http://pytest.org/latest/contents.html)
##### [SeleniumHQ Documentation](http://www.seleniumhq.org/docs/)
##### [UnitTest Documentation](https://docs.python.org/2/library/unittest.html)
73 changes: 0 additions & 73 deletions base_test.py

This file was deleted.

75 changes: 35 additions & 40 deletions lambdatest_test.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
import unittest, time, re
import base_test
import os
import unittest
import sys
from selenium import webdriver

from selenium.webdriver.common.keys import Keys

class LambdaTest(base_test.BaseTest):


username = os.environ.get("LT_USERNAME")
access_key = os.environ.get("LT_ACCESS_KEY")

class FirstSampleTest(unittest.TestCase):

# setUp runs before each test case
def setUp(self):
desired_caps = {
"build": 'unittest sample build',
"name": 'Py-unittest',
"platform": 'Windows 10',
"browserName": 'firefox',
"version": '73'
}
self.driver = webdriver.Remote(
command_executor="http://{}:{}@hub.lambdatest.com:80/wd/hub".format(username, access_key),
desired_capabilities= desired_caps)


# tearDown runs after each test case
def tearDown(self):
self.driver.quit()

def test_unit_user_should_able_to_add_item(self):

# try:

driver = self.driver

# Url

driver.get(self.base_url)

driver.get("https://lambdatest.github.io/sample-todo-app/")

# Click on check box

check_box_one = driver.find_element_by_name("li1")

check_box_one.click()

# Click on check box

check_box_two = driver.find_element_by_name("li2")

check_box_two.click()

# Enter item in textfield

textfield = driver.find_element_by_id("sampletodotext")

textfield.send_keys("Yey, Let's add it to list")

# Click on add button

add_button = driver.find_element_by_id("addbutton")

add_button.click()

# Verified added item

added_item = driver.find_element_by_xpath("/html/body/div/div/div/ul/li[6]/span").text

added_item = driver.find_element_by_xpath("//span[@class='done-false']").text
print (added_item)

#Assertion
if "Yey, Let's add it to list" in added_item:
driver.execute_script("lambda-status=passed")

else:
driver.execute_script("lambda-status=failed")





if __name__ == "__main__":

unittest.main()