2012-08-10 20 views
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 करते हैं, यह बदल जाता है फ्रेम के लिए पृष्ठ का संदर्भ। पृष्ठ के खिलाफ सभी क्रियाएं वास्तव में फ्रेम के खिलाफ हैं। तो जब आप दूसरी बार फ्रेम स्विच कर रहे हैं, तो आप वास्तव में 'मुख्य' फ्रेम के अंदर 'हेडर' फ्रेम ढूंढ सकते हैं (बजाय, जो मैं मानता हूं, मुख्य पृष्ठ के अंदर 'हेडर' फ्रेम)।

समाधान - Capybara within_frame (अनुशंसित):

जब एक फ्रेम के अंदर काम कर रहा है, तो आप Capybara के within_frame विधि का उपयोग करना चाहिए।

आप फ्रेम प्रबंधन अपने आप को (यानी Capybara के अंतर्निहित विधि का उपयोग नहीं) क्या करना चाहते हैं, तो आप पृष्ठ के संदर्भ स्विच कर सकते हैं: -: आप क्या करना चाहते हैं सेलेनियम switch_to

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 

समाधान ब्राउज़र पर वापस और फिर दूसरी फ्रेम में। यह निम्नलिखित की तरह दिखेगा। हालांकि मैं अंतर्निहित कैपिबरा विधि का उपयोग करने का सुझाव दूंगा।

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 काम करता है के रूप में उम्मीद:

# 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 

मैं फ़ाइल https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rb में within_frame के लिए स्रोत कोड मिल गया है। लाइन से शुरू 81.

संपादित करें: जब मैं इस जवाब लिखा, @JustinKo सवाल का जवाब है, तो दोनों उत्तर सही लेकिन +1 और उसके लिए जवाब को स्वीकार कर लिया है।

संबंधित मुद्दे