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

Tuesday 4 April 2017

How to use JSON file as Object Repository in Ruby ?


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.


JSON as Object Repository in Ruby


Create a JSON formatted object repository file; say, or.json

{
  "login_id": {
    "selector": ":css",
    "value": "#login"
  }
}

Now, create a module; say, utilities.rb

module Modulename
def loc(element)
  file = File.read(File.dirname(__FILE__) + "/../helpers/or.json")
  @driver.find_element(eval(JSON.parse(file)["#{element}"]['selector']), JSON.parse(file)["#{element}"]['value'])
end
end

Add the path in your env.rb

require File.dirname(__FILE__) + "/../helpers/utilities"
include Modulename

Finally call the loc() method in a page file as shown below,

loc("login_id").click