Tuesday, March 31, 2015

Difference between nil and false in ruby

The differences of the methods nil and false are:

- nil cannot be a value, where as a false can be a value
- A method returns true or false in case of a predicate, other wise nil is returned.
- false is a boolean data type, where as nil is not.
- nil is an object for NilClass, where as false is an object of for FalseClass

In sort::

False is a boolean datatype 
Nil is not a data type 

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

Tuesday, March 24, 2015

Install selenium-webdriver and watir-webdriver for ruby

If you are using Ruby for test automation then you probably are already familiar with developing in Ruby. To add Selenium and watir to your Ruby environment run the following command from a command-line.

gem install selenium-webdriver

and

gem install watir-webdriver

Thursday, March 19, 2015

Ruby's reserved words

Reserved word     Description
BEGIN             Code, enclosed in { and }, to run before the program runs.
END               Code, enclosed in { and }, to run when the program ends.
alias             Creates an alias for an existing method, operator, or global variable.
and               Logical operator; same as && except and has lower precedence.
begin             Begins a code block or group of statements; closes with end.
break             Terminates a while or until loop or a method inside a block.
\case             Compares an expression with a matching when clause; closes with end.
class             Defines a class; closes with end.
def               Defines a method; closes with end.
defined?          Determines if a variable, method, super method, or block exists.
do                Begins a block and executes code in that block; closes with end.
else              Executes if previous conditional, in if, elsif, unless, or when, is not true.
elsif             Executes if previous conditional, in if or elsif, is not true.
end               Ends a code block (group of statements) starting with begin, def, do, if, etc.
ensure            Always executes at block termination; use after last rescue.
false             Logical or Boolean false, instance of FalseClass. (See true.)
for               Begins a for loop; used with in.
if                Executes code block if true. Closes with end. 
module            Defines a module; closes with end.
next              Jumps before a loop's conditional.
nil               Empty, uninitialized variable, or invalid, but not the same as zero; object of NilClass.
not               Logical operator; same as !.
or                Logical operator; same as || except or has lower precedence.
redo              Jumps after a loop's conditional.
rescue            Evaluates an expression after an exception is raised; used before ensure.
retry             Repeats a method call outside of rescue; jumps to top of block (begin) if inside rescue.
return            Returns a value from a method or block. May be omitted.
self              Current object (invoked by a method).
super             Calls method of the same name in the superclass. The superclass is the parent of this class.
then              A continuation for if, unless, and when. May be omitted.
true              Logical or Boolean true, instance of TrueClass.
undef             Makes a method in current class undefined.
unless            Executes code block if conditional statement is false.
until             Executes code block while conditional statement is false.
when              Starts a clause (one or more) under case.
while             Executes code while the conditional statement is true.
yield             Executes the block passed to the method.
_ _FILE_ _        Name of current source file.
_ _LINE_ _     Number of current line in the current source file.

Convert json to html view

json = {"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

function syntaxHighlight(json) {
    if (typeof json != 'string') {
         json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

syntaxHighlight(json)

Output::

FastAPI: The Modern Python Web Framework

  Introduction to FastAPI: The Modern Python Web Framework The world of web frameworks has always been competitive — from Django and Flask ...