Monday, 4 May 2015

Cucumber _get scenario tags, feature name and scenario name during cucumber scenario execution in Ruby - Interesting..

Feature: Development feature
   To login to portal using valid credentials


   @portal   Scenario Outline: Login with valid credentials
      Given I launch portal login page
     Then I Sign in with valid credentials 
     Examples:     |brand|     |xyz|
   @portal    Scenario: Login with invalid credentials
     Given I launch portal login page
     Then sign in using invalid user_name and password
     And I should see error_message on login page
 
 
hooks.rb file:
 
Before do |scenario|
  # Feature name  case scenario    when Cucumber::Ast::Scenario      @feature_name = scenario.feature.name
    when Cucumber::Ast::OutlineTable::ExampleRow      @feature_name = scenario.scenario_outline.feature.name
  end  puts @feature_name
  # Scenario name  case scenario    when Cucumber::Ast::Scenario      @scenario_name = scenario.name
    when Cucumber::Ast::OutlineTable::ExampleRow      @scenario_name = scenario.scenario_outline.name
  end
  puts @scenario_name
  # Tags (as an array)  @scenario_tags = scenario.source_tag_names
  # puts "Printing tags: #{@scenario_tags}"end 


In Execution Results:

Development feature
To login to portal using valid credentials

Login with valid credentials

Printing tags: ["@portal"]

Thursday, 16 April 2015

Approve Rest Request without xml object:
Method: POST

def approve_order(anatwine_order_id)
   #Post Request    host = "https://something/approved"    user = 'User id'    pwd = 'Pwd'
    request_body_map = { } #if no body/xml object
    response = RestClient.post("#{host}", request_body_map ,{:authorization => "Basic #{Base64.strict_encode64("#{user}:#{pwd}")}"})
    response.code.to_s.must_equal '200'     responseend

Method: PUT

 def create_anatwine_order_id(channel, partner)
    file = File.open(xml_file)
    xml_obj= Nokogiri::XML(file)
    ##Note: Install ---------gem install rest-client -v 1.6.7 if you     get below error:     # #OpenSSL::X509::StoreError: No message available, rest-client     uri = create_order_uri(channel, partner)
    #eg= https://host/version/channel/partner/order 
    @response = RestClient.put(uri, xml_obj.to_xml,
    {:content_type => :'text/xml',
    :authorization => 'Basic pwd'     })
    @responseend


Method: GET

def get_response_code(response)
  response.code
end
 
def retrive_order(channel, retailer_order)
  # uri = "https://test/#{order_id}"  uri = build_retrive_order_uri(channel, retailer_order)
  @resource = RestClient::Resource.new(uri)
  @response = @resource.get(:Authorization => 'Basic' )
  @responseend 

Thursday, 2 April 2015

Ruby Interview Questions

http://anilpunjabi.tumblr.com/post/25948339235/ruby-and-rails-interview-questions-and-answers


Wednesday, 25 March 2015

Cucumber project structure creation in Ruby using testgen gem

Install the testgen gem:
ruby: gem install tesgen
JRuby: jruby -S gem install testgen


 
1. create normal cucumber project folder structure:

c:\>testgen project test_puppies
io/console not supported; tty will not be manipulated
      create  est_puppies
      create  est_puppies/cucumber.yml
      create  est_puppies/Gemfile
      create  est_puppies/Rakefile
      create  est_puppies/features
      create  est_puppies/features/support
      create  est_puppies/features/step_definitions
      create  est_puppies/features/support/env.rb


2. Watir Webdriver folder structure:

c:\>testgen project test_puppies1 --pageobject-driver=watir
io/console not supported; tty will not be manipulated
      create  est_puppies1
      create  est_puppies1/cucumber.yml
      create  est_puppies1/Gemfile
      create  est_puppies1/Rakefile
      create  est_puppies1/features
      create  est_puppies1/features/support
      create  est_puppies1/features/step_definitions
      create  est_puppies1/features/support/env.rb
      create  est_puppies1/features/support/hooks.rb
      create  est_puppies1/features/support/pages


2. SeleniumWebdriver folder structure:

c:\>testgen project test_puppies2 --pageobject-driver=selenium
       io/console not supported; tty will not be manipulated
      create  est_puppies2
      create  est_puppies2/cucumber.yml
      create  est_puppies2/Gemfile
      create  est_puppies2/Rakefile
      create  est_puppies2/features
      create  est_puppies2/features/support
      create  est_puppies2/features/step_definitions
      create  est_puppies2/features/support/env.rb
      create  est_puppies2/features/support/hooks.rb
      create  est_puppies2/features/support/pages


Tuesday, 24 March 2015

random numbers in ruby

random number: 5 digits:
numberofdigits = 5
puts rand(10**numberofdigits - 1).to_s
54686
==============
randon floating number:
number = (Random.new(rand(10000)).rand * 100).round / 1.0
puts number

51.0
==========
BigDecimal to integer
puts BigDecimal.new "0.961E4"
puts ("%f" % "0.961E4").to_i
puts 0.961E4.class == Float

output:
0.961E4
9610
true
=======================
Random string of length 10 characters:
range = [*'0'..'9',  *'a'..'z', *'A'..'Z']
puts Array.new(10) { range.sample }.join.upcase
output:
CBBAUTPRBX

xml builder in ruby- remove namespace etc

# updates node content 
def update_node_content(file_obj, key, index, code)
  getxml_elements_object(file_obj,
                         getxpath_forkey(key))[index].content = code.call
end# updates node content 
def update_node_native_content(file_obj, key, index, code)
  getxml_elements_object(file_obj,
                         getxpath_forkey(key))[index].native_content = code.call
end
def get_node(xmlFileObject, strNodeXPath)
  xmlFileObject.xpath(strNodeXPath)
end
def get_node_at(xmlFileObject, strNodeXPath, index=0)
  xmlFileObject.xpath(strNodeXPath)[index]
end
def add_node(xmlFileObject, strXpath, strNode, index)
  if xmlFileObject.xpath(strXpath).count >= 0    xmlFileObject.xpath(strXpath)[index].add_next_sibling(strNode)
    xmlFileObject  endend
# gets node countdef get_node_count(file_obj, key)
  getxml_elements_object(file_obj, getxpath_forkey(key)).count.to_s
end
# gets countdef get_count(xmlFileObject, xpath)
  xmlFileObject.xpath(xpath).count.to_s
end
# gets node contentdef get_node_content(file_obj, key, index)
  getxml_elements_object(file_obj, getxpath_forkey(key))[index].content
end
def remove_nodes_but_one(file_obj, xpath_key)
  count = file_obj.xpath(getxpath_forkey(xpath_key)).length
  while count > 1    file_obj.xpath(getxpath_forkey(xpath_key))[count-1].remove
    count-=1  endend

# xmlObject = setattribute_value(node,'key',"AUTI")
 def setattribute_value(node, attributename, valueto_assign)
  node.attributes[attributename].value = valueto_assign 
end


remove namespace from xml:
# xmlObject = get_xml_object("../Files/Product.xml") 
def get_xml_object(path)
  @file = File.read(path)
  xmlObj = Nokogiri::XML(@file, nil, 'utf-8')
  @get_namespaces = xml_obj.namespaces
  xmlObj.remove_namespaces!
  xmlObj 
end

in xml file:
<?xml version="1.0" encoding="utf-8"?>
 <order xmlns="http://testing.pic/party/export">

xmlns is the namespace..


Sunday, 22 March 2015

Key board operations using JRUBY and Robot class


blog: http://alvinalexander.com/java/java-robot-class-example-mouse-keystroke
http://ruby.about.com/od/Automation-and-FFI/fl/Automating-with-Robot.htm
key shortcuts: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html

eg: 
http://ruby.about.com/od/Automation-and-FFI/fl/Automating-with-Robot.htm

Use JRuby if you want to implement Robot class:

require 'rubygems'
require 'Selenium-webdriver'
require 'java'
java_import 'java.awt.Robot'
java_import 'java.awt.event.InputEvent'
java_import 'java.awt.event.KeyEvent'
class WatirTest
#  browser = Watir::Browser.new :firefox
  browser = Selenium::WebDriver.for :firefox
  browser.get 'http://gmail.com'
  sleep 3
  browser.find_element(:id, 'Email').send_keys 'vicky.venu81'
#  puts browser.execute_script("return document.body").inspect

  robot = Robot.new
 
  robot.key_press(KeyEvent::VK_ENTER)
  puts robot.methods
#  robot.mouseMove(963,10);

#  browser.quit
end


Working eg: 2
require 'java'
java_import 'java.awt.Robot'
java_import 'java.awt.event.InputEvent'
java_import 'java.awt.event.KeyEvent'

class RobotTest
  def testing
    x = 'a'
    puts x.ord
    
    puts "ASCII value of 'a': #{'a'.ord}"
    puts "ASCII value of 'z': #{'z'.ord}"
   
    puts "ASCII value of 'A': #{'A'.ord}"
    puts "ASCII value of 'Z': #{'Z'.ord}"
   
    word = "abcdefg"
    word.each_byte {|b| print b.chr }
    puts '+++++++++++++++++'
    sleep 5
    word.each_byte do |chr|
      puts chr
      robot = Robot.new
      code = chr-32
      robot.key_press(code)
    end

  end
end

RobotTest.new.testing()
 

Output:
97
ASCII value of 'a': 97
ASCII value of 'z': 122
ASCII value of 'A': 65
ASCII value of 'Z': 90
abcdefg+++++++++++++++++
97
98
99
100
101
102
103

 

 

++++++++++++++++++++++++++++++++
if wanted to implement additional methods using Robot class, ref the below the example in java:


public class JavaRobotExample
{
  Robot robot = new Robot();
  public static void main(String[] args) throws AWTException
  {
    new JavaRobotExample();
  }
   
  public JavaRobotExample() throws AWTException
  {
    robot.setAutoDelay(40);
    robot.setAutoWaitForIdle(true);
     
    robot.delay(4000);
    robot.mouseMove(40, 130);
    robot.delay(500);
    leftClick();
    robot.delay(500);
    leftClick();
    robot.delay(500);
    type("Hello, world");
    robot.mouseMove(40, 160);
    robot.delay(500);
    leftClick();
    robot.delay(500);
    leftClick();
     
    robot.delay(500);
    type("This is a test of the Java Robot class");
     
    robot.delay(50);
    type(KeyEvent.VK_DOWN);
     
    robot.delay(250);
    type("Four score and seven years ago, our fathers ...");
    robot.delay(1000);
    System.exit(0);
  }
   
  private void leftClick()
  {
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(200);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(200);
  }
   
  private void type(int i)
  {
    robot.delay(40);
    robot.keyPress(i);
    robot.keyRelease(i);
  }
  private void type(String s)
  {
    byte[] bytes = s.getBytes();
    for (byte b : bytes)
    {
      int code = b;
      // keycode only handles [A-Z] (which is ASCII decimal [65-90])
      if (code > 96 && code < 123) code = code - 32;
      robot.delay(40);
      robot.keyPress(code);
      robot.keyRelease(code);
    }
  }
}