Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Getting Started with Test Automation: Introduction to Cucumber with Lapis Lazuli

275 views

Published on

Introduction to web testing with Cucumber using spriteCloud's Lapis Lazuli Cucumber helper.

This presentation will take you through the steps needed to set up a test automation project using Cucumber - a software tool that runs automated tests in the BBD style - in combination with Lapis Lazuli, a gem that provides Cucumber helper functions and scaffolding for easier web test automation suite development.

To do this you will need to have installed Ruby with some drivers and libraries. You can find detailed notes on how to do this here: http://www.testautomation.info/Getting_Started

To successfully follow this tutorial, prior knowledge of test automation isn’t needed, but knowledge of scripting - especially Ruby - and HTML skills are recommended.

Lapus Lazuli's GitHub depository: https://github.com/spriteCloud/lapis-lazuli

For more information about everything test automation, whether you need someone to help you set up TA in your organisation or want to find out more about our Test Automation as a Service options, email: info@spritecloud.com.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Getting Started with Test Automation: Introduction to Cucumber with Lapis Lazuli

  1. 1. Copyright © 2015 spriteCloud B.V. All rights reserved. Introduction to Cucumber with Lapis Lazuli Getting Started with Test Automation
  2. 2. Copyright © 2015 spriteCloud B.V. All rights reserved. Getting started: This presentation will take you through the steps needed to set up a test automation project using Cucumber - a software tool that runs automated tests in the BBD style - in combination with Lapis Lazuli, a gem that provides Cucumber helper functions and scaffolding for easier web test automation suite development. To do this you will need to have installed Ruby with some drivers and libraries. You can find detailed notes on how to do this here: http://www.testautomation.info/Getting_Started
  3. 3. Copyright © 2015 spriteCloud B.V. All rights reserved. What you need: To successfully follow this tutorial, prior knowledge of test automation isn’t needed, but knowledge of scripting - especially Ruby - and HTML skills are recommended.
  4. 4. Copyright © 2015 spriteCloud B.V. All rights reserved. You will find out about: - Gherkin - Cucumber - Browser testing - Creating a TA project - Best practices
  5. 5. Copyright © 2015 spriteCloud B.V. All rights reserved. Example website To set up this project, we will test on a small web application as an example. Try the following steps out at: http://www.testautomation.info/training-page/ Note: The web app doesn’t store information, closing the browser window will reset the page and created user accounts.
  6. 6. Copyright © 2015 spriteCloud B.V. All rights reserved. HTML introduction Firstly, this is HTML Structure you will need to refer to: <parent> <element> <child></child> <child></child> </element> <element/> </parent> HTML Element: <a id="homepage" href="http://www.spritecloud.com"> spriteCloud </a> Element (node) Attribute (node) name Attribute (node) value Text (node)
  7. 7. Copyright © 2015 spriteCloud B.V. All rights reserved. Code examples A very short explanation of code you need during this training: A variable is a label you can give to a piece of data. Data can be simple (numbers, text, functions) or complex. Complex data or objects contain variables. Functions return data based on certain input data. # This is a comment variable = "string" variable2 = :symbol variable3 = 1000 * 3 + 200 Object.variable puts("Hello World!") => "Hello World!" $ program "on commandline" # Red is used as a highlight
  8. 8. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing Install Lapis Lazuli with: $ gem install lapis_lazuli Run the Interactive Ruby Shell $ irb # Load the library require("lapis_lazuli") # Activate LL in this IRB session include(LapisLazuli) # Goto the website browser.goto("http://www.testautomatio n.info/training-page/") # Print the page title browser.title() => "Calliope Training"
  9. 9. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing Finding elements on page using LL uses the .find / .find_all function. It allows you to do complex searches, but today we will focus on the basics. # Find the title # using the unique ID attribute browser.find("title").text() # or the longer syntax browser.find({:id => "title"}) # Number of links on a page # using the element browser.find_all(:a).length()
  10. 10. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing Sometimes elements don’t have an ID and you will have to use other attributes. # Number text fields on the page # using attributes browser.find_all({:like => { :element => "input", :attribute => "type", :include => "text"}} ).length() # or a short notation browser.find_all({ :like => ["input","type","text"] })
  11. 11. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing Website automation requires more interaction than loading pages and finding elements. Start by clicking the login button # Find the login button # and save it in a variable login = browser.find({ :id => "button-login" }) # Correct button? login.flash() # Click the button login.click()
  12. 12. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing To do a successful login we need to enter our credentials: # Username text field username = browser.find({ :id => "login-username" }) # Enter the username username.send_keys("test") # Repeat with password field # Do the login login.click() Username test Password test
  13. 13. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing What happens if you try to find an element that doesn’t exist on the page? # Search for not existing element browser.find({:id => "HIDDEN"}) => RuntimeError: Error in find - Cannot find elements with selectors: {:pick=>:first, :mode=>:match_one, :selectors=>[{:element=>"HIDDEN"}]} (http://www.spritecloud.com/)
  14. 14. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing The find functions allow you to set your own custom error message to helps non-technical users understand the issue. # Search for not existing element browser.find({ :id => “HIDDEN”, :message => “Could not find hidden element” }) => RuntimeError: Could not find hidden element - Cannot find elements with selectors: { … } (http://www.spritecloud.com/)
  15. 15. Copyright © 2015 spriteCloud B.V. All rights reserved. Browser testing Now try it yourself: - Click the first todo item - Add a todo Or try out automating another website, like searching on Google.com
  16. 16. Copyright © 2015 spriteCloud B.V. All rights reserved. Technology framework - BDD Language to describe tests - Test specification and execution - Tools to improve Watir for cucumber testing - Tools to improve WebDriver - Browser automation - Programming Language - Cucumber Software-as-a-Service by spriteCloud Lapis Lazuli Watir Selenium WebDriver Ruby Cucumber Gherkin Calliope
  17. 17. Copyright © 2015 spriteCloud B.V. All rights reserved. Gherkin Gherkin is the language that Cucumber understands. It is a Business Readable, Domain Specific Language that lets you describe software's behaviour without detailing how that behaviour is implemented. Gherkin serves two purposes — documentation and automated tests. Feature: Some terse yet descriptive text of what is desired Scenario: Some situation Given some precondition And some other precondition When some action by the actor And some other action And yet another action Then some outcome is achieved And something else we can check
  18. 18. Copyright © 2015 spriteCloud B.V. All rights reserved. Gherkin For the feature description, a three- line user story is ideal: ● In order to <achieve goal> ● As a <role> ● I want to <activity details> Feature: Todo Clicking In order to complete a todo As a test automation novice I want to click a todo Scenario: Regular numbers Given I am logged in When I click the fist todo Then 2 todos remain
  19. 19. Copyright © 2015 spriteCloud B.V. All rights reserved. Cucumber Cucumber is a software tool that computer programmers use for testing other software. It runs automated acceptance tests written in a behavior-driven development (BDD) style. Given /I am logged in/ do browser.goto "http://www.testautomation.info/training-page/" end When /I click the first todo/ do browser.find("todo-item-0").click end Then /(d+) todos remain/ do |number| browser.find("todo-remaining") # … some extra code end
  20. 20. Copyright © 2015 spriteCloud B.V. All rights reserved. Cucumber Given /I am logged in/ do browser.goto "http://www.testautomation.info/training-page/" end When /I click the first todo/ do browser.find("todo-item-0").click end Then /(d+) todos remain/ do |number| browser.find("todo-remaining") end Feature: Todo Clicking In order to complete a todo As a test automation novice I want to click a todo Scenario: Regular numbers Given I am logged in When I click the fist todo Then 2 todos remain
  21. 21. Copyright © 2015 spriteCloud B.V. All rights reserved. Starting a new project To create a project type: $ lapis_lazuli create MyTaTraining The project already includes an example scenario, so lets run it: $ cd MyTaTraining/ $ cucumber MyTaTraining/ config/ config.yml cucumber.yml features/ example.feature step_definitions/ interaction_steps.rb validation_steps.rb support/ env.rb
  22. 22. Copyright © 2015 spriteCloud B.V. All rights reserved. Cucumber result Let’s look at the cucumber output: Scenario: example01 - Google Search # example.feature:8 Given I navigate to Google in english # interaction_steps.rb:6 And I search for "spriteCloud" # interaction_steps.rb:16 Then I see "www.spriteCloud.com" on the page # validation_steps.rb:6 1 scenario (1 passed) 3 steps (3 passed) 0m9.976s
  23. 23. Copyright © 2015 spriteCloud B.V. All rights reserved. Cucumber result Let’s look at the cucumber output: Scenario: example01 - Google Search #example.feature:8 Given I navigate to Google in english # interaction_steps.rb:6 And I search for "spriteCloud" # interaction_steps.rb:16 Then I see "www.spriteCloud.com" on the page # validation_steps.rb:6 1 scenario (1 passed) 3 steps (3 passed) 0m9.976s MyTaTraining/ config/ config.yml cucumber.yml features/ example.feature step_definitions/ interaction_steps.rb validation_steps.rb support/ env.rb
  24. 24. Copyright © 2015 spriteCloud B.V. All rights reserved. Cucumber options By default Cucumber will run all feature files it can find, but it has multiple options for customized runs Advanced: To view all options $ cucumber --help # Using feature file $ cucumber features/test.feature # With line number $ cucumber features/test.feature:10 # Run a single tag $ cucumber --tags @example # Run two tags (logical OR) $ cucumber -t @example,@dev # Run scenario with both tags (AND) $ cucumber -t @example -t @dev # Excluding a tag $ cucumber -t ~@dev # Check which scenarios will run $ cucumber --dry-run
  25. 25. Copyright © 2015 spriteCloud B.V. All rights reserved. TA Best practices 1. Don’t add ‘code’ to Gherkin 2. Validate if a step has been completed 3. Try to use XPath or the like and not RegExp 4. Don’t use hard timeouts 5. Work with the development team 6. Use shortest possible selector 7. Scenarios are independent 8. Keep your code clean and simple

×