Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a custom Capybara Selector with pure Ruby (No XPath/CSS)

Tags:

capybara

In the acceptance tests for my application, I need to match table cells based on column headers, taking into account the colspan attributes on both the headers and table cells. I want to write a custom matcher to do this, but it seems to me that XPath isn't cut out for the job. (Sure, maybe it's possible to do something like this in XPath, but it doesn't seem to me like such a monster XPath expression would be very easy to write, read, or maintain.)

So what I'd like to do is write a custom Capybara selector which doesn't rely on the xpath or css DSL methods defined in Capybara::Selector. Instead, I'd like to use pure Ruby code for this, as I believe Ruby is much better suited for this problem than XPath is. How would I go about doing this? (E.g. Could I create my own selector object and append it to Capybara::Selector.all, or something?)

(Cross-posted from the Capybara mailing list)


TL;DR:

How do I define my own Capybara matcher without being limited to the use of XPath or CSS? XPath is fine in most cases, but right now I need something with a bit more power.

like image 303
Ajedi32 Avatar asked Apr 24 '26 19:04

Ajedi32


1 Answers

For anyone looking at this in the future, my final solution was to just write a plain old Ruby class for parsing and processing HTML tables via Capybara's API. Unfortunately such a solution doesn't take advantage of the synchronization features of Capybara and doesn't blend in with Capybara's existing APIs, but it works well enough for me.

like image 121
Ajedi32 Avatar answered Apr 27 '26 22:04

Ajedi32