Module: Calabash::Cucumber::TestsHelpers

Includes:
FailureHelpers
Included in:
Operations, WaitHelpers
Defined in:
lib/calabash-cucumber/tests_helpers.rb

Overview

A collection of methods to help you write tests.

Instance Method Summary collapse

Methods included from FailureHelpers

#fail, #screenshot, #screenshot_and_raise, #screenshot_embed

Instance Method Details

#check_element_does_not_exist(query) ⇒ nil

raises a Runtime error (and generates a screenshot) if at least one element matches query ‘query`.

Parameters:

  • query (String)

    the query to execute

Returns:

  • (nil)

    Returns nil if there is no match.

Raises:

  • (RuntimeError)

    if an element matches ‘query`.



55
56
57
58
59
# File 'lib/calabash-cucumber/tests_helpers.rb', line 55

def check_element_does_not_exist(query)
  if element_exists(query)
    screenshot_and_raise "Expected no elements to match query: #{query}"
  end
end

#check_element_exists(query) ⇒ nil

raises a Runtime error (and generates a screenshot) unless at least one element matches query ‘query`.

Parameters:

  • query (String)

    the query to execute

Returns:

  • (nil)

    Returns nil if there is a match.

Raises:

  • (RuntimeError)

    if no element matches ‘query`.



45
46
47
48
49
# File 'lib/calabash-cucumber/tests_helpers.rb', line 45

def check_element_exists(query)
  if not element_exists(query)
    screenshot_and_raise "No element found for query: #{query}"
  end
end

#check_view_with_mark_exists(expected_mark) ⇒ nil

raises a Runtime error (and generates a screenshot) unless at least one element matches mark ‘expected_mark`.

Parameters:

  • expected_mark (String)

    the mark to check for.

Returns:

  • (nil)

    Returns nil if there is a match.

Raises:

  • (RuntimeError)

    if no element matches ‘view marked:’#expected_mark‘`.



65
66
67
# File 'lib/calabash-cucumber/tests_helpers.rb', line 65

def check_view_with_mark_exists(expected_mark)
  check_element_exists("view marked:'#{expected_mark}'")
end

#classes(uiquery, *args) ⇒ Array<String>

Returns the classes of all views matching ‘uiquery`

Parameters:

  • uiquery (String)

    the query to execute

  • args (Array)

    optional args to pass to ‘query`.

Returns:

  • (Array<String>)

    class names of views matching ‘uiquery`



15
16
17
# File 'lib/calabash-cucumber/tests_helpers.rb', line 15

def classes(uiquery,*args)
  query_map(uiquery,:class,*args)
end

#each_cell(opts = {:query => "tableView", :post_scroll => 0.3, :animate => true}, &block) ⇒ Object

Calls given block with each row and section (‘yield(row, sec)`). Alternates between scrolling to each cell and yielding.

Parameters:

  • opts (Hash) (defaults to: {:query => "tableView", :post_scroll => 0.3, :animate => true})

    specifies details of the scroll

Options Hash (opts):

  • :query (String) — default: 'tableView'

    query specifying which table view to use

  • :post_scroll (Numeric) — default: 0.3

    wait to be done after each scroll

  • :animated (Boolean) — default: true

    animate or not



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/calabash-cucumber/tests_helpers.rb', line 77

def each_cell(opts={:query => "tableView", :post_scroll => 0.3, :animate => true}, &block)
  uiquery = opts[:query] || "tableView"
  skip = opts[:skip_if]
  check_element_exists(uiquery)
  secs = query(uiquery,:numberOfSections).first
  secs.times do |sec|
    rows = query(uiquery,{:numberOfRowsInSection => sec}).first
    rows.times do |row|
      next if skip and skip.call(row,sec)
      scroll_opts = {:section => sec, :row => row}.merge(opts)
      scroll_to_cell(scroll_opts)
      sleep(opts[:post_scroll]) if opts[:post_scroll] and opts[:post_scroll] > 0
      yield(row, sec)
    end
  end
end

#element_does_not_exist(uiquery) ⇒ Boolean

Returns true if no element matches query ‘uiquery`.

Parameters:

  • uiquery (String)

    the query to execute

Returns:

  • (Boolean)

    ‘true` if no element matches query `uiquery`. `false` otherwise.



22
23
24
# File 'lib/calabash-cucumber/tests_helpers.rb', line 22

def element_does_not_exist(uiquery)
  query(uiquery).empty?
end

#element_exists(uiquery) ⇒ Boolean

Returns true if at least one element matches query ‘uiquery`.

Parameters:

  • uiquery (String)

    the query to execute

Returns:

  • (Boolean)

    ‘true` if at least one element matches query `uiquery`. `false` otherwise.



29
30
31
# File 'lib/calabash-cucumber/tests_helpers.rb', line 29

def element_exists(uiquery)
  not element_does_not_exist(uiquery)
end

#view_with_mark_exists(expected_mark) ⇒ Boolean

Returns true if at least one element matches query ‘“* marked:’#expected_mark‘”`

Parameters:

  • expected_mark (String)

    the mark to search for

Returns:

  • (Boolean)

    ‘true` if at least one element matches query `“* marked:’#expected_mark‘”. `false` otherwise.



37
38
39
# File 'lib/calabash-cucumber/tests_helpers.rb', line 37

def view_with_mark_exists(expected_mark)
  element_exists("view marked:'#{expected_mark}'")
end