5

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">
Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152

4 Answers4

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.

Bryan Dimas
  • 1,389
  • 13
  • 18
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')
Nuri Hodges
  • 868
  • 6
  • 13
1

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
J Woods
  • 138
  • 1
  • 8
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
walidvb
  • 322
  • 1
  • 9
  • 1
    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. – Joshua Muheim Apr 09 '15 at 06:46
  • 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 break – walidvb Apr 09 '15 at 09:59
  • I have done this using binding.pry. It seems that the headless phantomjs browser doesn't support this? – Joshua Muheim Apr 09 '15 at 19:14