ping-ot
ping-ot

Reputation: 123

If else not working

I am having trouble with this one, I try to change the conditions and add other options but still I got the same error.. can someone help me? Thanks a lot!

this is my codes:

require 'watir-webdriver'
require 'minitest/autorun'
require "win32ole"

class Login < Minitest::Unit::TestCase

    $number_of_fails = 0
    $number_of_success = 0

    $fields = ["first_name", "middle_name", "last_name", "sss_number", "tin_number"]
    # $to_enter = ["Helfe", "padayao", "Marquez", "1234rer", "9387373"]

    def test_in()
        @browser =Watir::Browser.new :firefox
        @browser.goto 'http://gw01.nextix.org/login'
        accept_next_alert=true
        @browser.driver.manage.window.maximize

        excel= WIN32OLE::new("excel.Application")
        wrkbook=excel.Workbooks.Open("C:\\testing\\inputs.xlsx")
        wrksheet = wrkbook.worksheets(1)
        wrksheet.select

        rows = 2
        while rows <= 5
            $username = wrksheet.cells(rows, "A").value
            $password = wrksheet.cells(rows, "B").value

            @browser.text_field(:name, "username").set($username)
            sleep 3
            @browser.text_field(:name, "password").set($password)
            sleep 3
            @browser.button(:name => 'login').click
            sleep 3

            rows = rows + 1
        end

        $Dashboard = @browser.link(:text, "Dashboard")
        $Dashboard.exists?
        $Dashboard.click
        @browser.link(:text, "Users").click
        @browser.button(:value,"Add New User").click

        rows = 8
        while rows <= 13
            $fname = wrksheet.cells(rows, "A").value
            $mname = wrksheet.cells(rows, "B").value
            $lname = wrksheet.cells(rows, "C").value
            $sss = wrksheet.cells(rows, "D").value
            $tin = wrksheet.cells(rows, "E").value

            @browser.text_field(:id, $fields[0]).set($fname)
            sleep 5
            @browser.text_field(:id, $fields[1]).set($mname)
            sleep 5
            @browser.text_field(:id, $fields[2]).set($lname)
            sleep 5
            @browser.text_field(:id, $fields[3]).set($sss)
            sleep 5
            @browser.text_field(:id, $fields[4]).set($tin)
            @browser.send_keys :tab

            rows += 1

            for i in 0..4
                if @browser.text_field(:id => $fields[i], :aria_invalid => "false")
                    $number_of_success = $number_of_success + 1
                else
                    $number_of_fails = $number_of_fails + 1
                end
            end
        end

        puts "Number of Success: #{$number_of_success}"
        puts "Number of Failures: #{$number_of_fails}"

    end
end

The result:

User1@DOCUMENTATIONS /c/testing
$ ruby revised_login.rb
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
  c:/Ruby193/lib/ruby/1.9.1/minitest/autorun.rb:14:in `<top (required)>'
  revised_login.rb:2:in `<main>'
MiniTest::Unit.autorun is now Minitest.autorun. From c:/Ruby193/lib/ruby/1.9.1/m
initest/autorun.rb:18:in `<top (required)>'
MiniTest::Unit::TestCase is now Minitest::Test. From revised_login.rb:5:in `<mai
n>'
Run options: --seed 13926

# Running:

Number of Success: 30
Number of Failures: 0

.

Finished in 287.038568s, 0.0035 runs/s, 0.0000 assertions/s.

1 runs, 0 assertions, 0 failures, 0 errors, 0 skips

User1@DOCUMENTATIONS /c/testing $

Upvotes: 0

Views: 128

Answers (2)

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

The ruby IF statement expects you to follow it with something that will return True or False. If you do not do that, If is going to 'punt' and treat anything returned that is not null, zero, or 'false' as TRUE. The method you are invoking is going to return a handle to an object not true or false and thus IF is going to treat that as 'true'

to prove this, use irb to create a watir browser object, navigate to a web page, and then try the following (note that the page I am on has no text field that matched the selection criteria)

irb(main):009:0> puts b.text_field(:aria_invalid => 'false')
#<Watir::TextField:0x00000001f042a8>
=> nil
irb(main):010:0> puts b.text_field(:aria_invalid => 'false').exists?
false
irb(main):011:0> puts "true" if b.text_field(:aria_invalid => 'false')
true
=> nil
irb(main):012:0> puts "false" unless b.text_field(:aria_invalid => 'false').exists?
false
=> nil
irb(main):016:0> puts b.text_field(:aria_invalid => 'false').exists? ? "true" : "false"
false
=> nil

you can see that the IF treats the (effectively empty) object being returned as 'true' even when the object does not exist in the dom. You need to add the .exists? method, then we get back a true or a false, and you can see that now the conditional responds as we would expect given the existence or non-existence of the object.

Upvotes: 0

eunice
eunice

Reputation: 85

Try to add this methods in your condition..

  • exists? – Returns whether this element actually exists.
  • present? – Returns true if the element exists and is visible on the page
  • visible? – If any parent element isn’t visible then we cannot write to the element. The only reliable way to determine this is to iterate up the DOM element tree checking every element to make sure it’s visible.

Upvotes: 2

Related Questions