Module: Calabash::Cucumber::Core
- Includes:
- EnvironmentHelpers, FailureHelpers, KeyboardHelpers, QueryHelpers, StatusBarHelpers, UIA
- Included in:
- DatePicker, Operations, WaitHelpers
- Defined in:
- lib/calabash-cucumber/core.rb
Overview
A collection of methods that provide the core calabash behaviors.
Instance Method Summary collapse
-
#await_page(clz, *args) ⇒ Object
Instantiates a page using ‘page` and calls the page’s ‘await` method.
-
#backdoor(selector, *arguments) ⇒ Object
Calls a method on the app’s AppDelegate object.
-
#calabash_exit(opts = {}) ⇒ Object
Attempts to shut the app down gracefully by simulating the transition to closed steps.
-
#calabash_info(msg) ⇒ void
Prints a green info message.
-
#calabash_warn(msg) ⇒ void
Prints a blue warning message.
-
#clear_text(uiquery) ⇒ Array<String>
Sets the text value of the views matched by
uiquery
to''
(the empty string). -
#client_version ⇒ String
Returns the version of the loaded Calabash library.
-
#console_attach(uia_strategy = nil) ⇒ Calabash::Cucumber::Launcher?
Attach the current calabash launcher to a console.
-
#deprecated(version, msg, type) ⇒ void
Prints a deprecated message that includes the line number.
-
#device_agent ⇒ Calabash::Cucumber::DeviceAgent
Returns an object that provides an interface to the DeviceAgent public query and gesture API.
-
#dismiss_ipad_keyboard ⇒ Object
Dismisses a iPad keyboard by touching the ‘Hide keyboard’ button and waits for the keyboard to disappear.
-
#double_tap(uiquery, options = {}) ⇒ Array<Hash>
Performs the “double tap” gesture on the (first) view that matches query ‘uiquery`.
-
#flash(uiquery, *args) ⇒ Array
Causes all views matched by the ‘uiquery` to briefly change colors making them visually identifiable.
-
#flick(uiquery, delta, options = {}) ⇒ Array<Hash>
Performs the “flick” gesture on the first view that matches ‘uiquery`.
-
#html(q) ⇒ Array<String>
returns the ‘html’ property of all objects matching the query ‘q`.
-
#identifier(uiquery) ⇒ Array<String>
Returns all accessibilityIdentifiers of objects matching ‘uiquery`.
-
#keyboard_enter_char(char, options = {}) ⇒ Object
deprecated
Deprecated.
0.21.0 Use #keyboard_enter_text
-
#keyboard_enter_text(text) ⇒ Object
Uses the keyboard to enter text.
-
#label(uiquery) ⇒ Array<String>
Returns all accessibilityLabels of objects matching ‘uiquery`.
-
#location_for_place(place) ⇒ Geocoder::Result::Google
Returns a geo-location search result (via Google).
-
#page(clz, *args) ⇒ Object
Helper method to easily create page object instances from a cucumber execution context.
-
#pan(from_query, to_query, options = {}) ⇒ Array<Hash>
Performs the pan gesture between two coordinates.
-
#pan_coordinates(from_point, to_point, options = {}) ⇒ Object
Performs the pan gesture between two coordinates.
-
#pinch(in_out, options = {}) ⇒ Array<Hash>
Performs a “pinch” gesture.
-
#query(uiquery, *args) ⇒ Object
The core method for querying into the current visible view of the app under test.
-
#rotate(direction) ⇒ Symbol
Rotates the device in the direction indicated by ‘direction`.
-
#rotate_home_button_to(position) ⇒ Symbol
Rotates the home button to a position relative to the status bar.
-
#scroll(uiquery, direction) ⇒ Object
Scroll a scroll view in a direction.
-
#scroll_to_cell(options = {:query => "UITableView index:0", :row => 0, :section => 0, :scroll_position => :top, :animate => true}) ⇒ Object
Scroll a table view to a section and row.
-
#scroll_to_collection_view_item(item_index, section_index, options = {}) ⇒ Object
Scrolls to an item in a section of a UICollectionView.
-
#scroll_to_collection_view_item_with_mark(mark, options = {}) ⇒ Object
Scrolls to mark in a UICollectionView.
-
#scroll_to_mark(mark, options = {}) ⇒ Object
Scrolls to a mark in a UIScrollView.
-
#scroll_to_row(uiquery, number) ⇒ Object
Scroll a table view to a row.
-
#scroll_to_row_with_mark(mark, options = {}) ⇒ Object
Scrolls to a mark in a UITableView.
-
#send_app_to_background(seconds) ⇒ Object
Sends the app to the background.
-
#server_log_level ⇒ String
Get the Calabash server log level.
-
#server_version ⇒ String
Returns the version of the running calabash server.
-
#set_location(options) ⇒ Object
Simulates gps location of the device/simulator.
-
#set_server_log_level(level) ⇒ Object
Set the Calabash server log level.
-
#set_text(uiquery, txt) ⇒ Array<String>
Sets the text value of the views matched by
uiquery
totxt
. -
#set_user_pref(key, val) ⇒ Object
Sets user preference (NSUserDefaults) value of key ‘key` to `val`.
-
#shake(seconds) ⇒ Object
Cause the device to shake.
-
#slider_set_value(uiquery, value, options = {}) ⇒ Array<String>
Set the sliders indicated by ‘uiquery` to `value`.
-
#start_test_server_in_background(args = {}) ⇒ Calabash::Cucumber::Launcher
Starts the app and Calabash test server in the console.
-
#swipe(direction, options = {}) ⇒ Array<Hash>
Performs a “swipe” gesture.
-
#tap_keyboard_action_key ⇒ Object
Touches the keyboard action key.
-
#tap_keyboard_delete_key ⇒ Object
Touches the keyboard delete key.
-
#tap_mark(label, *args) ⇒ Array<Hash>
taps a view with mark ‘label`.
-
#tap_point(x, y) ⇒ Boolean
Performs the ‘tap` gesture on an absolute coordinate.
-
#touch(uiquery, options = {}) ⇒ Array<Hash>
Performs the ‘tap` gesture on the (first) view that matches query `uiquery`.
-
#touch_hold(uiquery, options = {}) ⇒ Array<Hash>
Performs the “long press” or “touch and hold” gesture on the (first) view that matches query ‘uiquery`.
-
#two_finger_tap(uiquery, options = {}) ⇒ Array<Hash>
Performs the “two-finger tap” gesture on the (first) view that matches query ‘uiquery`.
-
#user_pref(key) ⇒ Object
Gets the user preference (NSUserDefaults) value for a key.
Methods included from KeyboardHelpers
#docked_keyboard_visible?, #keyboard_visible?, #lookup_key_name, #split_keyboard_visible?, #undocked_keyboard_visible?, #wait_for_keyboard, #wait_for_no_keyboard
Methods included from StatusBarHelpers
#device_orientation, #landscape?, #portrait?, #status_bar_details, #status_bar_orientation
Methods included from UIA
#uia, #uia_call, #uia_call_windows, #uia_keyboard_visible?, #uia_names, #uia_orientation, #uia_query, #uia_query_windows, #uia_rotate, #uia_rotate_home_button_to, #uia_set_responder_value, #uia_wait_for_keyboard
Methods included from FailureHelpers
#fail, #screenshot, #screenshot_and_raise, #screenshot_embed
Methods included from QueryHelpers
#escape_backslashes, #escape_newlines, #escape_quotes, #escape_string
Methods included from EnvironmentHelpers
#default_device, #device_family_iphone?, #ios10?, #ios11?, #ios5?, #ios6?, #ios7?, #ios8?, #ios9?, #ios_gte_11?, #ios_version, #ipad?, #ipad_pro?, #iphone?, #iphone_35in?, #iphone_4in?, #iphone_6?, #iphone_6_plus?, #iphone_app_emulated_on_ipad?, #ipod?, #screen_dimensions, #simulator?, #uia_available?, #uia_not_available?, #xamarin_test_cloud?
Instance Method Details
#await_page(clz, *args) ⇒ Object
Instantiates a page using ‘page` and calls the page’s ‘await` method.
1619 1620 1621 |
# File 'lib/calabash-cucumber/core.rb', line 1619 def await_page(clz,*args) clz.new(self,*args).await end |
#backdoor(selector, *arguments) ⇒ Object
For methods that take arguments, don’t forget to include the trailing “:”
Calls a method on the app’s AppDelegate object.
Use this to call an arbitrary Objective-C or Swift method in your app’s UIApplicationDelegate.
Commonly used to “go around” the UI speed purposes or reset the app to a good known state.
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 |
# File 'lib/calabash-cucumber/core.rb', line 1464 def backdoor(selector, *arguments) parameters = { :selector => selector, :arguments => arguments } begin body = http({:method => :post, :path => "backdoor"}, parameters) result = response_body_to_hash(body) rescue RuntimeError => e raise RuntimeError, e end if result["outcome"] != "SUCCESS" raise RuntimeError, %Q{backdoor call failed: selector => '#{selector}' arguments => '#{arguments}' reason => '#{result["reason"]}' #{result["details"]} } end result["results"] end |
#calabash_exit(opts = {}) ⇒ Object
Shutdown the CalabashServer and close connections.
Attempts to shut the app down gracefully by simulating the transition to closed steps. The server will attempt to ensure that the following UIApplicationDelegate methods methods are called (in order).
“‘
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationWillTerminate:(UIApplication *)application
“‘
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 |
# File 'lib/calabash-cucumber/core.rb', line 1512 def calabash_exit(opts={}) default_opts = {:post_resign_active_delay => 0.4, :post_will_terminate_delay => 0.4, :exit_code => 0} merged_opts = default_opts.merge(opts) # Exiting the app shuts down the HTTP connection and generates ECONNREFUSED, # or HTTPClient::KeepAliveDisconnected # which needs to be suppressed. begin http({ :method => :post, :path => 'exit', :retryable_errors => Calabash::Cucumber::HTTPHelpers::RETRYABLE_ERRORS - [Errno::ECONNREFUSED, HTTPClient::KeepAliveDisconnected] }, { :post_resign_active_delay => merged_opts[:post_resign_active_delay], :post_will_terminate_delay => merged_opts[:post_will_terminate_delay], :exit_code => merged_opts[:exit_code] } ) rescue Errno::ECONNREFUSED, HTTPClient::KeepAliveDisconnected, SocketError [] end if launcher.automator if launcher.automator.name == :device_agent delay = merged_opts[:post_resign_active_delay] + merged_opts[:post_will_terminate_delay] + 0.4 sleep(delay) launcher.automator.send(:session_delete) end end true end |
#calabash_info(msg) ⇒ void
This method returns an undefined value.
Prints a green info message.
59 60 61 62 |
# File 'lib/calabash-cucumber/core.rb', line 59 def calabash_info(msg) require "run_loop/logging" RunLoop.log_info2(msg) end |
#calabash_warn(msg) ⇒ void
This method returns an undefined value.
Prints a blue warning message.
51 52 53 54 |
# File 'lib/calabash-cucumber/core.rb', line 51 def calabash_warn(msg) require "run_loop/logging" RunLoop.log_warn(msg) end |
#clear_text(uiquery) ⇒ Array<String>
Sets the text value of the views matched by uiquery
to ''
(the empty string)
Using this sparingly and with caution. We recommend using queries and touches to replicate what the _user would do_.
@raise If the uiquery
finds no matching queries or finds a view that does not respond to the objc selector ‘setText’
1724 1725 1726 1727 1728 1729 |
# File 'lib/calabash-cucumber/core.rb', line 1724 def clear_text(uiquery) views_modified = Map.map(uiquery, :setText, '') msg = "query '#{uiquery}' returned no matching views that respond to 'setText'" Map.assert_map_results(views_modified, msg) views_modified end |
#client_version ⇒ String
Returns the version of the loaded Calabash library.
180 181 182 |
# File 'lib/calabash-cucumber/core.rb', line 180 def client_version Calabash::Cucumber::VERSION end |
#console_attach(uia_strategy = nil) ⇒ Calabash::Cucumber::Launcher?
Attach the current calabash launcher to a console.
1807 1808 1809 1810 1811 1812 |
# File 'lib/calabash-cucumber/core.rb', line 1807 def console_attach(uia_strategy = nil) if Calabash::Cucumber::Environment.xtc? raise "This method is not available on the Xamarin Test Cloud" end launcher.attach({:uia_strategy => uia_strategy}) end |
#deprecated(version, msg, type) ⇒ void
This method returns an undefined value.
Prints a deprecated message that includes the line number.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/calabash-cucumber/core.rb', line 71 def deprecated(version, msg, type) allowed = [:pending, :warn] unless allowed.include?(type) raise ArgumentError, "Expected type '#{type}' to be one of #{allowed.join(", ")}" end stack = Kernel.caller(0, 6)[1..-1].join("\n") msg = "deprecated '#{version}' - #{msg}\n#{stack}" if type.eql?(:pending) pending(msg) else calabash_warn(msg) end end |
#device_agent ⇒ Calabash::Cucumber::DeviceAgent
Returns an object that provides an interface to the DeviceAgent public query and gesture API.
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 |
# File 'lib/calabash-cucumber/core.rb', line 1831 def device_agent launcher = Calabash::Cucumber::Launcher.launcher_if_used if !launcher raise RuntimeError, %Q[ There is no launcher. If you are in the Calabash console, you can try to attach to an already running Calabash test using: > console_attach If you are running from Cucumber or rspec, call Launcher#relaunch before calling this method. ] end if !launcher.automator raise RuntimeError, %Q[ The launcher is not attached to an automator. If you are in the Calabash console, you can try to attach to an already running Calabash test using: > console_attach If you are running from Cucumber or rspec, call Launcher#relaunch before calling this method. ] end if launcher.automator.name != :device_agent raise RuntimeError, %Q[ The launcher automator is not DeviceAgent: #{launcher.automator} #device_agent is only available for Xcode 8. In your tests, use this pattern to branch on the availability of DeviceAgent. if uia_available? # Make a UIA call else # Make a DeviceAgent call end ] end automator = launcher.automator if !automator.running? raise RuntimeError, %Q[The DeviceAgent is not running.] else require "calabash-cucumber/device_agent" Calabash::Cucumber::DeviceAgent.new(automator.client, self) end end |
#dismiss_ipad_keyboard ⇒ Object
The dismiss keyboard key does not exist on the iPhone or iPod
Dismisses a iPad keyboard by touching the ‘Hide keyboard’ button and waits for the keyboard to disappear.
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 |
# File 'lib/calabash-cucumber/core.rb', line 824 def dismiss_ipad_keyboard # TODO Maybe relax this restriction; turn it into a nop on iPhones? # TODO Support iPhone 6 Plus form factor dismiss keyboard key. if device_family_iphone? screenshot_and_raise %Q[ There is no Hide Keyboard key on an iPhone. Use `ipad?` to branch in your test. ] end expect_keyboard_visible! launcher.automator.dismiss_ipad_keyboard wait_for_no_keyboard end |
#double_tap(uiquery, options = {}) ⇒ Array<Hash>
This assumes the view is visible and not animating.
Performs the “double tap” gesture on the (first) view that matches query ‘uiquery`.
If the view is not visible it will fail with an error. If the view is animating it will silently fail.
By default, taps the center of the view.
319 320 321 |
# File 'lib/calabash-cucumber/core.rb', line 319 def double_tap(uiquery, ={}) (:double_tap, uiquery, ) end |
#flash(uiquery, *args) ⇒ Array
Causes all views matched by the ‘uiquery` to briefly change colors making them visually identifiable.
165 166 167 168 169 |
# File 'lib/calabash-cucumber/core.rb', line 165 def flash(uiquery, *args) # todo deprecate the *args argument in the flash method # todo :flash operation should return views as JSON objects Map.map(uiquery, :flash, *args).compact end |
#flick(uiquery, delta, options = {}) ⇒ Array<Hash>
Performs the “flick” gesture on the first view that matches ‘uiquery`.
If the view is not visible it will fail with an error.
If the view is animating it will silently fail.
By default, the gesture starts at the center of the view and “flicks” according to ‘delta`.
A flick is a swipe with velocity.
463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/calabash-cucumber/core.rb', line 463 def flick(uiquery, delta, ={}) if uiquery.nil? raise ArgumentError, "Query argument cannot be nil" end = { :delta => delta }.merge() (:flick, uiquery, ) end |
#html(q) ⇒ Array<String>
returns the ‘html’ property of all objects matching the query ‘q`
1659 1660 1661 |
# File 'lib/calabash-cucumber/core.rb', line 1659 def html(q) query(q).map { |e| e['html'] } end |
#identifier(uiquery) ⇒ Array<String>
Returns all accessibilityIdentifiers of objects matching ‘uiquery`.
1638 1639 1640 |
# File 'lib/calabash-cucumber/core.rb', line 1638 def identifier(uiquery) query(uiquery, :accessibilityIdentifier) end |
#keyboard_enter_char(char, options = {}) ⇒ Object
0.21.0 Use #keyboard_enter_text
There are several special ‘characters’, some of which do not appear on all keyboards; e.g. ‘Delete`, `Return`.
Use keyboard to enter a character.
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
# File 'lib/calabash-cucumber/core.rb', line 679 def keyboard_enter_char(char, ={}) expect_keyboard_visible! default_opts = {:wait_after_char => 0.05} = default_opts.merge() special_char = launcher.automator.char_for_keyboard_action(char) if special_char launcher.automator.enter_char_with_keyboard(special_char) elsif char.length == 1 launcher.automator.enter_char_with_keyboard(char) else raise ArgumentError, %Q[ Expected '#{char}' to be a single character or a special string like: * Return * Delete To type strings with more than one character, use keyboard_enter_text. ] end duration = [:wait_after_char] if duration > 0 Kernel.sleep(duration) end [] end |
#keyboard_enter_text(text) ⇒ Object
Uses the keyboard to enter text.
742 743 744 745 746 747 |
# File 'lib/calabash-cucumber/core.rb', line 742 def keyboard_enter_text(text) expect_keyboard_visible! existing_text = text_from_first_responder escaped = existing_text.gsub("\n","\\n") launcher.automator.enter_text_with_keyboard(text, escaped) end |
#label(uiquery) ⇒ Array<String>
Returns all accessibilityLabels of objects matching ‘uiquery`.
1631 1632 1633 |
# File 'lib/calabash-cucumber/core.rb', line 1631 def label(uiquery) query(uiquery, :accessibilityLabel) end |
#location_for_place(place) ⇒ Geocoder::Result::Google
Returns a geo-location search result (via Google). Requires internet.
1371 1372 1373 1374 1375 |
# File 'lib/calabash-cucumber/core.rb', line 1371 def location_for_place(place) search_results = locations_for_place(place) raise "Got no results for #{place}" if search_results.empty? search_results.first end |
#page(clz, *args) ⇒ Object
Helper method to easily create page object instances from a cucumber execution context.
The advantage of using ‘page` to instantiate a page object class is that it will automatically store a reference to the current Cucumber world which is needed in the page object methods to call Cucumber-specific methods like puts or embed.
1601 1602 1603 |
# File 'lib/calabash-cucumber/core.rb', line 1601 def page(clz,*args) clz.new(self,*args) end |
#pan(from_query, to_query, options = {}) ⇒ Array<Hash>
Performs the pan gesture between two coordinates.
Swipes, scrolls, drag-and-drop, and flicks are all pan gestures.
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/calabash-cucumber/core.rb', line 503 def pan(from_query, to_query, ={}) = { # Minimum value for UIAutomation is 0.5. # DeviceAgent duration must be > 0. :duration => 1.0 }.merge() duration = [:duration] if uia_available? && duration < 0.5 raise ArgumentError, %Q[ Invalid duration: #{duration} The minimum duration is 0.5 ] elsif duration <= 0.0 raise ArgumentError, %Q[ Invalid duration: #{duration} The minimum duration is 0.0. ] end # TODO validate first_touch_hold_duration launcher.automator.pan(from_query, to_query, ) end |
#pan_coordinates(from_point, to_point, options = {}) ⇒ Object
Performs the pan gesture between two coordinates.
Swipes, scrolls, drag-and-drop, and flicks are all pan gestures.
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
# File 'lib/calabash-cucumber/core.rb', line 565 def pan_coordinates(from_point, to_point, ={}) = { # Minimum value for UIAutomation is 0.5. # DeviceAgent duration must be > 0. :duration => 1.0 }.merge() duration = [:duration] if uia_available? && duration < 0.5 raise ArgumentError, %Q[ Invalid duration: #{duration} The minimum duration is 0.5 ] elsif duration <= 0.0 raise ArgumentError, %Q[ Invalid duration: #{duration} The minimum duration is 0.0. ] end # TODO validate first_touch_hold_duration launcher.automator.pan_coordinates(from_point, to_point, ) end |
#pinch(in_out, options = {}) ⇒ Array<Hash>
Performs a “pinch” gesture.
By default, the gesture starts at the center of the screen.
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/calabash-cucumber/core.rb', line 622 def pinch(in_out, ={}) = { :query => nil, # Ignored by UIAutomation :amount => 100, :duration => 0.5 }.merge() symbol = in_out.to_sym if ![:in, :out].include?(symbol) raise ArgumentError, %Q[ Invalid pinch direction: '#{symbol}'. Valid directions are: "in", "out", :in, :out ] end duration = [:duration] if uia_available? && duration < 0.5 raise ArgumentError, %Q[ Invalid duration: #{duration} The minimum duration is 0.5 ] elsif duration <= 0.0 raise ArgumentError, %Q[ Invalid duration: #{duration} The minimum duration is 0.0. ] end launcher.automator.pinch(in_out.to_sym, ) end |
#query(uiquery, *args) ⇒ Object
The core method for querying into the current visible view of the app under test. The query method takes as first parameter a String ‘:uiquery`. This string must follow the query syntax described in: / Query Syntax.
Optionally ‘query` takes a variable number of “invocation” arguments (`args` below). # If called with an empty list of `*args`, `query` will find the views specified by `uiquery` and return a serialized view (see Examples below).
If ‘*args` are given, then they should describe a valid selector invocation on the queried views. For example `query(’UILabel’, :text)‘ would perform the `:text` selector on all visible `UILabel` objects and return those as an Array of Strings.
The ‘query` method provide a powerful mechanism for `querying` app view state and can be used to reflectively call arbitrary methods on objects reachable from the view. For a full specification of `*args` see / Query Syntax.
148 149 150 |
# File 'lib/calabash-cucumber/core.rb', line 148 def query(uiquery, *args) Map.map(uiquery, :query, *args) end |
#rotate(direction) ⇒ Symbol
Rotates the device in the direction indicated by ‘direction`.
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/calabash-cucumber/core.rb', line 241 def rotate(direction) as_symbol = direction.to_sym if as_symbol != :left && as_symbol != :right raise ArgumentError, "Expected '#{direction}' to be :left or :right" end launcher.automator.send(:rotate, as_symbol) end |
#rotate_home_button_to(position) ⇒ Symbol
A rotation will only occur if your view controller and application support the target orientation.
Rotates the home button to a position relative to the status bar.
Refer to Apple’s documentation for clarification about left vs. right landscape orientations.
For legacy support the ‘dir` argument can be a String or Symbol. Please update your code to pass a Symbol.
For legacy support ‘:top` and `top` are synonyms for `:up`. Please update your code to pass `:up`.
For legacy support ‘:bottom` and `bottom` are synonyms for `:down`. Please update your code to pass `:down`.
218 219 220 221 222 223 224 225 226 |
# File 'lib/calabash-cucumber/core.rb', line 218 def (position) normalized_symbol = expect_valid_rotate_home_to_arg(position) current_orientation = .to_sym return current_orientation if current_orientation == normalized_symbol launcher.automator.send(:rotate_home_button_to, normalized_symbol) end |
#scroll(uiquery, direction) ⇒ Object
this is implemented by calling the Obj-C ‘setContentOffset:animated:` method and can do things users cant.
Scroll a scroll view in a direction. By default scrolls half the frame size.
851 852 853 854 855 856 857 858 859 860 861 862 |
# File 'lib/calabash-cucumber/core.rb', line 851 def scroll(uiquery, direction) allowed_directions = [:up, :down, :left, :right] dir_symbol = direction.to_sym unless allowed_directions.include?(dir_symbol) raise ArgumentError, "Expected '#{direction} to be one of #{allowed_directions}" end views_touched = Map.map(uiquery, :scroll, dir_symbol) msg = "could not find view to scroll: '#{uiquery}', args: #{dir_symbol}" Map.assert_map_results(views_touched, msg) views_touched end |
#scroll_to_cell(options = {:query => "UITableView index:0", :row => 0, :section => 0, :scroll_position => :top, :animate => true}) ⇒ Object
should expose a non-option first argument query and required parameters ‘section`, `row`
Scroll a table view to a section and row.
Make sure your query matches exactly one UITableView. If multiple views are matched, the results can be unpredictable.
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 |
# File 'lib/calabash-cucumber/core.rb', line 968 def scroll_to_cell(={:query => "UITableView index:0", :row => 0, :section => 0, :scroll_position => :top, :animate => true}) uiquery = [:query] || 'tableView' row = [:row] sec = [:section] if row.nil? || sec.nil? raise ArgumentError, 'You must supply both :row and :section keys to scroll_to_cell' end args = [] if .has_key?(:scroll_position) args << [:scroll_position] else args << 'top' end if .has_key?(:animate) args << [:animate] end views_touched = Map.map(uiquery, :scrollToRow, row.to_i, sec.to_i, *args) msg = "unable to scroll: '#{uiquery}' to '#{}'" Map.assert_map_results(views_touched, msg) views_touched end |
#scroll_to_collection_view_item(item_index, section_index, options = {}) ⇒ Object
item and section are zero-indexed
Scrolls to an item in a section of a UICollectionView.
Make sure your query matches exactly one UICollectionView. If multiple views are matched, the results can be unpredictable.
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 |
# File 'lib/calabash-cucumber/core.rb', line 1109 def scroll_to_collection_view_item(item_index, section_index, ={}) = {:query => "UICollectionView index:0", :scroll_position => :top, :animate => true, :failure_message => nil} = .merge() uiquery = [:query] if uiquery.nil? raise ArgumentError, "The :query option cannot be nil" end if uiquery == "" raise ArgumentError, "The :query option cannot be the empty string" end if uiquery == "*" raise ArgumentError, "The :query option cannot be the wildcard '*'" end if item_index < 0 raise ArgumentError, "Invalid item index: '#{item_index}' - must be >= 0" end if section_index < 0 raise ArgumentError, "Invalid section index: '#{section_index}' - must be >= 0" end scroll_position = [:scroll_position] candidates = [:top, :center_vertical, :bottom, :left, :center_horizontal, :right] if !candidates.include?(scroll_position) raise ArgumentError, %Q[ Invalid :scroll_position option '#{scroll_position}'. Valid options are: #{candidates.join(", ")} ] end animate = [:animate] views_touched = Map.map(uiquery, :collectionViewScroll, item_index.to_i, section_index.to_i, scroll_position, animate) = [:failure_message] if ! = %Q[ Unable to scroll to item index '#{item_index}' in section index '#{section_index}' in CollectionView matched by: #{uiquery} with options: #{} ] end Map.assert_map_results(views_touched, ) views_touched end |
#scroll_to_collection_view_item_with_mark(mark, options = {}) ⇒ Object
Scrolls to mark in a UICollectionView.
Make sure your query matches exactly one UICollectionView. If multiple views are matched, the results can be unpredictable.
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 |
# File 'lib/calabash-cucumber/core.rb', line 1210 def scroll_to_collection_view_item_with_mark(mark, ={}) = {:query => "UICollectionView index:0", :scroll_position => :top, :animate => true, :failure_message => nil} = .merge() uiquery = [:query] if mark.nil? raise ArgumentError, "The mark cannot be nil" end if uiquery.nil? raise ArgumentError, "The :query option cannot be nil" end if uiquery == "" raise ArgumentError, "The :query option cannot be the empty string" end if uiquery == "*" raise ArgumentError, "The :query option cannot be the wildcard '*'" end scroll_position = [:scroll_position] candidates = [:top, :center_vertical, :bottom, :left, :center_horizontal, :right] if !candidates.include?(scroll_position) raise ArgumentError, %Q[ Invalid :scroll_position option '#{scroll_position}'. Valid options are: #{candidates.join(", ")} ] end args = [scroll_position, [:animate]] views_touched = Map.map(uiquery, :collectionViewScrollToItemWithMark, mark, *args) = [:failure_message] if ! = %Q[ Unable to scroll to item with mark '#{mark}' in UICollectionView matching query: #{uiquery} with options: #{} ] end Map.assert_map_results(views_touched, ) views_touched end |
#scroll_to_mark(mark, options = {}) ⇒ Object
Scrolls to a mark in a UIScrollView.
Make sure your query matches exactly one UIScrollView. If multiple scroll views are matched, the results can be unpredictable.
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/calabash-cucumber/core.rb', line 889 def scroll_to_mark(mark, ={}) if mark.nil? raise ArgumentError, "The mark cannot be nil" end = {:query => "UIScrollView index:0", :animate => true, :failure_message => nil}.merge() uiquery = [:query] if uiquery.nil? raise ArgumentError, "The :query option cannot be nil" end if uiquery == "" raise ArgumentError, "The :query option cannot be the empty string" end if uiquery == "*" raise ArgumentError, "The :query option cannot be the wildcard '*'" end args = [[:animate]] views_touched = Map.map(uiquery, :scrollToMark, mark, *args) = [:failure_message] if ! = %Q[ Unable to scroll to mark '#{mark}' in UIScrollView matching #{uiquery}" ] end Map.assert_map_results(views_touched, ) views_touched end |
#scroll_to_row(uiquery, number) ⇒ Object
Scroll a table view to a row. Table view should have only one section.
Make sure your query matches exactly one UITableView. If multiple views are matched, the results can be unpredictable.
941 942 943 944 945 946 |
# File 'lib/calabash-cucumber/core.rb', line 941 def scroll_to_row(uiquery, number) views_touched = Map.map(uiquery, :scrollToRow, number) msg = "Unable to scroll to row #{number} in table view with '#{uiquery}'" Map.assert_map_results(views_touched, msg) views_touched end |
#scroll_to_row_with_mark(mark, options = {}) ⇒ Object
Scrolls to a mark in a UITableView.
Make sure your query matches exactly one UITableView. If multiple views are matched, the results can be unpredictable.
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 |
# File 'lib/calabash-cucumber/core.rb', line 1024 def scroll_to_row_with_mark(mark, ={}) = {:query => "UITableView index:0", :scroll_position => :middle, :animate => true, :failure_message => nil}.merge() if mark.nil? raise ArgumentError, "The mark cannot be nil" end uiquery = [:query] if uiquery.nil? raise ArgumentError, "The :query option cannot be nil" end if uiquery == "" raise ArgumentError, "The :query option cannot be the empty string" end if uiquery == "*" raise ArgumentError, "The :query option cannot be the wildcard '*'" end args = [[:scroll_position], [:animate]] views_touched = Map.map(uiquery, :scrollToRowWithMark, mark, *args) = [:failure_message] if ! = %Q[ Unable to scroll to mark: '#{mark}' in table view matched by query: #{uiquery} with options: #{} ] end Map.assert_map_results(views_touched, ) views_touched end |
#send_app_to_background(seconds) ⇒ Object
Sends the app to the background.
Sending the app to the background for more than 60 seconds may cause unpredictable results.
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 |
# File 'lib/calabash-cucumber/core.rb', line 1277 def send_app_to_background(seconds) if seconds < 1.0 raise ArgumentError, "Seconds '#{seconds}' must be >= 1.0" end parameters = { :duration => seconds } begin body = http({:method => :post, :path => "suspend"}, parameters) result = response_body_to_hash(body) rescue RuntimeError => e raise RuntimeError, e end if result["outcome"] != "SUCCESS" raise RuntimeError, %Q{Could not send app to background: reason => '#{result["reason"]}' details => '#{result["details"]}' } end result["results"] end |
#server_log_level ⇒ String
Get the Calabash server log level.
1549 1550 1551 |
# File 'lib/calabash-cucumber/core.rb', line 1549 def server_log_level _debug_level_response(http(:method => :get, :path => 'debug')) end |
#server_version ⇒ String
Returns the version of the running calabash server.
173 174 175 |
# File 'lib/calabash-cucumber/core.rb', line 173 def server_version JSON.parse(http(:path => 'version')) end |
#set_location(options) ⇒ Object
Seems UIAutomation is broken here on physical devices on iOS 7.1
Simulates gps location of the device/simulator.
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 |
# File 'lib/calabash-cucumber/core.rb', line 1341 def set_location() if uia_available? uia_set_location() else if [:place] res = location_for_place([:place]) lat = res.latitude lon = res.longitude else lat = [:latitude] lon = [:longitude] end body_data = {:action => :change_location, :latitude => lat, :longitude => lon} body = http({:method => :post, :path => 'location'}, body_data) res = JSON.parse(body) if res['outcome'] != 'SUCCESS' screenshot_and_raise "Set location change failed, for #{lat}, #{lon} (#{body})." end res['results'] end end |
#set_server_log_level(level) ⇒ Object
Set the Calabash server log level.
1555 1556 1557 |
# File 'lib/calabash-cucumber/core.rb', line 1555 def set_server_log_level(level) _debug_level_response(http({:method => :post, :path => 'debug'}, {:level => level})) end |
#set_text(uiquery, txt) ⇒ Array<String>
Sets the text value of the views matched by uiquery
to txt
.
You should always try to enter text “like the user would” using the ‘keyboard_enter_text` method. There are cases, however, when this does not work or is very slow.
Please note that if you use this method, the UITextFieldDelegate and UITextViewDelegate methods ***will not be called*** if you use this method of text entry. This means that if you have UI elements that respond to text changes, they ***will not be updated***.
UIAutomation’s keyboard.typeString is incredibly buggy. Calabash goes to great lengths to provide a stable typing interface. However, there are cases where our patches cause problems. If your app crashes or hangs when calling ‘keyboard_enter_text` there are a couple of options.
-
Try ‘fast_enter_text`. This may or may not cause delegate methods to be called (see the note above).
-
Call ‘keyboard.typeString` directly. This will bypass the Calabash fixes (which sometimes cause hanging and/or crashes).
“‘ touch(“ < touch a text field or text view > ”) wait_for_keyboard uia(“UIATarget.localTarget().frontMostApp().keyboard().typeString(’your string’)”) “‘
Please be aware that keyboard.typeString is prone to errors. We recommend using ‘keyboard_enter_text` or `fast_enter_text` whenever possible.
One valid use of this method is on WebViews. Find examples in the [CalWebApp features/steps/set_text_steps.rb](github.com/calabash/ios-webview-test-app/blob/master/CalWebViewApp/features/steps/set_text_steps.rb).
@raise If the uiquery
finds no matching queries or finds a view that does not respond to the objc selector ‘setText’
1704 1705 1706 1707 1708 1709 1710 |
# File 'lib/calabash-cucumber/core.rb', line 1704 def set_text(uiquery, txt) text_fields_modified = Map.map(uiquery, :setText, txt) msg = "query '#{uiquery}' returned no matching views that respond to 'setText'" Map.assert_map_results(text_fields_modified, msg) text_fields_modified end |
#set_user_pref(key, val) ⇒ Object
Sets user preference (NSUserDefaults) value of key ‘key` to `val`.
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 |
# File 'lib/calabash-cucumber/core.rb', line 1747 def set_user_pref(key, val) res = http({:method => :post, :path => 'userprefs'}, {:key=> key, :value => val}) res = JSON.parse(res) if res['outcome'] != 'SUCCESS' screenshot_and_raise "set_user_pref #{key} = #{val} failed because: #{res['reason']}\n#{res['details']}" end res['results'] end |
#shake(seconds) ⇒ Object
Cause the device to shake.
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 |
# File 'lib/calabash-cucumber/core.rb', line 1307 def shake(seconds) if seconds <= 0.0 raise ArgumentError, "Seconds '#{seconds}' must be >= 0.0" end parameters = { :duration => seconds } begin body = http({:method => :post, :path => "shake"}, parameters) result = response_body_to_hash(body) rescue RuntimeError => e raise RuntimeError, e end if result["outcome"] != "SUCCESS" raise RuntimeError, %Q{Could not shake the device: reason => '#{result["reason"]}' details => '#{result["details"]}' } end result["results"] end |
#slider_set_value(uiquery, value, options = {}) ⇒ Array<String>
Set the sliders indicated by ‘uiquery` to `value`.
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 |
# File 'lib/calabash-cucumber/core.rb', line 1435 def (uiquery, value, ={}) = {:animate => true, :notify_targets => true} = .merge() value_str = value.to_s args = [[:animate], [:notify_targets]] views_touched = Map.map(uiquery, :changeSlider, value_str, *args) msg = "Could not set value of slider to '#{value}' using query '#{uiquery}'" Map.assert_map_results(views_touched, msg) views_touched end |
#start_test_server_in_background(args = {}) ⇒ Calabash::Cucumber::Launcher
It is not recommended to call this method outside of the calabash console. Call ‘Calabash::Cucumber::Launcher#relaunch instead.
Starts the app and Calabash test server in the console.
1573 1574 1575 1576 1577 1578 |
# File 'lib/calabash-cucumber/core.rb', line 1573 def start_test_server_in_background(args={}) stop_test_server launcher = Calabash::Cucumber::Launcher.new launcher.relaunch(args) launcher end |
#swipe(direction, options = {}) ⇒ Array<Hash>
Performs a “swipe” gesture.
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/calabash-cucumber/core.rb', line 396 def swipe(direction, ={}) = { :query => nil, :force => :normal }.merge() [:direction] = direction.to_sym if ![:up, :down, :left, :right].include?([:direction]) raise ArgumentError, %Q[ Invalid direction argument: '#{direction}'. Valid directions are: :up, :down, :left, and :right ] end if ![:light, :strong, :normal].include?([:force]) raise ArgumentError, %Q[ Invalid force option: '#{[:force]}'. Valid forces are: :strong, :normal, :light ] end launcher.automator.swipe() end |
#tap_keyboard_action_key ⇒ Object
Not all keyboards have an action key. For example, numeric keyboards do not have an action key.
Touches the keyboard action key.
The action key depends on the keyboard. Some examples include:
-
Return
-
Next
-
Go
-
Join
-
Search
725 726 727 728 |
# File 'lib/calabash-cucumber/core.rb', line 725 def tap_keyboard_action_key expect_keyboard_visible! launcher.automator.tap_keyboard_action_key end |
#tap_keyboard_delete_key ⇒ Object
Touches the keyboard delete key.
733 734 735 736 |
# File 'lib/calabash-cucumber/core.rb', line 733 def tap_keyboard_delete_key expect_keyboard_visible! launcher.automator.tap_keyboard_delete_key end |
#tap_mark(label, *args) ⇒ Array<Hash>
taps a view with mark ‘label`. Equivalent to `touch(“* marked:’##label‘”)`
1652 1653 1654 |
# File 'lib/calabash-cucumber/core.rb', line 1652 def tap_mark(label, *args) touch("view marked:'#{label}'", *args) end |
#tap_point(x, y) ⇒ Boolean
Performs the ‘tap` gesture on an absolute coordinate.
300 301 302 |
# File 'lib/calabash-cucumber/core.rb', line 300 def tap_point(x,y) touch(nil, offset: {x:x, y:y}) end |
#touch(uiquery, options = {}) ⇒ Array<Hash>
Performs the ‘tap` gesture on the (first) view that matches query `uiquery`. Note that `touch` assumes the view is visible and not animating. If the view is not visible `touch` will fail. If the view is animating `touch` will silently fail.
By default, taps the center of the view. tapped view.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/calabash-cucumber/core.rb', line 276 def touch(uiquery, ={}) if uiquery.nil? offset = [:offset] if !(offset && offset[:x] && offset[:y]) raise ArgumentError, %Q[ If query is nil, there must be a valid offset in the options. Expected: options[:offset] = {:x => NUMERIC, :y => NUMERIC} Actual: options[:offset] = #{offset ? offset : "nil"} ] end end (:touch, uiquery, ) end |
#touch_hold(uiquery, options = {}) ⇒ Array<Hash>
This assumes the view is visible and not animating.
Performs the “long press” or “touch and hold” gesture on the (first) view that matches query ‘uiquery`.
If the view is not visible it will fail with an error. If the view is animating it will silently fail.
By default, the gesture starts at the center of the view.
366 367 368 |
# File 'lib/calabash-cucumber/core.rb', line 366 def touch_hold(uiquery, ={}) (:touch_hold, uiquery, ) end |
#two_finger_tap(uiquery, options = {}) ⇒ Array<Hash>
This assumes the view is visible and not animating.
Performs the “two-finger tap” gesture on the (first) view that matches query ‘uiquery`.
If the view is not visible it will fail with an error. If the view is animating it will silently fail.
By default, taps the center of the view.
342 343 344 |
# File 'lib/calabash-cucumber/core.rb', line 342 def two_finger_tap(uiquery,={}) (:two_finger_tap, uiquery, ) end |
#user_pref(key) ⇒ Object
Gets the user preference (NSUserDefaults) value for a key.
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 |
# File 'lib/calabash-cucumber/core.rb', line 1761 def user_pref(key) res = http({:method => :get, :raw => true, :path => 'userprefs'}, {:key=> key}) res = JSON.parse(res) if res['outcome'] != 'SUCCESS' screenshot_and_raise "get user_pref #{key} failed because: #{res['reason']}\n#{res['details']}" end res['results'].first end |