Skip to content

Cucumber and Ruby keywords and functions

Nikolina Djekic edited this page Sep 27, 2019 · 1 revision

Cucumber and Ruby

Table of Contents

  1. Cucumber folder structure
  2. .feature file structure
  3. Cucumber keywords
  4. Cucumber functionalities
  5. Ruby and Appium methods
  6. Functionalities in Ruby
  7. Appium commands
  8. Debugging
  9. Terminal commands
  10. UI Automator Viewer

Cucumber folder structure

  folder_name
    features - required folder
      area.feature
      area2.feature
      step_definition - required folder
        area_step.rb
        area2_step.rb
      support
        env.rb - file in which we set parameters for testing
        hooks.rb - file in which we create hooks

.feature file structure

  Feature: Description
    Background: Steps to be executed in every Scenario in this feature file 
  Scenario: Description
  # Steps with keywords --> Steps are reusable accross feature files
  Given step
  When step
  And step
  Then step
  But step

Cucumber keywords

Cucumber keywords are Given, When, Then, And, But. They are userd to make logical sense between steps. We can use any prefix we wan and the step will work because cucumber does not add prefix as part of step.

Cucumber functionalities

  1. Set parameter in feature file

     Given Description "parameter"
  2. Make Scenario Outline when you need to execute multiple steps with many values

     Scenario Outline:
       #Steps
     Given
     When I type "<value1_name>"
     Then I should see "<value2_name>"
     And See also "<value3_name>"
     Examples:
     |value1_name|value2_name|value3_name|
     |val1       |val2       |val3       |
     |val1       |val2       |val3       |
  3. @Tags Are used for grouping tests in Cucumber

     @name
     Scenario or Step or Feature
  4. Hooks are used to execute anything we need to do before or after Test Scenario. Two hooks cucumber has are Before and After

     Before do
       # anything that needs to be done before Test Scenario
       # launch Appium for example
     end
     After do |scenario|
       # anything that needs to be done after Test Scenario
       # kill Appium for example
       # Send screenshots of device if test fails for example
     end
     After Configuration do
     #what to do after every scenario
     end
  5. Profiles are placed in same level as features folder. Profiles file is : cucumber.yml.

     <!-- Creates profile name and mames report in report.html file and reports to console  -->
      name: -t @tag --format html --out report.html --format pretty

Ruby and Appium methods

  1. puts - command that prints message

     puts("message")
  2. |arg| - local variable that lives between do and end

     |var|
     puts(var)
  3. find_element(id: "id") - find element by resource id

  4. find_element(accessibility_id: "content desc") - used when we target element with content description

  5. find_elements(id: "id") - find multiple elements by resource id

  6. find_elements(accessibility_id: "content desc") - find multiple elements by resource id

  7. find_elements(criteria)[index] - finds element that is on INDEX index inside array

  8. find_element(id: "id").find_element(xpath: "//class[@text='']") - find element inside container by resourcing its class and then text inside that specific element

  9. text("text") - used when we need to find element by text

  10. .click - for click event in Appium

  11. .send_keys(value) - when we want to write inside element

  12. .text - gets text from element

  13. fail("Message") - method that executes when test failed. Inside brackets goes optional message

  14. .split("criteia") - split any array by any criteria

  15. .each - iterate through array of data

  16. .enabled? - returns boolean true if state is enabled and false if its state is disabled

  17. .failed? - return boolean true if test failed and false if test passed

  18. .tap - click on screen

  19. .swipe - scroll on screen

  20. .times{} - repeat some code number of times

  21. .exists{what} - used to check if there is that element on page

  22. .get_source - return all values of that page in XML

  23. Time.now(strftime("%d-%m-%Y_%H.%M.%S")) - Time function formatting

  24. File - Class for File manipulation

  25. .to_i - transforms string to integer(for indexes --> .to_i-1)

  26. biniding pry - calls pry and pauses test execution and gives control to us

Functionalities in Ruby

  1. Taking parameter as part of step

     Then('condition {string} or "([^"]*)"') do |arg|
       puts("message" + arg)
     end
  2. Taking parameter as part of step ex 1st, 2nd, 3rd

     Then('condition (\d+)(?:st|rd|th)?') do |arg|
       puts("message" + arg)
     end
  3. Set key values that can be accepted

     Given(Description (option1|option2|option3)) do |state|
     end
  4. if elsif statement

       if state == "option1"
         do this
       elsif state == "option2"
         do this
       end
  5. fail function in multiple lines

     if Condition != expected
       fail("Message")
     end
  6. fail function one line

     fail("Message if failed") if Condition != expected
  7. Pass variable inside string with #{var}

     Then('condition {string} or "([^"]*)"') do |arg|
       puts("message #{string}")
     end
  8. Arrays [] are data type in ruby that has indexes. Indexing starts at 0.

  9. each loop for going through array of data

     array.each do |var|
     end
  10. For repeating Ruby code for number X of times we use .times

     X.times{code}
  11. Loop through data until some condition isn't met

     until condition do
       # what needs to be done every time
     end
  12. Functions are made so one piece of code could be executed multiple times. We make functions inside env.rb file.

     def functionName(parameters)
       # what needs to be executed multiple times
     end

Appium commands

1.When we need to click on element by its coordinates x and y Z of times we use TouchAction, .tap and .perform event

 Appium::TouchAction.new.tap(x:X.xx, y:Y.yy, count: Z).perform
  1. When we need to scroll on page with Appium we use TouchAction, .swipe and .perform for some duration of time

     Appium::TouchAction.new.swipe(start_x:X.xx, start_y:Y.yy, end_x:X.xx, end_y:Y.yy, duration:X ms).perform

Debugging

Pry is tool that can be called if we want to pause our test execution and take over control over test at any time we want.

Terminal commands

  1. cucumber - executes every test scenario in every .feature file
  2. cucumber -t @name - executes scenario or feature file with @name tag
  3. cucumber --dry-run - executes scenarios without Ruby code, checks for step definitions
  4. cucumber -t @tag1, @tag2 - executes every scenario that either has @tag1 or @tag2
  5. cucumber -t @tag1 -t @tag2 - executes every scenario that has both @tag1 and @tag2
  6. cucucmber -p profile-name -executes test scenario with this profile name
  7. adb device - which device is connected to computer
  8. uiautomatorviewer - starts automator viewer
  9. irb - starts ruby console

UI Automator Viewer

  1. Takes screenshot of device. On the right side are elements and their properties. If we don't have any resource_id we can target element by content description. Or in edge cases by text.

Clone this wiki locally