Joshua Muheim
Joshua Muheim

Reputation: 13195

RSpec and Capybara: How to get the horizontal and vertical position of an element

I have added visually hidden jump links to my website, that appear when they are focused (similar to e.g. GitHub, just press Tab once on the home page).

I want to test this behaviour with Capybara. I can't check for e.g. 'visibility: true/false', because the links are not really hidden (otherwise screenreaders would't see them), but only moved out from the viewport by absolute positioning. When they are focused, they are placed at their original position in the viewport.

So I guess I have to check the X and Y coordinate, but Capybara doesn't seem to offer a native method for getting them? Is this true? And if so, how would I get them for e.g. an element '#jump_to_content'? By executing some JavaScript?

Update

I found some inspiration here: https://content.pivotal.io/blog/testing-accessibility-with-rspec-and-capybara

But this doesn't seem to work for my configration:

link.native.location
NoMethodError: undefined method `location' for #<Capybara::Poltergeist::Node tag="a">

Upvotes: 6

Views: 3942

Answers (4)

J Woods
J Woods

Reputation: 168

According to this SO answer, the best way to use JavaScript to find the position of an element is to use getBoundingClientRect(); as it returns values that are relative to the viewport.

It feels more readable to me to find the element with Capybara instead of JavaScript, and then find its position. I'm using Selenium and Chrome.

link = find("a", text: "content")

link.evaluate_script("this.getBoundingClientRect().left;")
link.evaluate_script("this.getBoundingClientRect().right;")
link.evaluate_script("this.getBoundingClientRect().top;")
link.evaluate_script("this.getBoundingClientRect().bottom;")

Some browsers (like Chrome) also have:

link.evaluate_script("this.getBoundingClientRect().x;")
link.evaluate_script("this.getBoundingClientRect().y;")
link.evaluate_script("this.getBoundingClientRect().height;")
link.evaluate_script("this.getBoundingClientRect().width;")

You can also do

link.rect.y
link.rect.height
link.rect.x
link.rect.width

Upvotes: 2

Bryan Dimas
Bryan Dimas

Reputation: 1452

You are correct, Capybara does not offer a native method for getting an element's position in the page, but if you have access to jQuery you can use Capybara's page.evaluate_script method to easily determine an element's position. It would look something like this:

page.evaluate_script("$('.menu > div:nth-child(1)').offset().top")

The .offset() method allows us to retrieve the current position of an element relative to the document. Contrast this with .position(), which retrieves the current position relative to the offset parent

Just note that

While it is possible to get the coordinates of elements with visibility:hidden set, display:none is excluded from the rendering tree and thus has a position that is undefined.

Upvotes: 2

walidvb
walidvb

Reputation: 322

As per Nuri's answer, the best way you can go is asking the browser directly(headless browsers don't read css files, afaik).

However, rather than asking the style of the element, you might want to directly check its position by using offsetTop. So something like

expect(page.evaluate_script("document.querySelector('a#jump-to-content').offsetTop;")).to eq('-8000px')

If you need more control, you could also run it all in javascript:

expect(page.evaluate_script("document.querySelector('a#jump-to-content').offsetTop < document.querySelector('body').offsetTop;").to be_true

Upvotes: 0

Nuri Hodges
Nuri Hodges

Reputation: 888

Using capybara's page.evaluate_script will allow you to execute some JavaScript and create an assertion off the return value:

expect(page.evaluate_script("document.querySelectorAll('a#jump-to-content')[0].style.top;")).to eq('-8000px')

Upvotes: 1

Related Questions