Tuesday, 23 December 2014

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

No comments:

Post a Comment