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

3 comments:

  1. nice blog. helped me a lot. share some more tutorials???????

    ReplyDelete
  2. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.
    When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    thanks a lot for taking a time to share a wonderful article.
    Best Selenium Training Institute in Chennai |
    Selenium Training in Velachery

    ReplyDelete