Pesquisar este blog
segunda-feira, 21 de setembro de 2015
I will show how to create test automation with ddt and python, with code clean and adaptable in Real Project: Part 2
We will expand our project to other operating systems, Linux and windows for example, our project will be multi-platform, now let's go get the way of our directory and add to our links.
import platform,sys
# GET FULL PATH DIRETORY
f = sys.path[0]
# GET DIRECTORY OF PROJECT
project = f.split("sanity_test", 1)
PROJECT_PATH = project[0]
LOGIN_PAGE = "/login.php?note=expired"
BASE_URL = "http://localhost//testlink-1.9.13"
MAIN_MENU = "/lib/general/navBar.php?tproject_id=0&tplan_id=0&updateMainPage=1"
INDEX_MENU = "//index.php?caller=login"
LOGOUT_PAGE = "/logout.php"
LOGIN_PASS_WIN = PROJECT_PATH + "data\\login_password_ok.csv"
LOGIN_PASS_LIN = PROJECT_PATH + "data/login_password_ok.csv"
LOGIN_FAIL_WIN = PROJECT_PATH + "data\\loginOrPass_failed.csv"
LOGIN_FAIL_LIN = PROJECT_PATH + "data/loginOrPass_failed.csv"
TOP_TEXT_MAIN_PAGE = "TestLink 1.9.13 (Stormbringer)"
XPATH_TOP_TEXT_MAIN_PAGE = "/html/body/div[2]/span[3]"
DELAY_FAST = 3
DELAY_HIGH = 7
SCREEN_SAVE= PROJECT_PATH + "sanity_test\\screenshots\\"
# GET PATH OF ACCORDING WITH OPERATING SYSTEM
platform = platform.uname()[0]
if platform == "Windows":
PATH_TEST_OK = LOGIN_PASS_WIN
PATH_TEST_FAIL = LOGIN_FAIL_WIN
elif platform == "Linux":
PATH_TEST_OK = LOGIN_PASS_LIN
PATH_TEST_FAIL = LOGIN_FAIL_LIN
Now our main test modified, with screen slot now, need create subdirectoy screenshots in sanity_test.
"""
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, os
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(PATH_TEST_OK))
# will automatically unpack tuples and lists into multiple arguments, and dictionaries into multiple
# keyword arguments.
@unpack
def test_login_password_ok(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
self.driver.get(self.base_url+ INDEX_MENU)
time.sleep(DELAY_HIGH)
screenshot = self.driver.get_screenshot_as_file(SCREEN_SAVE + tl_login + "_" + tl_password + "" + user_type +'.png')
print "Screenshot saved to: %s" % screenshot
return True
else:
# Inform if not found the field expected.
time.sleep(DELAY_HIGH)
# 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(DELAY_HIGH)
# Logout this web application
self.driver.get(self.base_url+ LOGOUT_PAGE)
def tearDown(self):
# will end the whole session.
self.driver.quit()
Now our simple code without need another classes, adaptable to multiple platforms!
Let's go now complicate our project, our client asked that the tests they are run on multiple browsers, Firefox, Chrome, Internet Explorer by default.
http://www.vidadetestador.com/2015/09/i-will-show-how-to-create-test_22.html
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário