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
terça-feira, 15 de setembro de 2015
I will show how to create test automation with ddt and python, with code clean and adaptable in Real Project:
By using the data-driven testing approach, we can use a single test to verify different
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:
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"
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
"""
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
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
quinta-feira, 13 de agosto de 2015
Como aumentar o import da suite de teste no TestLink
Quando tentamos importar uma suite de testes para outro Projeto, muita das vezes a suite é maior que o permitido por padrão. Aconteceu comigo a pouco tempo, devemos fazer as alterações abaixo, para suportar 10 Mbytes e não termos mais problemas.
No config.inc.php, vamos alterar as seguintes linhas:
-> Tamanho Máximo que podemos Importar
/** Maximum uploadfile size to importing stuff in TL */
$tlCfg->import_file_max_size_bytes = ‘409600’;
-> Numero Máximo de Linhas que podem ser importadas
// the maximum allowed file size for each repository entry, default 1MB.
$tlCfg->import_max_row = ‘10000’; // in chars
-> O Máximo tamanho permitido para cada repositório.
// the maximum allowed file size for each repository entry
$tlCfg->repository_max_filesize = 1; //MB
Para os valores 10485760, 100000, 10 :
$tlCfg->import_file_max_size_bytes = ‘10485760’;
$tlCfg->import_max_row = ‘100000’; // in chars
$tlCfg->repository_max_filesize = 10; //MB
No php.ini, vamos alterar as seguintes linhas, para os valores abaixo:
max_execution_time = 1800
upload_max_filesize = 50M
post_max_size = 50M
memory_limit = 128M
No linux o caminho:
sudo gedit /etc/php5/apache2/php.ini
Por precaução vamos para o serviço do apache e iniciar novamente:
Em Linux:
/etc/init.d/apache2 stop
/etc/init.d/apache2 restart
** Use o sudo caso der problema de permissão.
terça-feira, 11 de agosto de 2015
Python vs Java na Automação de Testes
A muito tempo quero fazer uma comparação entre Python e Java para automação de testes. Vejo que a maioria dos treinamento são em Java para automação de testes. No entanto as outras linguagens são esquecidas como Python principalmente, é pouco falado e disseminado entre as empresas, sendo que Python é muito mais rápido a linha de aprendizado que Java. No entanto o aprendizado em Java leva muito mais tempo e treinamento.
Vamos falar do ponto mais importante que é produtividade. Fiz um pequeno teste usando a própria ferramenta do Selenium IDE pra medir a produtividade, medindo pela quantidade de linhas de código gerada pela própria ferramenta, exportando para as devidas linguagem.
Passo 01:
Foram gravado os seguintes passos:
1. Abrir o google;
2. Pesquisar pelo assunto "Amazon";
3. Selecionar o Link e em seguida pesquisar pelo assunto "Python";
4. Selecionar o livro "Use a Cabeça! Python";
5. Clicar no botão "adicione ao carrinho".
Passo 02:
Agora vamos exportar o script gerado, seguindo as opções "Arquivo - Exportar Teste como..."
Vamos exportar com as opções abaixo:
Python 2 / unittest / WebDriver
Java / Unit 4 / WebDriver
Passo 03:
Agora vamos abrir com o notepad++ e avaliar a quantidade de linhas de código:
Python - 55 Linhas - 2133 Caracteres
Java - 77 Linhas - 2233 Caracteres
Analisando o código gerado a ferramenta criou o método main do python pra fazer a chamada da classe e no java ela não fez isso, fazendo o mesmo a quantidade de linhas no java ia ser bem maior.
if __name__ == "__main__":
unittest.main()
Quantidade de imports, vemos que no Python temos 7 linhas de import e no java temos 8 linhas.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
Otimizado um pouco o nosso código ficaria com 3 linhas e funcionaria de boa, não sendo necessário as outras:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
import unittest, time, re
No Python não precisamos colocar ; no final das variáveis, pelo que contei no código em java tivemos 38 ponto e vírgula, isso é desvantagem, sem falar dos parentheses. Uma característica marcada no Python é a identação do código é obrigatório que o código esteja alinhado de forma correta, isso facilita muito a manutenção do código, se torna fácil de entender a lógica e os loops, no caso do java a ferramenta fez a organização do código, mas não é assim que os desenvolvedores fazem, dificilmente você vai encontrar um código bem arrumado.
No código abaixo não sei porque a ferramenta criou driver = self.driver, podemos muito bem usar direto self.driver em todas as linhas, não precisando criar driver = self.driver, removemos com isso mais uma linha ficando com 50 linhas / 1970 Caracteres do nosso código, contra 77 linhas / 2233 Caracteres do java, aqui vemos 263 Caracteres que em python podemos ser mais produtivo que em Java.
É claro que é meio difícil medir a produtividade, simplesmente porque cada pessoa tem a sua velocidade e existe a IDEs como Eclipse e Pycharm que tem o code completion, Java é uma linguagem muito poderosa, mas na automação de testes não utilizamos nem 30% dela, no entanto Java é muito mais verboso e tem um tempo maior de aprendizado que Python. No meu caso, eu codifico scripts de testes em Python, posso dizer que é muito prático e rápido de codificar, no Python você pode usar o extend em mais de uma classe, no Java você não consegue isso, mas existe outras formas pra contornar em java, mas não de forma prática como no Python.
Código em Python:
# -*- coding: utf-8 -*-from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
import unittest, time, re
class SeliniumPython(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.com.br/" self.verificationErrors = []
self.accept_next_alert = True def test_selinium_python(self):
self.driver.get(self.base_url + "/search?q=amazon&ie=utf-8&oe=utf-8&gws_rd=cr&ei=2JXKVZSXDMKXgwSSnLWYAw")
self.driver.find_element_by_link_text("Amazon.com.br: compre livros, eBooks, e-readers Kindle e ...").click()
self.driver.find_element_by_id("twotabsearchtextbox").click()
self.driver.find_element_by_id("twotabsearchtextbox").clear()
self.driver.find_element_by_id("twotabsearchtextbox").send_keys("python")
self.driver.find_element_by_css_selector("input.nav-input").click()
self.driver.find_element_by_xpath("//li[@id='result_3']/div/div/div/div[2]/div/a/h2").click()
self.driver.find_element_by_id("bb_atc_button").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Código em Java:
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class SeleniumJava {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.google.com.br/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSeleniumJava() throws Exception {
driver.get(baseUrl + "/search?q=amazon&ie=utf-8&oe=utf-8&gws_rd=cr&ei=2JXKVZSXDMKXgwSSnLWYAw");
driver.findElement(By.linkText("Amazon.com.br: compre livros, eBooks, e-readers Kindle e ...")).click();
driver.findElement(By.id("twotabsearchtextbox")).click();
driver.findElement(By.id("twotabsearchtextbox")).clear();
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("python");
driver.findElement(By.cssSelector("input.nav-input")).click();
driver.findElement(By.xpath("//li[@id='result_3']/div/div/div/div[2]/div/a/h2")).click();
driver.findElement(By.id("bb_atc_button")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
catch (NoAlertPresentException e)
return false;
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
sábado, 8 de agosto de 2015
Criando uma classe Email para Report de problemas nos Testes.
É muito útil você receber um Relatório mostrando o momento exato que aconteceu o problema/bug nos testes automatizados, para isso vamos fazer uma classe email, enviando para a mesma os parâmetros que desejamos receber, num eventual teste de estresse é essencial saber a duração, o tempo que durou a aplicação em um determinado teste de estresse/carga, para constar em nosso relatório. Como somos fera, vamos criar em Python o código de envio de email, criando uma conexão SMTP (Simple Mail Transfer Protocol) client session, o que quer dizer isso, bom vamos criar uma conta do google e ele vai retransmitir mensagens enviadas para o nosso destinatário.
O legal de enviar um email de erro, você pode receber no celular é você não precisar checar o tempo todo os seus testes e nem parar todos os testes automatizados por causa de um erro.
Para mais detalhes leia a página abaixo:
SMTP Google
# -*- coding: utf-8 -*-
"""
Autor: Reinaldo M. R. J
Data: 08-08-2015
Desc: Classe para enviar o report dos testes.
"""
from utils.common import *
# Import the email modules we'll need
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email.mime.image import MIMEImage
# Import smtplib for the actual sending function
import smtplib
import os
class test_Report:
def __init__(self):
self.common = FunctionsVariables()
def email_file(self,path_full, file_name):
try:
# Create the container (outer) email message.
msg = MIMEMultipart()
# In SUBJECT
SUBJECT = "Report of Sanity Test"
msg['Subject'] = SUBJECT
msg['From'] = "xxxx@gmail.com"
# Aqui vai conter nosso destinatario.
msg['To'] = "reinaldo.rossetti@outlook.com"
msg['Date'] = formatdate(localtime = True)
# No MIMEText vamos digitar o que vai conter no corpo do email.
msg.attach(MIMEText('Sanity teste finalizado com Sucesso.,'
' Verifique o resultado em anexo.'))
part = MIMEBase('application', "octet-stream")
part.set_payload(open(path_full, "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename='\
+ file_name)
msg.attach(part)
# Configuração do smtp do gmail e porta.
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(msg['From'], 'digite a senha aqui')
try:
# Python 3.2.1
server.send_message(msg)
except AttributeError:
# Python 2.7.2
server.sendmail(msg['From'], [msg['To']], msg.as_string())
print "Enviado com Sucesso!"
finally:
server.quit()
except (RuntimeError, TypeError, NameError):
print "Unexpected error:", sys.exc_info()[0]
raise
def SendEmailFalha (self, testCaseNumber, testCaseName, \
duration, result, path_full, img_name):
try:
# Create the container (outer) email message.
msg = MIMEMultipart()
destination = ("reinaldo.rossetti@outlook.com", "reiload@gmail.com")
text = ("Number of Test: " + str(testCaseNumber) + " Result: "\
+ str(result) + "\nTest Device: " + testCaseName \
+ "\nDuration DAYS:HOURS:MIN: " + str(duration))
msg['Subject'] = "Status dos Testes - " + str(testCaseName) +\
" Result: " + str(result)
msg['From'] = "xxxx@gmail.com"
msg['To'] = ','.join(destination)
texto = MIMEText(text, 'plain')
file = path_full
fp = open(file, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-Disposition', 'attachment',
filename=img_name)
msg.attach(texto)
msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login(msg['From'], 'digite aqui sua senha')
try:
# Python 3.2.1
s.send_message(msg)
except AttributeError:
# Python 2.7.2
s.sendmail(msg['From'], [msg['To']], msg.as_string())
print "Enviado com Sucesso!"
finally:
s.quit()
except (RuntimeError, TypeError, NameError):
print "Unexpected error:", sys.exc_info()[0]
pass
if __name__ == '__main__':
test = test_Report()
test.SendEmailFalha("Test-333", "Stress TestX", "00:12:00",
"False","D:\Minhas imagens\conferirCONCD006.png", "conferirCONCD006.png")
No nosso código foi incluído um try e um except com pass, isso quer disser se tivemos algum erro, ele vai mostrar o error e vai passar na função não parando o código em eventual erro, outra coisa interessante do nosso código é a versão do Python 2.7.x e 3.x as funções são diferentes, dessa forma feita não vai dar problema com as diferentes versões. Outra boa prática séria usar remover variáveis que podem mudar no nosso código, como os email do Destinatário, podemos colocar todas essas variáveis em FunctionsVariables, e apagamos a linha em nosso código, naquele momento do aperreio temos um única classe com todas as variáveis que podem mudar.
Exemplo in functions_variables.py
class functionsVariables(object):
global destination
destination = ("reinaldo.rossetti@outlook.com", "reiload@gmail.com")
O Nosso Resultado Final é esse abaixo:
domingo, 2 de agosto de 2015
sábado, 1 de agosto de 2015
Criando um Bom Plano de Teste
Na Engenharia de Testes na fase de Planejamento devemos criar um Plano de Testes conciso e eficaz, adotando a metodologia ágil. O que isso quer dizer?, deve conter no documento aquilo que realmente tem valor para as pessoas interessadas no projeto. Não adianta escrever um documento de 50 páginas aonde ninguém vai ler ou dar valor.
O plano de testes funciona como um contrato entre os times envolvidos, então deve ser claro e conciso e ao mesmo tempo venha trazer eficácia para o time de testes. Para isso você precisa entender os Artefatos/Documentos gerados no desenvolvimento do sistema, segue os principais abaixo:
• Artefatos de Entrada:
– Plano do projeto ;
– Requisitos funcionais e não-funcionais ;
– Documentação referente aos projetos/sistemas relacionados.
Agora vamos falar de Boas Práticas:
- Sempre usar ferramentas para Gestão dos Testes e Bug Tracker (TestLink, JIRA, TestRail, Mantis, Bugzilla, etc);
- Envolver os profissionais de testes desde a elicitação inicial dos requisitos do software (workshops);
- Definir uma estratégia para priorização dos testes com foco no negócio do software (priorizar requisitos mais importantes);
- Descrever bem o que a área de Testes vai fazer e não vai fazer;
- Descrever bem os Critérios de Suspensão da versão.
- É algo fundamental colocar as Premissas e Acordos entre as áreas envolvidas, um exemplo seria:
"Fica acordado que o analista de Sistemas/Negócio que conhece bem do sistema e ou arquiteto do projeto deverão prestar consultoria em pelo menos 1h diária, para a equipe de testes em casos de dúvidas geradas na revisão dos documentos, e na criação do Plano de Teste como também na criação do Ambiente para a realização dos Testes."
- Outra coisa muito importante é não esquecer de criar um Plano de Testes que contém os requisitos não funcionais do projeto, como Testes de Segurança e Performance, problema aqui põem todo projeto a perder.
Existe outra forma de fazer um Plano de Teste, Planos de Testes por Iteração, são Planos de Testes enxutos pra cada Iteração do Projeto.
Segue um exemplo do que seria um Plano de Teste em 18 páginas, coloquei referências bibliográficas para melhor aprofundamento no assunto, não sendo necessário colocar no plano de testes:
Plano de Testes - LINK
https://en.wikipedia.org/wiki/Test_plan
Assinar:
Postagens (Atom)


