Tuesday, 23 December 2014

xpaths

First Row in a table:FIrst row is having class=odd

     $x("//tr[1][@class='odd']")

hyperlink in first row of a table:

     "//tr[1][@class='odd']/td[2]/a"  or    
     $x("//table/tbody/tr[1][@class='odd']/td[2]/a")    whole hierarchy.. test it in firebug console 

WebTable _selenium webdriver using Ruby code so far developed.. Minispec used to validate text

require_relative '../../lib/Element.rb'
require_relative '../../lib/Link.rb'
require_relative '../../lib/TextBox.rb'
require_relative '../../lib/ComboBox.rb'
require_relative '../../../features/util/ExpectedElements'
require 'selenium-webdriver'
require 'rspec'
require 'rspec/expectations'


class OrdersMainPage
# Initializing the page object for Login page
  def initialize
    # Declaring page objects
    @linkAllOrders= Link.new(:css, "#orders-list-all")
    @linkReserved= Link.new(:css, "#orders-all-reserved")
    @linkCancelled= Link.new(:css, "#orders-all-cancelled")
    @linkApproved= Link.new(:css, "#orders-all-approved")
    @linkUnfulfilled= Link.new(:css, "#orders-all-unfulfilled")
    @linkShipping= Link.new(:css, "#orders-all-shipping")
    @linkReturnStatus= Element.new(:xpath, "//span[contains(text(), 'Orders by Return Status')]")
    @linkNone= Link.new(:css, "#orders-all-none")
    @linkPartReturned= Link.new(:css, "#orders-all-partreturned")
    @linkReturned= Link.new(:css, "#orders-all-returned")

    #Middle Pane Orders Page
    @linkStatusFilter= ComboBox.new(:id, "status")
    @linkChannels= Link.new(:css, "#btn btn-default dropdown-toggle")
    @linkDateFilter= Element.new(:css, "div#reports-daterangepicker")
    @linkSearch= Element.new(:css, "i[class='fa fa-search']:first-child")
    @linkProducts= Link.new(:css, "a[data-test='menuHeaderCatalogue'")

    @tableOrders=Element.new(:xpath, "//table[@id='DataTables_Table_0']")
    @elementRightNavigate=Element.new(:css, "i[class='fa fa-long-arrow-right']")

    # @defaultSearchFields = ExpectedElements.new(@allOrders, @reserved, @cancelled)
  end

  def getOrderData(order_id)
    #"//a[contains(text(), '3466')]"
    puts "get order data function inside #{order_id}"
    @order_row=nil
    iterateTable=1
    iRow=1

    #if an order doesn't exits then navigate to next page. iterate 3 times
    while iterateTable<=5
      #check for the order link in the page
      if existsElement(:xpath, "//a[contains(text(), '" + order_id + "')]")
        break
      else
        @elementRightNavigate.clickObj
        iterateTable=+1
      end
    end

    begin
      #get row count
      table=$browser.find_element(:xpath, "//table[@id='DataTables_Table_0']/tbody")
      iRows= table.find_elements(:tag_name, "tr")
      rowSize=iRows.length
      # puts   "row size is : #{rowSize}"

      #get row with cell text
      rows=$browser.find_elements(:xpath, "//table[@id='DataTables_Table_0']/tbody/tr")
      rows.each do |rw|
        if rw.text.include?(order_id.to_s)
          @order_row=$browser.find_element(:xpath, "//table[@id='DataTables_Table_0']/tbody/tr[" + iRow.to_s + "]")
          break
        end
        iRow=iRow+1
      end

      # get Cells data into an array
      unless @order_row.nil?
        a= Array.new(4)
        cells= @order_row.find_elements(:tag_name, "td")
        @cellData = Array.new(cells.length-1)
        i=0
        cells.each do |cell|
          # puts cell.text
          @cellData[i]= cell.text
          i=i+1
        end
      end

    rescue Exception => e
      e.message
    end
    @cellData
  end

  def  OrdersPageValidation
    begin
      @linkAllOrders.getText.must_include 'All Orders'
      @linkReserved.getText.must_include 'Reserved'
      @linkCancelled.getText.must_include 'Cancelled'
      @linkApproved.getText.must_include 'Approved'
      @linkUnfulfilled.getText.must_include 'Awaiting'
      @linkShipping.getText.must_include 'Shipping'

      @linkReturnStatus.getText.must_include 'Orders by Return Status'
      @linkNone.getText.must_include 'None'
      @linkPartReturned.getText.must_include 'Part Returned'
      @linkReturned.getText.must_include 'Returned'
    rescue Exception => e
        e.message
    end
  end

  def orderDataValidation(orderData, table)
    begin table.hashes.each do |hash|
      #rspec validations
      orderData.to_s.must_include (hash['order_id'].to_s)
      orderData.to_s.must_include (hash['retailer_order_id'].to_s)
      orderData.to_s.must_include (hash['Channel'].to_s)
      orderData.to_s.must_include (hash['customer_name'].to_s)
      orderData.to_s.must_include (hash['quantity'].to_s)
      orderData.to_s.must_include (hash['price_paid'].to_s)
      orderData.to_s.must_include (hash['status'].to_s)
      end
    end
  end


  def existsElement(by, value)      #use elementExist instead..
    flag=false
    begin
      if $browser.find_element(by, value).displayed?
        flag=true
      end
    rescue Exception
    ensure
      return flag
    end
  end

  #get first order item row data into an array
  def getFirstOrderData()
    @firstRow=$browser.find_element(:xpath, "//table[@id='DataTables_Table_0']/tbody/tr[1]")
    cells= @firstRow.find_elements(:tag_name, "td")
    cellData = Array.new(cells.length-1)
    i=0
    cells.each do |cell|
      # puts cell.text
      cellData[i]= cell.text.to_s.strip
      i=i+1
    end
    cellData
  end
  #get the first order data and validate its status based on filter criteria
  def ordersFilterByStatus(table)
    begin table.hashes.each do |hash|
      @status= hash['status'].to_s.downcase.strip
      @linkStatusFilter.selectByText(hash['status'].to_s)
      sleep 3
      #get the first row order item data and compare with the parameter..
      cellData= getFirstOrderData
      unless @status.include?('return') || @status.include?('none')
        puts cellData[7]
        cellData[7].to_s.downcase.strip.must_equal (@status)     #check for Status column
      else
        puts cellData[8]
        cellData[8].to_s.downcase.strip.must_equal (@status)  #check for Return Status column
      end
      end
    end
  end


end       #end of the class

Monday, 22 December 2014

WebTable getRowwithText, rowcount, getcell data in selenium webdriver using ruby

 def getOrderData(order_id)
    #"//a[contains(text(), '3466')]"
    puts "get order data function inside #{order_id}"
    @order_row=nil
    iterateTable=1
    iRow=1

    #if an order doesn't exits then navigate to next page. iterate 3 times
    while iterateTable<=5
      #check for the order link in the page
      if existsElement(:xpath, "//a[contains(text(), '" + order_id + "')]")
        break
      else
        @elementRightNavigate.clickObj
        iterateTable=+1
      end
    end

    begin
      #get row count
      table=$browser.find_element(:xpath, "//table[@id='DataTables_Table_0']/tbody")
      iRows= table.find_elements(:tag_name, "tr")
      rowSize=iRows.length
      # puts   "row size is : #{rowSize}"

      #get row with cell text
      rows=$browser.find_elements(:xpath, "//table[@id='DataTables_Table_0']/tbody/tr")
      rows.each do |rw|
        if rw.text.include?(order_id.to_s)
          @order_row=$browser.find_element(:xpath, "//table[@id='DataTables_Table_0']/tbody/tr[" + iRow.to_s + "]")
          break
        end
        iRow=iRow+1
      end

      # get Cells data into an array
      unless @order_row.nil?
        a= Array.new(4)
        cells= @order_row.find_elements(:tag_name, "td")
        @cellData = Array.new(cells.length-1)
        i=0
        cells.each do |cell|
          puts cell.text
          @cellData[i]= cell.text
          i=i+1
        end
      end

    rescue Exception => e
      e.message
    end
    @cellData
  end


def existsElement(by, value)      #use elementExist instead..
    flag=false
    begin
      if $browser.find_element(by, value).displayed?
        flag=true
      end
    rescue Exception
    ensure
      return flag
    end
  end

Saturday, 13 December 2014

Ruby Cucumber - table data and parametrization

Scenario

 @test Scenario Outline: Testing for table and data
  Given I login with data <credentials>
  Then I test with data
    |first_name|last_name|middle_name|dob          |mobile_no |    |vicky     |G     |venu       |XXXXXXXX   |99xxxxxxxx    |  And I log out successfully

  Examples:  |credentials         |  |"myusername,mypassword"|

Step Definition:

Given(/^I login with data "([^"]*)"$/) do |credentials|
  puts credentials.class
  if credentials.include?(',')
    @arr_data=credentials.split(',')
    puts 'username is: '+ @arr_data[0]
    puts 'password is: '+ @arr_data[1]
  endend

Then(/^I test with data$/) do |table|
  begin  table.hashes.each do |hash|
     puts "first_name: #{hash['first_name']}"     puts "last_name: #{hash['last_name']}"     puts "middle_name: #{hash['middle_name']}"     puts "dob: #{hash['dob']}"     puts "mobile_no: #{hash['mobile_no']}"  end  rescue Exception=> e    puts e.message
  end
end
And(/^I log out successfully$/) do  pass
end

+++++++++++++++++Output++++++++++++++++++++

String

username is: myusername
password is: mypassword

first_name: vicky
last_name: gopi
middle_name: venu
dob: 09021986
mobile_no: 99xxxxxxxx

Ruby and Cucumber Basic example- Basic input, table and data

Feature: Cucumber basics using Ruby

@portalScenario: Sum example
   Given I add "4+8"
   And Sum should be "6"

  @test  Scenario Outline: Sum example for multiple values
   Given I add for <vals>
   And Sum should be <answer> for <vals>

Examples:   
|vals |answer|   
|"2+4"|"6"  |   
|"4+4"|"9"  |   
|"4+5"|"9"  |   
|"4+0"|"199"|

@test   
Scenario: Testing for multiple data at each step
Given I validate product feed for the details
    |username     |vickyvenu81@gmal.com  |      
    |profile       |venu                 |      
    |emp_id         |739                 |      
    |project        |bankofamerica       |     
And print pass or fail
    |col1       |col2  |     
    |data1   |data2 |     
    |data3   |data4 |
     

++++++++++++++++Step Definitions+++++++++++++++++++++++++++
Given(/^I add "([^"]*)"$/) do |arg|
  @sum= eval(arg)
end
And(/^Sum should be "([^"]*)"$/) do |arg|
  puts @sum  if @sum.to_i != 6    puts "fail"  else    puts "pass"  endend
Given(/^I add for "([^"]*)"$/) do |vals|
  @sum= eval(vals)
end
And(/^Sum should be "([^"]*)" for "([^"]*)"$/) do |answer, vals|
  puts 'sum = '+ @sum.to_s
  if @sum!=answer.to_i
    puts 'error in evaluating sum and sum should be: '+ @sum.to_s
  endend

Given(/^I validate product feed for the details$/) do |table|
  # table is a table.hashes.keys # => [:username, :password, :profile, :logout, :items_count]  puts '+++++++++++++++++++++++++++++++++++++++++++'  data = table.rows_hash
  puts data['emp_id']   #Here we are accesing row wise.. |property| value|  this will print 739end
And(/^print pass or fail$/) do |table|
  puts "table.class:-  #{table.class}"  puts 'table inspection:'  puts table.inspect

  table.hashes.each do |hash|
    puts "col1 data should be printed: #{hash['col1']}"    puts "col2 data should be printed: #{hash['col2']}"  end
  puts 'table.raw example: '  data= table.raw
  puts data[0]

end

+++++++++++++++++++++Output++++++++++++++++++++++++++++++++
C:\Ruby193\bin\ruby.exe -EUTF-8 -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\Ruby193\bin/cucumber C:/RnD/features/gmail.feature --format Teamcity::Cucumber::Formatter --expand --color -r features
Testing started at 20:17 ...
Tag: @@portal

12

fail
Tag: @@test

Given I add for <vals>                          # features/step_definitions/gmailtest.rb:14

And Sum should be <answer> for <vals>           # features/step_definitions/gmailtest.rb:18

sum = 6

sum = 8

error in evaluating sum and sum should be: 8

sum = 9

sum = 4

error in evaluating sum and sum should be: 4
Tag: @@test

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

739

table.class:-  Cucumber::Ast::Table

table inspection:


  |     col1  |     col2  |
  |     data1 |     data2 |
  |     data3 |     data4 |

col1 data should be printed: data1

col2 data should be printed: data2

col1 data should be printed: data3

col2 data should be printed: data4

table.raw example: 

["col1", "col2"]
6 scenarios (6 passed)
12 steps (12 passed)
0m0.181s

Process finished with exit code 0

+++++++++++++++++++++++Explanation++++++++++++++++++++++++

# http://www.ruby-doc.org/gems/docs/d/davidtrogers-cucumber-0.6.2/Cucumber/Ast/Table.html# Step Definitions that match a plain text Step with a multiline argument table will receive it as an instance of Table.# A Table object holds the data of a table parsed from a feature file and lets you access and manipulate the data in different ways.#                                                                                                                                  Given I have:# | a | b |# | c | d |# This will store [['a', 'b'], ['c', 'd']] in the data variable.## hashes()# Converts this table into an Array of Hash where the keys of each Hash are the headers in the table.# For example, a Table built from the following plain text:## | a | b | sum |# | 2 | 3 | 5   |# | 7 | 9 | 16  |# Gets converted into the following:## [{'a' => '2', 'b' => '3', 'sum' => '5'}, {'a' => '7', 'b' => '9', 'sum' => '16'}]## raw: Gets the raw data of this table. For example, a Table built from the following plain text:# rows_hash: Converts this table into a Hash where the first column is used as keys and the second column is used as values## | a | 2 |# | b | 3 |# Gets converted into the following:# {'a' => '2', 'b' => '3'}

Tuesday, 2 December 2014

PWatir page object model links

All the questions and answers related to pom :
https://github.com/cheezy/page-object/issues/255  for a table.. good one


issues related to page objects and answers:
https://github.com/cheezy/page-object/issues

really good one..



http://www.cheezyworld.com/2011/07/29/introducing-page-object-gem/

https://github.com/watir/watir-webdriver/wiki/Page-Objects