0

Iam trying to get list of everything that failed.ie catch error message if any one of the id is not found.Follwoing is a capybara automation script.

In the following code exception handling is working fine!

  puts "Verifying Home Page"
      def verifyHomepage(id, successMsg, errorMsg)
       begin   
        page.find(id)
        puts successMsg
       rescue
        puts errorMsg
       end
    end

   verifyHomepage("#logoAnchor", "logo anchor found", "Logo anchor not Found")
   verifyHomepage(".navbar-inner", "Header Bar found", "Header Bar  not Found")
   verifyHomepage(".unstyled", "All Occasions Frame found", "All Occasions Frame not Found")
   verifyHomepage("##easyPrintPromoBox", "Easy Print Frame", "Easy Print Frame not Found")
   verifyHomepage(".tabbable", "l3 Tabs  Featured Occasions/Pairings/ArtistEasy Print Frame", "3 Tabs  Featured Occasions/Pairings/ArtistEasy Print Frame Not found")
   verifyHomepage("##givingCardPromoBox", "Create the perfect gift found", "Create the perfect gift not Found")
   verifyHomepage(".footerr", "Footer Frame found", "Footer Frame found not Found")

Here iam repeatedly calling verifyHomePage method 7 times using different param values.

How is it possbile to pass the 3 params as array values instead of string values.what i meant is eg:

 verifyHomepage(idArray[],successMsg[],errorMsg[])
1
  • you don't specify the argument types in a function declaration, and if you want to pass a one-element array, that would be [sth] Commented May 16, 2013 at 4:50

2 Answers 2

2

You can make use of the splat operator to pass in a variable number of arguments to your method, and each argument can be a 3-element array.

def verifyHomepage(*args)
  args.each do |a|
    id = a[0]
    successMsg = a[1]
    errorMsg = a[2]
    # process args
  end
end

verifyHomepage(['.footerr', 'Footer Frame found', 'Footer Frame not found'], ['#giving', 'Create the perfect gift', 'not found'])
Sign up to request clarification or add additional context in comments.

Comments

1

What you suggest:

verifyHomepage(idArray[],successMsg[],errorMsg[])

is possible, but then you're maintaining three arrays that need to be kept in sync. An Array of Hashes is a much cleaner approach:

def verifyHomepage(items)
  items.each do |item|
    begin
      page.find(item[:selector])
      puts item[:successMsg]
    rescue
      puts item[:errorMsg]
    end
  end
end

items = [{ :selector => "#logoAnchor",
           :successMsg => "logo anchor found",
           :errorMsg => "Logo anchor not Found"},
         { :selector => ".navbar-inner",
           :successMsg => "Header Bar found",
           :errorMsg => "Header Bar  not Found"},
         { :selector => ".unstyled",
           :successMsg => "All Occasions Frame found",
           :errorMsg => "All Occasions Frame not Found"},
         { :selector => "##easyPrintPromoBox",
           :successMsg => "Easy Print Frame",
           :errorMsg => "Easy Print Frame not Found"},
         { :selector => ".tabbable",
           :successMsg => "l3 Tabs  Featured Occasions/Pairings/ArtistEasy Print Frame",
           :errorMsg => "3 Tabs  Featured Occasions/Pairings/ArtistEasy Print Frame Not found"},
         { :selector => "##givingCardPromoBox",
           :successMsg => "Create the perfect gift found",
           :errorMsg => "Create the perfect gift not Found"},
         { :selector => ".footerr",
           :successMsg => "Footer Frame found",
           :errorMsg => "Footer Frame found not Found"}]

puts "Verifying Home Page"
verifyHomepage(items)

1 Comment

and now for the bonus question, what is the most elegant way to accept a single item as well as an array of items without obliging the caller to wrap their item in [].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.