Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building your own Monkey - The Era of Automation

Building your own Monkey - The Era of Automation

Selenium + Python + Lettuce/Behave + ChromeDriver

Avatar for Letícia Rostirola

Letícia Rostirola

October 31, 2017
Tweet

More Decks by Letícia Rostirola

Other Decks in Programming

Transcript

  1. Why Automation? • Saves you time, money and people •

    Consistency of tests • Continuous Integration • Avoid boredom
  2. What to automate? • Repetitive tasks (monkey job) • Frequently

    used functionality • Tests that take a lot of effort and time when manual testing • Tests that run on several different hardware or software platforms and configurations. • ...
  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”. https://cucumber.io/docs/reference
  4. features/twitter/tweet.feature Feature: Tweet Scenario: Invalid login Given I am logged

    in as "invalid" Then I should see the message "The email and password you entered did not match our records. Please double-check and try again." Scenario: Tweet like a crazy teenager > 140 chars Given I am logged in as "me" When I write the tweet "Restart não presta mais porque eu cheguei aqui 8 horas da manhã e eles não vieram falar com a gente. Não, eu não vou perdoar. Eu vou xingar no twitter hoje, muito. Sério." Then I should not be able to submit the tweet Scenario: Tweet like a pro Given I am logged in as "me" When I write the tweet "This is my first tweet using automation \o/ #selenium #python" And I press "js-tweet-btn" button Then I should see my tweet
  5. features/src/config.py (optional) # Hostname of the application being tested TWITTER_HOST

    = 'https://twitter.com/' PATH = '<your_path>/little-monkey' ## Chromedriver path CHROMEDRIVER = '/home/lero/.virtualenvs/little-monkey-lettuce/chromedriver' USERS = { 'me': { 'username': '[email protected]', 'password': 'pass' } } BROWSER = 'chrome’
  6. features/src/terrain.py from config import BROWSER, PATH, CHROMEDRIVER from lettuce import

    before, after, world from selenium import webdriver @before.all def setup_browser(): if BROWSER == 'chrome': chrome_options = webdriver.ChromeOptions() world.browser = webdriver.Chrome(executable_path=CHROMEDRIVER, chrome_options=chrome_options) world.browser.set_window_size(1280,800) ... @after.all def tear_down_feature(self): world.browser.quit()
  7. features/src/steps.py from config import TWITTER_HOST, USERS, PATH from lettuce import

    step, world from lettuce_webdriver.util import assert_true, assert_false, AssertContextManager from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait @step('I write the tweet "(.*?)"$') def I_write_twitter_post(step, tweet): with AssertContextManager(step): WebDriverWait(world.browser, 10).until( EC.presence_of_element_located((By.ID, "tweet-box-home-timeline"))) element = world.browser.find_element_by_id("tweet-box-home-timeline") element.clear() element.send_keys(tweet) @step('I should see the message "([^"]+)"$') def should_see(step, text): assert_true(step, contains_content(world.browser, text.encode('utf-8'))) ...
  8. 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