sets of test cases or test data by driving the test with input and expected values from
an external data source instead of using the hardcoded values every time a test is run.
The ddt library provides the ability to parameterize the test cases written using the
unittest library in Python. We can provide a set of data using ddt to a test case for
data-driven tests.
The ddt library provides a set of class and method decorators that we can use to
create data-driven tests.
Unmesh Gundecha - A practical guide on automated web testing with Selenium using Python.
I will show how to create test automation with ddt and python, with code clean and adaptable in Real Project:
1. Step - Install ddt pack:
We can download and install ddt using the following command line in shell or cmd windows:
pip install ddt
Home Page: https://github.com/txels/ddt
2. Step - Reading values from CSV:
We will use the previous test case and move the data that we supplied to
the @data decorator into a separate CSV file called login_password_ok.csv instead of
keeping it in the script. This data will be stored in a tabular format as shown
in the following screenshot:
3. Install the TestLink in localhost:
Follow the video below:
https://www.youtube.com/watch?v=BaOxsJOpk30
4. Step - Create the users in TestLink :
Follow the video below:
5. Step - Creating the code:
5.1- Now let's go create two files in python:
- login_test.py
- variables_url_test.py - Will contain data that can change at any time.
5.1.1 Create file variables_url_test.py and insert data below:
BASE_URL = "http://localhost//testlink-1.9.13"
MAIN_MENU = "/lib/general/navBar.php?tproject_id=0&tplan_id=0&updateMainPage=1"
LOGOUT_PAGE = "/logout.php"
LOGIN_PASS= "C:\\xxx\\xxx\\xxx\\xxx\\data\\login_password_ok.csv"
TOP_TEXT_MAIN_PAGE = "TestLink 1.9.13 (Stormbringer)"
XPATH_TOP_TEXT_MAIN_PAGE = "/html/body/div[2]/span[3]"
DELAY_FAST = 3
5.1.1 Create file login_test.py and insert data below:
"""
Author: Reinaldo Mateus R J, Test version: 0.1
Fist Step - Imports modules, in Python code in one module gains access to the code in another module by
the process of importing.
Second Step - create function get_data in csv file.
Third Step - create class and function specific for test.
"""
import csv, unittest, time
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from variables_test import *
def get_data(file_name):
# create an empty list to store rows
rows = []
# open the CSV file
print file_name
data_file = open(file_name, "rb")
# create a CSV Reader from CSV file
reader = csv.reader(data_file)
# skip the headers
next(reader, None)
# add rows from reader to list
for row in reader:
rows.append(row)
return rows
# DDT consists of a class decorator @ddt (for your TestCase subclass)
@ddt
class LoginTestClass(unittest.TestCase):
def setUp(self):
# create a new Firefox session
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
# url base of website
self.base_url = BASE_URL
# Read the users in rows, and Passing variables tl_login,tl_password,user_type to function test_run.
@data(*get_data(LOGIN_PASS))
# will automatically unpack tuples and lists into multiple arguments, and dictionaries into multiple
# keyword arguments.
@unpack
def test_run(self,tl_login,tl_password,user_type):
wait = WebDriverWait(self.driver, 90)
# Try three times if fail.
for i in range(3):
try:
self.driver.get(self.base_url+ LOGIN_PAGE)
elem = wait.until(lambda driver: driver.find_element_by_name("tl_login"))
# Send user name in tl_login field
elem.send_keys(tl_login)
# Next we are sending keys, this is similar to entering keys using your keyboard.
elem.send_keys(Keys.RETURN)
elem = self.driver.find_element_by_name("tl_password")
# Send password in tl_password field
elem.send_keys(tl_password)
# This is similar to entering keys using your keyboard.
elem.send_keys(Keys.RETURN)
time.sleep(DELAY_FAST)
# timeout five seconds
time.sleep(DELAY_FAST)
self.driver.get(self.base_url+ MAIN_MENU)
print "Test: ", tl_login, tl_password, user_type
time.sleep(DELAY_FAST)
confirm = wait.until(lambda driver: driver.find_element_by_xpath\
("/html/body/div[2]/span[contains(text(),'"+user_type+"')]" ))
print confirm.text
elem_test = str(confirm.text)
time.sleep(DELAY_FAST)
# split text in two words in the string.
elem_test = elem_test.split(" ", 1)
time.sleep(DELAY_FAST)
print "Tag value: " + str(elem_test)
# compare second word with user_type
if elem_test[1] == "["+user_type+"]":
if (self.driver.find_element_by_xpath(XPATH_TOP_TEXT_MAIN_PAGE)):
# Test - compare text expected with XPATH_TOP_TEXT_MAIN_PAGE in browser.
self.assertTrue((self.driver.find_element_by_xpath(XPATH_TOP_TEXT_MAIN_PAGE).text\
== TOP_TEXT_MAIN_PAGE))
print "Test User " +user_type+ " Passed with success!", tl_login, tl_password, user_type
break
else:
# Inform if not found the field expected.
time.sleep(10)
# Test - compare text expected with XPATH_TOP_TEXT_MAIN_PAGE in browser.
self.assertTrue((self.driver.find_element_by_xpath(XPATH_TOP_TEXT_MAIN_PAGE).text\
== TOP_TEXT_MAIN_PAGE))
print "Element Xpath not found: ", tl_login, tl_password, user_type
except:
pass
print "Failed!", tl_login, tl_password, user_type
print "Failed attempts: ", i
time.sleep(7)
# Logout this web application
self.driver.get(self.base_url+ LOGOUT_PAGE)
def tearDown(self):
# close the browser window
self.driver.quit()
From basic mode we finished our tests, but not the testing activity is carried out to find the defects in the code & improve the quality of software application. Testing of application can be carried out in two different ways, Positive testing and Negative testing. Keep in mind that both positive and negative testing is equally important for effective testing which help to improve quality of software.
Negative Test scenarios:
Login and Password wrong, should not accept.
Login ok and Password wrong, should not accept.
Login wrong and Password ok, should not accept.
Password textbox should not exceeds more than 20 characters
Password textbox should not accept special characters.
Now we will expand our project to other operating systems in part two:
http://www.vidadetestador.com/2015/09/i-will-show-how-to-create-test_21.html
Nenhum comentário:
Postar um comentário