Webdriver in Ruby, how to make coke reusable by making classes -
i have simple webdriver test in ruby below:
require 'rubygems' require 'selenium-webdriver' driver = selenium::webdriver.for :firefox # example application under test driver.get "https://dev08-olb.nz.thenational.com/ib4b/app/login" element = driver.find_element :id => "accessid" element.send_keys "123456" element = driver.find_element :id => "userid" element.send_keys "user1" element = driver.find_element :id => "password" element.send_keys "password1" element.submit if "hello".eql? driver.title puts "page title #{driver.title}" else puts "damn it, must monday" end driver.quit
in future there lots of other cases require log in want repeat less possible create following 2 ruby scripts.
login.rb
require 'rubygems' require 'selenium-webdriver' class login def initialize(accessid, user, pass) @accessid = accessid @user = user @pass = pass driver = selenium::webdriver.for :firefox driver.get "https://dev08-olb.nz.thenational.com/ib4b/app/login" element = driver.find_element :id => "accessid" element.send_keys @accessid element = driver.find_element :id => "userid" element.send_keys @user element = driver.find_element :id => "password" element.send_keys @pass element.submit end end
helloworld.rb
require 'rubygems' require 'selenium-webdriver' require './login' login.new("123456","user1","password1") driver = selenium::webdriver.for :firefox if "hello".eql? driver.title puts "page title #{driver.title}" else puts "damn it, must monday" end driver.quit
however in helloworld.rb driver gets instantiated twice 2 instance of browser gets open @ test execution, not desired. if don't instantiate driver in helloworld.rb, can't access property later.
what's way of managing these kind of situation?
your appreciated.
thanks in advance.
this more how program tests in ruby, rather selenium specific. there many ways of doing it.
here one, try initialize driver
helloworld.rb
, pass login.rb
helloworld.rb
require 'rubygems' require 'selenium-webdriver' require './login' driver = selenium::webdriver.for :firefox login.new(driver, "123456","user1","password1") if "hello".eql? driver.title puts "page title #{driver.title}" else puts "damn it, must monday" end driver.quit
login.rb
require 'rubygems' require 'selenium-webdriver' class login def initialize(driver, accessid, user, pass) @driver = driver @accessid = accessid @user = user @pass = pass driver.get "https://dev08-olb.nz.thenational.com/ib4b/app/login" element = driver.find_element :id => "accessid" element.send_keys @accessid element = driver.find_element :id => "userid" element.send_keys @user element = driver.find_element :id => "password" element.send_keys @pass element.submit end end
Comments
Post a Comment