Monday, 10 April 2017

Select dropdown in Ruby | Selenium


Note| 
 
Text in Blue [#PYTHON], Red [#RUBY], and Orange [#JAVA] can be edited or mentioned important for the entire blog. All the posts are practically done by me.


Select dropdown


Add below snippet in the step definition file,

When(/^I select using "([^"]*)" with data "([^"]*)" from "([^"]*)" on the xyz page$/) do |event, value, dropdown|
  yourpage_data = PageName.new(@driver)
  case event
    when "position"
      yourpage_data.select_by_position(value, dropdown)
    when "value"
      yourpage_data.select_by_value(value, dropdown)
    else
      raise ArgumentError, "can't find the event #{event}"
  end
end

Select by position

Make sure you create cucumber steps before step definitions; say,

And I select using "position" with data "1" from "your_locator" on the xyz page


Add the following snippets inside the page file,

your_locator_dropdown = @driver.find_element(:css => "your_css_locator")
your_locator = @driver.find_elements(:css => "your_css_locator")

def select_by_position(value, dropdown)
  __send__("#{dropdown}_dropdown").click
  __send__("#{dropdown}").each_with_index do |elem, index|
    if index == value
      elem.click
      break
    end
  end
end



Select by value

Make sure you create cucumber steps before step definitions; say,

And I select using "value" with data "demo_value" from "your_locator" on the xyz page


Add the following snippets inside the page file,

your_locator_dropdown = @driver.find_element(:css => "your_css_locator")
your_locator = @driver.find_elements(:css => "your_css_locator")

def select_by_value(value, dropdown)
  __send__("#{dropdown}_dropdown").click
  __send__("#{dropdown}").each do |elem|
    if elem.attribute("value").to_s == value.to_s
      elem.click
      break
    end
  end
end


Select by last value

Make sure you create cucumber steps before step definitions; say,

And I select using "last_value" with data "" from "your_locator" on the xyz page


Add the following snippets inside the page file,

your_locator_dropdown = @driver.find_element(:css => "your_css_locator")
your_locator = @driver.find_elements(:css => "your_css_locator")

def select_by_last_value(dropdown)
  __send__("#{dropdown}_dropdown").click
  (__send__("#{dropdown}").last).click
end


Select by text

Make sure you create cucumber steps before step definitions; say,

And I select using "text" with data "demo_text" from "your_locator" on the xyz page


Add the following snippets inside the page file,

your_locator_dropdown = @driver.find_element(:css => "your_css_locator")
your_locator = @driver.find_elements(:css => "your_css_locator")

def select_by_text(value, dropdown)
  __send__("#{dropdown}_dropdown").click
  __send__("#{dropdown}").each do |elem|
    if elem.text.to_s == value.to_s
      elem.click
      break
    else
      puts "#{elem.text} not matching #{value}"
    end
  end
end

2 comments:

  1. Very Useful information that i have found. don't stop sharing and Please keep updating us..... Thanks

    ReplyDelete