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

    我正在嘗試在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,它切換頁面的上下文到框架。針對該頁面的所有操作現在實際上都是針對該框架的。所以當你第二次切換幀時,你實際上是在'main'幀中找到'header'幀(而不是我想要的,主頁中的'header'幀)。

    解決方案 - 水豚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:

    如果你想自己做(即不使用水豚內置的方法)的框架管理,可以切換頁面的上下文回到瀏覽器,然後到第二幀。這看起來像下面這樣。雖然我會建議使用內置的水豚法。

    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 2015-02-18 13:38:17

    2

    我找到了解決辦法。 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 
    

    我發現源代碼within_frame文件https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rb。從行開始81

    編輯: 雖然我寫了這個答案,@JustinKo回答的問題,所以這兩個答案都是正確的,但+1並接受替他回答。

    相關問題