From 494c6ec158c2828a7892cd3b9e5378314ed24417 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Wed, 10 Oct 2018 16:41:59 +0530 Subject: [PATCH 01/10] Added lookup method for reusing locators and data in multiple feature files --- .../methods/assertion_methods.rb | 16 +++++++-------- .../methods/click_elements_methods.rb | 8 ++++---- .../methods/input_methods.rb | 20 +++++++++---------- lib/selenium-cucumber/methods/misc_methods.rb | 4 ++++ .../methods/mobile_methods.rb | 10 +++++----- .../methods/navigate_methods.rb | 6 +++--- .../methods/progress_methods.rb | 4 ++-- 7 files changed, 36 insertions(+), 32 deletions(-) diff --git a/lib/selenium-cucumber/methods/assertion_methods.rb b/lib/selenium-cucumber/methods/assertion_methods.rb index 17001d3..32171c0 100644 --- a/lib/selenium-cucumber/methods/assertion_methods.rb +++ b/lib/selenium-cucumber/methods/assertion_methods.rb @@ -38,7 +38,7 @@ def check_partial_title(partial_text_title, test_case) # param 1 : String : Locator type (id, name, class, xpath, css) # param 2 : String : Locator value def get_element_text(access_type, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").text + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").text end # Method to check element text @@ -74,7 +74,7 @@ def check_element_partial_text(access_type, expected_value, access_name, test_ca # param 1 : String : Locator type (id, name, class, xpath, css) # param 2 : String : Locator value def is_element_enabled(access_type, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").enabled? + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").enabled? end # Element enabled checking @@ -95,7 +95,7 @@ def check_element_enable(access_type, access_name, test_case) # param 2 : String : Expected element text # param 3 : String : atrribute name def get_element_attribute(access_type, access_name, attribute_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").attribute("#{attribute_name}") + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").attribute("#{attribute_name}") end # method to check attribute value @@ -120,7 +120,7 @@ def check_element_attribute(access_type, attribute_name, attribute_value, access # param 2 : String : Locator value def is_element_displayed(access_type, access_name) begin - $driver.find_element(:"#{access_type}" => "#{access_name}").displayed? + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").displayed? rescue Selenium::WebDriver::Error::NoSuchElementError # elements not found return false false @@ -140,7 +140,7 @@ def check_element_presence(access_type, access_name, test_case) # param 2 : String : Locator value # param 3 : Boolean : test case [true or flase] def is_checkbox_checked(access_type, access_name, should_be_checked = true) - checkbox = $driver.find_element(:"#{access_type}" => "#{access_name}") + checkbox = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") expect(checkbox.selected?).to be should_be_checked end @@ -150,13 +150,13 @@ def is_checkbox_checked(access_type, access_name, should_be_checked = true) # param 2 : String : Locator value # param 3 : Boolean : test case [true or flase] def is_radio_button_selected(access_type, access_name, should_be_selected = true) - radio_button = $driver.find_element(:"#{access_type}" => "#{access_name}") + radio_button = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") expect(radio_button.selected?).to be should_be_selected end # method to assert option from radio button group is selected/unselected def is_option_from_radio_button_group_selected(access_type, by, option, access_name, should_be_selected = true) - radio_button_group = $driver.find_elements(:"#{access_type}" => "#{access_name}") + radio_button_group = $driver.find_elements(:"#{access_type}" => "#{lookup(access_name)}") getter = ->(rb, by) { by == 'value' ? rb.attribute('value') : rb.text } @@ -176,7 +176,7 @@ def check_alert_text(text) end def is_option_from_dropdown_selected(access_type, by, option, access_name, should_be_selected=true) - dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") + dropdown = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) if by == 'text' diff --git a/lib/selenium-cucumber/methods/click_elements_methods.rb b/lib/selenium-cucumber/methods/click_elements_methods.rb index 0ae219d..3605696 100644 --- a/lib/selenium-cucumber/methods/click_elements_methods.rb +++ b/lib/selenium-cucumber/methods/click_elements_methods.rb @@ -1,7 +1,7 @@ require_relative 'required_files' def click(access_type, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").click + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").click end def click_by_text(access_type, access_name, text) @@ -11,7 +11,7 @@ def click_by_text(access_type, access_name, text) raise "Text '#{text}' was not found in the page source" unless text_found # enter loop if the text is found if text_found - elements = $driver.find_elements(:"#{access_type}" => "#{access_name}") + elements = $driver.find_elements(:"#{access_type}" => "#{lookup(access_name)}") elements.each do |element| if element.text == text element_found = true @@ -24,7 +24,7 @@ def click_by_text(access_type, access_name, text) end def click_forcefully(access_type, access_name) - $driver.execute_script('arguments[0].click();', $driver.find_element(:"#{access_type}" => "#{access_name}")) + $driver.execute_script('arguments[0].click();', $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}")) end def double_click(access_type, access_value) @@ -33,7 +33,7 @@ def double_click(access_type, access_value) end def submit(access_type, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").submit + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").submit end diff --git a/lib/selenium-cucumber/methods/input_methods.rb b/lib/selenium-cucumber/methods/input_methods.rb index 1eed69c..d475a53 100644 --- a/lib/selenium-cucumber/methods/input_methods.rb +++ b/lib/selenium-cucumber/methods/input_methods.rb @@ -2,44 +2,44 @@ # method to enter text into textfield def enter_text(access_type, text, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").send_keys text + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").send_keys text end # method to clear text from textfield def clear_text(access_type, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").clear + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").clear end # method to select option from dropdwon list def select_option_from_dropdown(access_type, by, option, access_name) - dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") + dropdown = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) select_list.select_by(:"#{by}", "#{option}") end # method to select all option from dropdwon list def select_all_option_from_multiselect_dropdown(access_type, access_name) - dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") + dropdown = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) select_list.select_all end # method to unselect all option from dropdwon list def unselect_all_option_from_multiselect_dropdown(access_type, access_name) - dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") + dropdown = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) select_list.deselect_all end # method to check checkbox def check_checkbox(access_type, access_name) - checkbox = $driver.find_element(:"#{access_type}" => "#{access_name}") + checkbox = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") checkbox.click unless checkbox.selected? end # method to uncheck checkbox def uncheck_checkbox(access_type, access_name) - checkbox = $driver.find_element(:"#{access_type}" => "#{access_name}") + checkbox = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") if checkbox.selected? checkbox.click @@ -48,18 +48,18 @@ def uncheck_checkbox(access_type, access_name) # method to select radio button def toggle_checkbox(access_type, access_name) - $driver.find_element(:"#{access_type}" => "#{access_name}").click + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").click end # method to select radio button def select_radio_button(access_type, access_name) - radio_button = $driver.find_element(:"#{access_type}" => "#{access_name}") + radio_button = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") radio_button.click unless radio_button.selected? end # method to select option from radio button group def select_option_from_radio_button_group(access_type, by, option, access_name) - radio_button_group = $driver.find_elements(:"#{access_type}" => "#{access_name}") + radio_button_group = $driver.find_elements(:"#{access_type}" => "#{lookup(access_name)}") getter = ->(rb, by) { by == 'value' ? rb.attribute('value') : rb.text } ele = radio_button_group.find { |rb| getter.call(rb, by) == option } diff --git a/lib/selenium-cucumber/methods/misc_methods.rb b/lib/selenium-cucumber/methods/misc_methods.rb index c00b97a..2c9cc36 100644 --- a/lib/selenium-cucumber/methods/misc_methods.rb +++ b/lib/selenium-cucumber/methods/misc_methods.rb @@ -32,3 +32,7 @@ def get_device_info IO.popen('adb shell getprop ro.build.version.release') { |f| $os_version = f.gets.chomp.upcase} return $device, $os_version end + +def lookup(key) + $lookup_table[key] || key +end \ No newline at end of file diff --git a/lib/selenium-cucumber/methods/mobile_methods.rb b/lib/selenium-cucumber/methods/mobile_methods.rb index 3f9bd9b..99b3b2d 100644 --- a/lib/selenium-cucumber/methods/mobile_methods.rb +++ b/lib/selenium-cucumber/methods/mobile_methods.rb @@ -53,11 +53,11 @@ def swipe_direction(direction) end def swipe_element_with_direction(access_type, access_name, direction) - ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.location + ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") }.location x_start = ele_from.x y_start = ele_from.y - ele_size = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") } + ele_size = WAIT.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") } ele_height = ele_size.size.height.to_i ele_width = ele_size.size.width.to_i #puts ele_size.width @@ -148,7 +148,7 @@ def swipe_element_with_direction(access_type, access_name, direction) def swipe_coordinates_with_direction(start_x, start_y, direction) - # ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.size + # ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") }.size # height = size.height.to_i - 5 # puts "height : #{height}" # 1776 # width = size.width.to_i - 5 @@ -190,7 +190,7 @@ def swipe_coordinates_with_direction(start_x, start_y, direction) def long_press_on_element_default_duration(access_type, access_name) begin - ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.location + ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") }.location x = ele_from.x y = ele_from.y @@ -207,7 +207,7 @@ def long_press_on_element_default_duration(access_type, access_name) def long_press_on_element_with_duration(access_type, access_name, duration) begin - ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.location + ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") }.location x = ele_from.x y = ele_from.y diff --git a/lib/selenium-cucumber/methods/navigate_methods.rb b/lib/selenium-cucumber/methods/navigate_methods.rb index d8c9a24..401de82 100644 --- a/lib/selenium-cucumber/methods/navigate_methods.rb +++ b/lib/selenium-cucumber/methods/navigate_methods.rb @@ -61,7 +61,7 @@ def zoom_in_out(operation) # Method to zoom in/out web page until web element displyas def zoom_in_out_till_element_display(access_type, operation, access_name) while true - if WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.displayed? + if WAIT.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") }.displayed? break else zoom_in_out(operation) @@ -81,13 +81,13 @@ def maximize_browser # Method to hover on element def hover_over_element(access_type, access_name) - element = $driver.find_element(:"#{access_type}" => "#{access_name}") + element = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") $driver.action.move_to(element).perform end # Method to scroll page to perticular element def scroll_to_element(access_type, access_name) - ele_scroll = $driver.find_element(:"#{access_type}" => "#{access_name}") + ele_scroll = $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}") ele_scroll.location_once_scrolled_into_view end diff --git a/lib/selenium-cucumber/methods/progress_methods.rb b/lib/selenium-cucumber/methods/progress_methods.rb index 1a2d1db..4a53c9b 100644 --- a/lib/selenium-cucumber/methods/progress_methods.rb +++ b/lib/selenium-cucumber/methods/progress_methods.rb @@ -6,10 +6,10 @@ def wait(time) def wait_for_element_to_display(access_type, access_name, duration) wait = Selenium::WebDriver::Wait.new(:timeout => duration.to_i) # seconds - wait.until { $driver.find_element(:"#{access_type}" => "#{access_name}").displayed? } + wait.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").displayed? } end def wait_for_element_to_enable(access_type, access_name, duration) wait = Selenium::WebDriver::Wait.new(:timeout => duration.to_i) # seconds - wait.until { $driver.find_element(:"#{access_type}" => "#{access_name}").enabled? } + wait.until { $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").enabled? } end From 6d683f27c353d8c59ed0da90e12c6e935b7b95d1 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Wed, 10 Oct 2018 17:11:07 +0530 Subject: [PATCH 02/10] Change driver.navigate funtion --- lib/selenium-cucumber/methods/navigate_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/selenium-cucumber/methods/navigate_methods.rb b/lib/selenium-cucumber/methods/navigate_methods.rb index 401de82..88c69e6 100644 --- a/lib/selenium-cucumber/methods/navigate_methods.rb +++ b/lib/selenium-cucumber/methods/navigate_methods.rb @@ -2,7 +2,7 @@ # method to open link def navigate_to(link) - $driver.get link + $driver.get lookup(link) end # method to navigate back & forword From 47184da0fa240cebb218f23a2995374244765fcd Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Wed, 10 Oct 2018 17:15:52 +0530 Subject: [PATCH 03/10] data also can be passed via lookup now --- lib/selenium-cucumber/methods/input_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/selenium-cucumber/methods/input_methods.rb b/lib/selenium-cucumber/methods/input_methods.rb index d475a53..1023e5c 100644 --- a/lib/selenium-cucumber/methods/input_methods.rb +++ b/lib/selenium-cucumber/methods/input_methods.rb @@ -2,7 +2,7 @@ # method to enter text into textfield def enter_text(access_type, text, access_name) - $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").send_keys text + $driver.find_element(:"#{access_type}" => "#{lookup(access_name)}").send_keys lookup(text) end # method to clear text from textfield From d9d0315fc650fed20efd92c85db3fb01e6e1b5c9 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Wed, 10 Oct 2018 17:29:19 +0530 Subject: [PATCH 04/10] changed assertion statement of text verification --- lib/selenium-cucumber/methods/assertion_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/selenium-cucumber/methods/assertion_methods.rb b/lib/selenium-cucumber/methods/assertion_methods.rb index 32171c0..a396700 100644 --- a/lib/selenium-cucumber/methods/assertion_methods.rb +++ b/lib/selenium-cucumber/methods/assertion_methods.rb @@ -49,9 +49,9 @@ def get_element_text(access_type, access_name) def check_element_text(access_type, expected_value, access_name, test_case) element_text = get_element_text(access_type, access_name) if test_case - expect(element_text).to eq expected_value + expect(element_text).to eq lookup(expected_value) else - expect(element_text).to_not eq expected_value + expect(element_text).to_not eq lookup(expected_value) end end From 3f817255e80cb4cb2f16f4067585a1369d8e7f64 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Thu, 11 Oct 2018 15:08:24 +0530 Subject: [PATCH 05/10] added lookup yaml read code and fixed review comments --- features-skeleton/support/env.rb | 1 + features-skeleton/variables.yaml | 2 ++ lib/selenium-cucumber/methods/misc_methods.rb | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 features-skeleton/variables.yaml diff --git a/features-skeleton/support/env.rb b/features-skeleton/support/env.rb index 85e842d..595c0c8 100644 --- a/features-skeleton/support/env.rb +++ b/features-skeleton/support/env.rb @@ -8,6 +8,7 @@ $device_name = ENV['DEVICE_NAME'] $udid = ENV['UDID'] $app_path = ENV['APP_PATH'] +$lookup_table = YAML.load(File.join(File.dirname(__FILE__), “../variables.yaml”) # check for valid parameters validate_parameters $platform, $browser_type, $app_path diff --git a/features-skeleton/variables.yaml b/features-skeleton/variables.yaml new file mode 100644 index 0000000..a9dbc07 --- /dev/null +++ b/features-skeleton/variables.yaml @@ -0,0 +1,2 @@ +--- +'name1': "locator1" \ No newline at end of file diff --git a/lib/selenium-cucumber/methods/misc_methods.rb b/lib/selenium-cucumber/methods/misc_methods.rb index 2c9cc36..638c0d7 100644 --- a/lib/selenium-cucumber/methods/misc_methods.rb +++ b/lib/selenium-cucumber/methods/misc_methods.rb @@ -35,4 +35,4 @@ def get_device_info def lookup(key) $lookup_table[key] || key -end \ No newline at end of file +end From 28ac1f01e01b6655932aff2c1acf189faac6e043 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Thu, 11 Oct 2018 15:09:52 +0530 Subject: [PATCH 06/10] Added newline in yaml file --- features-skeleton/variables.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features-skeleton/variables.yaml b/features-skeleton/variables.yaml index a9dbc07..a902d38 100644 --- a/features-skeleton/variables.yaml +++ b/features-skeleton/variables.yaml @@ -1,2 +1,2 @@ --- -'name1': "locator1" \ No newline at end of file +'name1': "locator1" From ff13b9e3e14f3a4887935a83452c69615d182f37 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Thu, 11 Oct 2018 15:16:32 +0530 Subject: [PATCH 07/10] Updated env.rb Fixed syntax error --- features-skeleton/support/env.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features-skeleton/support/env.rb b/features-skeleton/support/env.rb index 595c0c8..a5ed182 100644 --- a/features-skeleton/support/env.rb +++ b/features-skeleton/support/env.rb @@ -8,7 +8,7 @@ $device_name = ENV['DEVICE_NAME'] $udid = ENV['UDID'] $app_path = ENV['APP_PATH'] -$lookup_table = YAML.load(File.join(File.dirname(__FILE__), “../variables.yaml”) +$lookup_table = YAML.load(File.join(File.dirname(__FILE__), “../variables.yaml”)) # check for valid parameters validate_parameters $platform, $browser_type, $app_path From a12b98deca363ba5f8e7b3e53167c045dbcce9c0 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Thu, 11 Oct 2018 15:32:36 +0530 Subject: [PATCH 08/10] Changed double quotes --- features-skeleton/support/env.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features-skeleton/support/env.rb b/features-skeleton/support/env.rb index a5ed182..9733749 100644 --- a/features-skeleton/support/env.rb +++ b/features-skeleton/support/env.rb @@ -8,7 +8,7 @@ $device_name = ENV['DEVICE_NAME'] $udid = ENV['UDID'] $app_path = ENV['APP_PATH'] -$lookup_table = YAML.load(File.join(File.dirname(__FILE__), “../variables.yaml”)) +$lookup_table = YAML.load(File.join(File.dirname(__FILE__), '../variables.yaml')) # check for valid parameters validate_parameters $platform, $browser_type, $app_path From b1d5fc5e7f2516c626b02a93cb3e7103773ac2f4 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Tue, 16 Oct 2018 11:29:27 +0530 Subject: [PATCH 09/10] Update env.rb Changed yaml.load line --- features-skeleton/support/env.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features-skeleton/support/env.rb b/features-skeleton/support/env.rb index 9733749..9ed6741 100644 --- a/features-skeleton/support/env.rb +++ b/features-skeleton/support/env.rb @@ -8,7 +8,7 @@ $device_name = ENV['DEVICE_NAME'] $udid = ENV['UDID'] $app_path = ENV['APP_PATH'] -$lookup_table = YAML.load(File.join(File.dirname(__FILE__), '../variables.yaml')) +$lookup_table = YAML.load(File.open(File.join(File.dirname(__FILE__), '../variables.yaml'))) # check for valid parameters validate_parameters $platform, $browser_type, $app_path From 07561fed9f90f3dcedff2df02cb1358a38e85248 Mon Sep 17 00:00:00 2001 From: Sourabh Pataskar Date: Mon, 3 Dec 2018 17:38:14 +0700 Subject: [PATCH 10/10] Fixed partial text step to pass values from variables.yaml --- lib/selenium-cucumber/methods/assertion_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/selenium-cucumber/methods/assertion_methods.rb b/lib/selenium-cucumber/methods/assertion_methods.rb index a396700..bcccf37 100644 --- a/lib/selenium-cucumber/methods/assertion_methods.rb +++ b/lib/selenium-cucumber/methods/assertion_methods.rb @@ -64,9 +64,9 @@ def check_element_partial_text(access_type, expected_value, access_name, test_ca element_text = get_element_text(access_type, access_name) if test_case - expect(element_text).to include(expected_value) + expect(element_text).to include("#{lookup(expected_value)}") else - expect(element_text).to_not include(expected_value) + expect(element_text).to_not include("#{lookup(expected_value)}") end end