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

4

6

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">
Merna answered 23/3, 2015 at 11:51 Comment(0)
D
2

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.

Drupe answered 7/1, 2017 at 2:28 Comment(0)
B
2

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
Boring answered 12/4, 2020 at 5:43 Comment(0)
B
1

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')
Bahaism answered 7/4, 2015 at 18:16 Comment(0)
V
0

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
Vested answered 7/4, 2015 at 20:39 Comment(3)
This sounds promising, but it doesn't seem to be available in my specs: Capybara::Poltergeist::JavascriptError: One or more errors were raised in the Javascript code on the page... and TypeError: null is not an object (evaluating 'document.querySelector('#jump_to_navigation').offsetTop'). When only executing page.evaluate_script("document.querySelector('#jump_to_navigation')") I get nil. In the browser (outside tests) it returns the wanted item.Merna
Does the element exist in the initial page load(ajax)? If not, you might want to wait until it is there. To make sure, have you tried break the execution of your test, and running that js directly in the browser, and in the console? PS: I use binding.pry to breakVested
I have done this using binding.pry. It seems that the headless phantomjs browser doesn't support this?Merna

© 2022 - 2024 — McMap. All rights reserved.