2012-12-13 10 views
26

वेब पेज पर उपयोगकर्ता "बटन" और "बटन के रूप में स्टाइल लिंक" के बीच अंतर नहीं करते हैं। क्या यह जांचने का कोई तरीका है कि पृष्ठ पर "बटन या लिंक" मौजूद है या नहीं?बटन या लिंक की उपस्थिति के लिए कैपिबरा मैचर

page.should have_button('Click me') 

जो बटन के रूप में स्टाइल लिंक नहीं मिल रहा है:

उदाहरण के लिए Capybara कदम है।

+0

मैं जावास्क्रिप्ट लिंक के कारण ऐसा करने जा रहा था, लेकिन फिर मैंने यह नहीं करने का फैसला किया: इस मामले में कम से कम, यह * अंतर करना संभव है। उदाहरण के लिए, मध्य पर एक नया टैब खुलता है, और होवर पर लिंक गंतव्य केवल लिंक के लिए दिखाया जाता है। सबसे अच्छा अभ्यास है: उस मामले में लिंक का उपयोग न करें, और परीक्षणों में उन्हें अनुमति न दें। –

+1

एक मामला है हालांकि यह इसके लायक है: अस्वीकृति। अन्यथा, यदि आप बटनों में लिंक बदलते हैं तो परीक्षण चुपचाप हमेशा गुजरते हैं। –

उत्तर

49

मैं पहले से ही पता चला बेहतर जवाब:

page.should have_selector(:link_or_button, 'Click me') 

click_link_or_button से इसके बाद जो यहां परिभाषित किया गया है: https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/actions.rb#L12

def click_link_or_button(locator) 
    find(:link_or_button, locator).click 
end 
alias_method :click_on, :click_link_or_button 

यह एक चयनकर्ता कॉल :link_or_button । इस चयनकर्ता यहां परिभाषित किया गया है: https://github.com/jnicklas/capybara/blob/master/lib/capybara/selector.rb#L143

Capybara.add_selector(:link_or_button) do 
    label "link or button" 
    xpath { |locator| XPath::HTML.link_or_button(locator) } 
end 

यह इस प्रणाली को बुलाती है: http://rdoc.info/github/jnicklas/xpath/XPath/HTML#link_or_button-instance_method

# File 'lib/xpath/html.rb', line 33 

def link_or_button(locator) 
    link(locator) + button(locator) 
end 

तो मैं चयनकर्ता की उपस्थिति की जांच करने की कोशिश की और यह काम किया:

page.should have_selector(:link_or_button, 'Click me') 
0

मुझे लगता है कि आप खोज बटन उदाहरण विधि का उपयोग कर सकते हैं:

(Capybara::Element) find_button(locator) 

आईडी, नाम, मूल्य का उपयोग करना।

या आप एक लिंक

(Capybara::Element) find_link(locator) 

से चाहते हैं: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders#find_button-instance_method

+0

मैं इसे 2 – mirelon

+0

की बजाय एक कॉल के साथ ढूंढना चाहता था ऐसा लगता है कि आपके पास एक अच्छा तरीका है :) – simonmorley

1

व्यक्तिगत तौर पर मैं अपने बटन दे या एक आईडी से लिंक और

page.should have_css('#foo')

इस तरह का उपयोग कर उस के लिए विचार करेंगे आप इसके कार्यान्वयन के बारे में चिंता किए बिना लिंक या बटन का उल्लेख कर सकते हैं।

मैं हमेशा इस उपयोगी पाते हैं: https://gist.github.com/428105

1

आप कर सकते हैं कस्टम मैचर

RSpec::Matchers::define :have_link_or_button do |text| 
    match do |page| 
    Capybara.string(page.body).has_selector?(:link_or_button, text: text) 
    end 
end 
का भी उपयोग करें

फिर

expect(page).to have_link_or_button('Login') 
0

मैं एक अजीब मामले में जहां कुछ धुआं परीक्षण विभिन्न ग्राहक केंद्रित प्रवेश पृष्ठों जिसे प्रवेश कर रही पर मामूली बदलाव किया था सबमिट बटन ... उपयोगकर्ता की एक ककड़ी मेज से प्रेरित भर में मार्च किया था करते हैं, ऑर्ग , आदि।

# A bit of a hack, org_name is normally a subdomain, but sometimes it is the complete domain 
def login(user, org_name) 
    # Use the below to automatically hit each user's org's server 
    if org_name.include? '.com' 
    Capybara.app_host = "http://#{org_name}" 
    else 
    Capybara.app_host = "http://#{org_name}.mydomain.com" 
    end 

    visit '/' 
    fill_in 'username', :with => user 
    fill_in 'userpwd', :with => '***' 
    begin 
    page.find(:link_or_button, 'submit') 
    click_on 'submit' 
    rescue Capybara::ElementNotFound 
    page.find(:link_or_button, 'Log In') 
    click_on 'Log In' 
    rescue Capybara::ElementNotFound 
     pending "Need to determine how to invoke the Login button for #{org_name} near Line ##{__LINE__} of #{__method__} in #{__FILE__} " 
    end 

    # ----------------------- 
    # Work-around for modal popup saying SSL is mismatched if you are using actual production URLs 
    # The rescue is for cases that do not exhibit the modal pop-up 
    page.driver.browser.switch_to.alert.accept rescue Selenium::WebDriver::Error::NoAlertPresentError 

    # Ensure that login was successful 
    page.should_not have_content 'Login failed' 
end 
1

उम्मीद वाक्य रचना

expect(page).to have_selector(:link_or_button, 'Click me') 

यह एक कस्टम मिलान को परिभाषित करने की जरूरत के बिना काम करता है का उपयोग करना।

+0

^यह। चाहिए /: वाक्यविन्यास को हटा दिया जाना चाहिए –