Wednesday, March 25, 2015

Difference between "include" and "extend" in Ruby?

extend: 
If you're using a module, that means you're bringing all the methods into your class. If you extend a class with a module, that means you're "bringing in" the module's methods as class methods.

include:
If you include a class with a module, that means you're "bringing in" the module's methods as instance methods.

EX:


 module A
   def say
     puts "this is module A"
   end
 end

 class B
   include A
 end

 class C
   extend A
 end

Output:

 B.say
 => undefined method 'say' for B:Class



 B.new.say
 => this is module A



 C.say
 => this is module A



 C.new.say
 => undefined method 'say' for C:Class

What is the use of 'require' in ruby ?

Please run below commands on rails console.

Input:     require "file_name"
Output:  true => load the file

Input:    require "file_name"
Output: false => already load

Input:    require "file_name"
Output: Error => Need to add this file

RUBY:: Form submit by webdriver

Use of selenium webdriver

require 'rubygems'
require 'selenium-webdriver'
require 'json'

For remote access
caps = Selenium::WebDriver::Remote::Capabilities.chrome
driver = Selenium::WebDriver.for(:remote, :url => "server_url", :desired_capabilities => caps)

For local access
driver = Selenium::WebDriver.for :firefox


driver.navigate.to "http://www.facebook.com/"
driver.find_element(:id, 'email').send_keys "aemailcom"
driver.find_element(:id, 'pass').send_keys "apssword"
driver.find_element(:xpath, "//input[@value='Log In']").click
puts driver.title
driver.quit

Use of watir webdriver

require 'rubygems'
require 'watir-webdriver'

driver = Watir::Browser.new :firefox
driver.navigate.to "http://www.facebook.com/"
driver.find_element(:id, 'email').send_keys "aemailcom"
driver.find_element(:id, 'pass').send_keys "apssword"
driver.find_element(:xpath, "//input[@value='Log In']").click
puts driver.url
driver.close