From 2ec507884a5329f758d1ff985920d81d789eba46 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 6 Sep 2019 15:03:41 -0500 Subject: [PATCH] Add Click On Text With Capybara as a ruby til --- README.md | 3 ++- ruby/click-on-text-with-capybara.md | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 ruby/click-on-text-with-capybara.md diff --git a/README.md b/README.md index bdc4ad7..b2adc64 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_842 TILs and counting..._ +_843 TILs and counting..._ --- @@ -629,6 +629,7 @@ _842 TILs and counting..._ - [Assoc For Hashes](ruby/assoc-for-hashes.md) - [Block Comments](ruby/block-comments.md) - [Chaining Multiple RSpec Change Matchers](ruby/chaining-multiple-rspec-change-matchers.md) +- [Click On Text With Capybara](ruby/click-on-text-with-capybara.md) - [Colorful Output With MiniTest](ruby/colorful-output-with-minitest.md) - [Comparing Class Hierarchy Relationships](ruby/comparing-class-hierarchy-relationships.md) - [Comparing Arrays In RSpec](ruby/comparing-arrays-in-rspec.md) diff --git a/ruby/click-on-text-with-capybara.md b/ruby/click-on-text-with-capybara.md new file mode 100644 index 0000000..4476f97 --- /dev/null +++ b/ruby/click-on-text-with-capybara.md @@ -0,0 +1,22 @@ +# Click On Text With Capybara + +Traditionally, web apps have clickable text in the form of links and buttons. +[Capybara's +`click_on`](https://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Actions#click_link_or_button-instance_method) +method is made for just this. + +```ruby +click_on("Home") # for Home +click_on("Submit") # for +``` + +A lot of more modern, JS-heavy apps stick click handlers on all kinds of +elements. If you want to test what happens when you click some text that is not +a link or button, `click_on` won't work. Instead, you'll need to find the +element and +[`click`](https://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#click-instance_method) +on it. + +```ruby +find("span", text: "Click Me").click +```