ruby
  • capybara
  • 2012-08-10 13 views 13 likes 
    13

    2つのフレームの中で何かしようとしていますが、フレームを切り替えようとするたびにエラーが発生します。たとえば :Capybaraで2つのフレームを切り替えるにはどうすればいいですか?

    # 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 
    

    エラーは次のとおりです。

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

    問題は何ですか?多分私はここで何か間違っていますか?

    私は「ヘッダ」に切り替えて最初はその後、「メイン」フレームに切り替えてみてくださいフレームの切り替えの順序を変更する場合は、同じエラーが、それは、この時間は何も「メイン」フレームが存在しないと言うことを除いて発生します

    # 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 
    

    エラー:

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

    答えて

    21

    問題

    問題は、あなたがpage.driver.browser.switch_to.frameを行うときに、それがスイッチということですページのコンテキストをフレームに追加します。ページに対するすべてのアクションは、実際にはフレームに対して行われます。だから、2回目にフレームを切り替えると、実際にはメインフレーム内に 'ヘッダ'フレームがあります。

    ソリューション - カピバラのwithin_frame(推奨):

    フレーム内部の作業をするとき、あなたはカピバラさんwithin_frameメソッドを使用する必要があります。あなたはしたいと思う:

    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:

    あなたは(つまり、カピバラの組み込みメソッドを使用していない)フレーム管理を自分で行いたい場合は、ページのコンテキストを切り替えることができますブラウザに戻り、次に2番目のフレームに戻ります。これは次のようになります。私はカピバラ(Capybara)法を使うことを提案している。

    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 
    
    +0

    すごく助けてくれました! – karthikeayan

    2

    解決策が見つかりました。 within_frameは期待通りに動作します。私は、ファイルhttps://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rbにwithin_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('/') 
    
        within_frame 'main' do 
         fill_in 'korisnik', :with => 'foo' 
         fill_in 'lozinka', :with => 'bar' 
         click_button 'Potvrda unosa' 
        end 
    
        within_frame 'header' do 
         click_on 'Stanje' 
        end 
        end 
    end 
    
    account = Account.new 
    account.check_balance 
    

    。 EDITラインから81

    を開始:私はこの答えを書いている間 、@JustinKoは質問に答え、その両方の答えは正しいですが、1、彼のための答えを受け入れました。

    関連する問題