Cleicy Guião
Cleicy Guião

Reputation: 111

How can I switch Frame using Capybara and Ruby?

I'm developing an atomation test using Capybara and Ruby. I need to switch frame to acess the webelement in the Report page, but I don't know how can I do it.

HTML code:

<iframe name="OF_jreport" id="OF_jreport" width="100%" height="100%" frameborder="0" border="0"></iframe>

I'm trying it:

def check_supplier_report()
            sleep 10

            @session.switch_to_frame("//*[@id=\"OF_jreport\"]")
            teste = @session.find("//*[@id=\"GTC_CODE\"]").text
            puts teste
end

But on console it is return the follow error:

You must provide a frame element, :parent, or :top when calling switch_to_frame (ArgumentError) ./features/helpers/commons.rb:158:in `check_supplier_report'

Can someone help me? Thanks

Upvotes: 2

Views: 665

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49880

You should prefer within_frame over switch_to_frame whenever possible. within_frame is higher level and ensures the system is left in a stable state. You should also consider preferring CSS over XPath when possible because it is quicker and simpler to read.

def check_supplier_report()
  sleep 10 # ??? Not sure why you have this

  @session.within_frame("OF_jreport") do
     teste = @session.find(:css, "#GTC_CODE").text
     puts teste
  end
end

You should really prefer within_frame over switch_to_frame whenever possible which would then be within_frame('OF_jreport') { ... do whatever in the frame }

Upvotes: 1

CEH
CEH

Reputation: 5909

Based on the error message, it looks like switch_to_frame wants the frame element passed as a parameter. I believe you need to find the frame before you can pass it into this method.

So, replace this line @session.switch_to_frame("//*[@id=\"OF_jreport\"]") with these two lines:

# Find the frame
frame = @session.find("//*[@id=\"OF_jreport\"]")

# Switch to the frame
@session.switch_to_frame(frame)

Upvotes: 2

Related Questions