如何使用水豚在两个框架之间切换 [英] How can I switch between two frames with Capybara

查看:57
本文介绍了如何使用水豚在两个框架之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在2帧内执行某些操作,但是每次尝试在两个帧之间切换时都会出现错误。
例如:

I am trying do something inside 2 frames but error raises everytime when I try to switch between frames. For example:

# encoding: utf-8

require "capybara/dsl"

Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'https://hb.posted.co.rs/posted'

class Account
  include Capybara::DSL

  def check_balance
    visit('/')
    page.driver.browser.switch_to.frame 'main'
    fill_in 'korisnik', :with => 'foo'
    fill_in 'lozinka', :with => 'bar'
    click_button 'Potvrda unosa'

    page.driver.browser.switch_to.frame 'header'
    click_on 'Stanje' 
  end
end

account = Account.new
account.check_balance

错误是:


[远程服务器]
file:/// tmp / webdriver-profile20120810-9163-xy6dtm / extensions / fxdriver @ googlecode.com / components / driver_component.js:6638:in
`unknown':无法找到框架:主
(Selenium :: WebDriver :: Error :: NoSuchFrameError)

[remote server] file:///tmp/webdriver-profile20120810-9163-xy6dtm/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:in `unknown': Unable to locate frame: main (Selenium::WebDriver::Error::NoSuchFrameError)

出了什么问题?

如果我更改了切换帧的顺序,请先尝试切换到页眉,然后再切换到主帧,然后出现相同的错误除了它说这次没有主帧:

If I change order of switching frames so try first to switch to 'header' then switch to 'main' frame then same error raises except that it says that this time there is no 'main' frame:

# encoding: utf-8

require "capybara/dsl"

Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'https://hb.posted.co.rs/posted'

class Account
  include Capybara::DSL

  def check_balance
    visit('/')
    page.driver.browser.switch_to.frame 'header'
    click_on 'Stanje' 

    page.driver.browser.switch_to.frame 'main'
    fill_in 'korisnik', :with => 'foo'
    fill_in 'lozinka', :with => 'bar'
    click_button 'Potvrda unosa'
  end
end

account = Account.new
account.check_balance

错误:


[远程服务器]
file:///tmp/webdriver-profile20120810-9247-w3o5hj/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:在
中'未知':无法找到框架:主
(Selenium :: WebDriver :: Error :: NoSuchFrameError)

[remote server] file:///tmp/webdriver-profile20120810-9247-w3o5hj/extensions/fxdriver@googlecode.com/components/driver_component.js:6638:in `unknown': Unable to locate frame: main (Selenium::WebDriver::Error::NoSuchFrameError)


推荐答案

问题

问题在于,当您执行 page.driver.browser.switch_to.frame ,它将页面的上下文切换到框架。现在,针对页面的所有操作实际上都针对框架。因此,当您第二次切换框架时,您实际上是在说主框架内的标题框架(而不是我想的是在主页内的标题框架)。

The problem is that when you do page.driver.browser.switch_to.frame, it switches the page's context to the frame. All actions against the page are now actually against the frame. So when you are switching frames the second time, you are actually saying find the 'header' frame inside the 'main' frame (rather than, what I assume you want, the 'header' frame inside the main page).

解决方案-水豚intra_frame(推荐):

在框架内工作时,应使用Capybara的 within_frame 方法。您可能想做:

When working inside a frame, you should use Capybara's within_frame method. You would want to do:

  def check_balance
    visit('/')

    within_frame('main'){
      fill_in 'korisnik', :with => 'foo'
      fill_in 'lozinka', :with => 'bar'
      click_button 'Potvrda unosa'
    }

    within_frame('header'){
      click_on 'Stanje' 
    }
  end

解决方案-硒switch_to:

如果您想自己进行框架管理(即不使用Capybara的内置方法),则可以将页面的上下文切换回浏览器,然后切换到第二个框架。如下所示。虽然我建议使用内置的Capybara方法。

If you want to do the frame management yourself (ie not use Capybara's built-in method), you can switch the page's context back to the browser and then to the second frame. This would look like the following. Though I would suggest using the built-in Capybara method.

  def check_balance
    visit('/')
    page.driver.browser.switch_to.frame 'header'
    click_on 'Stanje' 

    #Switch page context back to the main browser
    page.driver.browser.switch_to.default_content

    page.driver.browser.switch_to.frame 'main'
    fill_in 'korisnik', :with => 'foo'
    fill_in 'lozinka', :with => 'bar'
    click_button 'Potvrda unosa'
  end

这篇关于如何使用水豚在两个框架之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆