2. Before Automating: BDD
● Encouraging conversation between all involved
● Writing down examples to make things clearer
● Reduce ambiguity
“Automating the scenarios resulting from the conversations is an optional next step.”
Julien Biezemans
Creator of Cucumber.js
3. Gherkin: a natural language style
“Gherkin is designed to be easy to learn by non-
programmers, yet structured enough to allow
concise description of examples to illustrate
business rules in most real-world domains”.
4. Why Test Automation?
● Saves you time, money and people
● Consistency of tests
● Continuous Integration
● Avoid boredom
5. Cucumber
Cucumber can be used to implement automated tests based
on scenarios described in your Gherkin feature files.
In the example given in step definitions:
When she eats 3 cucumbers
Cucumber extracts the text 3 from the step, converts it to an
int and passes it as an argument to the method.
6. Cucumber vs Behave vs Godog
Cucumber: Java, JS, Ruby.
Behave: Cucumber Python style.
Godog: Cucumber for golang.
10. Page Objects design pattern
“Despite the term "page" object, these objects shouldn't usually be built for each page, but rather for
the significant elements on a page. So a page showing multiple albums would have an album list
page object containing several album page objects. There would probably also be a header page
object and a footer page object.”
Martin Fowler
Software Developer
11. Page Objects: Benefits
● Create reusable code that can be shared
across multiple test cases
● Reduce the amount of duplicated code
● If the user interface changes, the fix needs
changes in only one place
12. Page Objects: Example
from lib.pages.basepage import BasePage
from selenium.webdriver.common.by import By
class LogoutPage(BasePage):
def __init__(self, context):
BasePage.__init__(self, context.browser, base_url='http://twitter.com/logout')
locator_dictionary = {
"submit" : (By.CSS_SELECTOR, 'button[type="submit"]'),
}
16. Features
Feature: Tweet
Allow valid users
Post 280 characters limit per tweet
To have a better day
@rainy
Scenario: Tweet like a crazy teenager > 280 chars
Given the valid user is logged in on the homepage
When user post invalid tweet
Then the tweet button should be disabled
@sunny @sanity
Scenario: Tweet properly <= 280 chars
Given the valid user is logged in on the homepage
When user post valid tweet
Then the tweet button should be enabled
And the user should be able to tweet
17. environment.py
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from lib.pages.basepage import BasePage
def before_all(context):
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
context.browser = BasePage(driver)
def after_scenario(context, scenario):
context.browser.screenshot(str(scenario))
def after_all(context):
print("===== That's all folks =====")
context.browser.close()
18. steps/page.py
from features.lib.pages.loginpage import LoginPage
from behave import given, when, then
from config import USER
@when(u'the "{user_type}" user logs in')
def login(context, user_type):
username_field = context.browser.find(
LoginPage.locator_dictionary['email'])
password_field = context.browser.find(
LoginPage.locator_dictionary['password'])
username_field.send_keys(USER[user_type]['email'])
password_field.send_keys(USER[user_type]['pass'])
submit_button = context.browser.find(
LoginPage.locator_dictionary['submit'])
submit_button.click()
Scenario: Valid login
Given "valid" user navigates to page "landing"
When the "valid" user logs in
Then the user should be redirected to homepage
feature/steps/login.py feature/steps/login.py
23. Tips
● Be aware of Chromedriver/chrome version
○ Chrome headless requires chromedriver 2.3+
● Use Selenium explicit/implicit wait instead of python time.sleep function
○ Better, faster and stronger
● Use find_by_id (or similar) instead of find_by_xpath
○ IE provides no native XPath-over-HTML solution
24. Best Practices
● "Tag" parts of your feature file
● Gherkin common mistakes
○ Using absolute values instead of configurable values
○ Describing every action instead of a functionality
○ Writing scripts in first person instead of third person
● Good relationship with Frontenders > You will need IDs
● The scenarios should run independently, without any dependencies on other scenarios
Testes Funcionais: Resumidamente verificar se a aplicação está apta a realizar as funções na qual foi desenvolvida para fazer.
Ilustrar com exemplos sobre testes repetidos, quebrar features anteriores, CI.
Usar a linguagem que se sentir mais confortável, que o time dominar, ou que tenha uma facil rampa de aprendizado
encapsular em cada classe os atributos e métodos, como campos e ações de cada pagina.