Cucumber and Capybara, clicking a non-link or button element

杀马特。学长 韩版系。学妹 提交于 2019-11-30 06:21:35

问题


I am trying to test an inplace editor using Cucumber/Capybara/Selenium stack, but my problem is that the editor is activated by clicking a div and not a link or button. I can not seem to figure out how to get Capybara to do this. Is there a way of doing this?


回答1:


You can click on an element via Capybara::Element.click. I add the following for this in my web_steps.rb to click on divs.

When /^(?:|I )click within "([^"]*)"$/ do |selector|
  find(selector).click
end

There is also Element.trigger('mouseover') which appears to enable hover albeit not working with Selenium.

It is also very likely you will need to decorate your feature/scenario with Capybara's provided @javascript tag.




回答2:


Besides being able to click on button elements like @Jim Mitchener explained, you can also click on a part of a text in the following way:

# WhenI click on the text "Sign in"
When(/^I click on text "(.*?)"$/) do |text|
  click_text(text)
end

def click_text(text)
  elem = find(:xpath, "//*[contains(translate(text(), '#{text.upcase}', '#{text.downcase}'), '#{text.downcase}')]", match: :first, wait: false)
  scroll_to(elem, -200)
  elem.click
end

This helper function does the same thing as find(selector).click, it finds the text element.

I found this article very good, it explains different types of steps you can write in cucumber.



来源:https://stackoverflow.com/questions/3585533/cucumber-and-capybara-clicking-a-non-link-or-button-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!