cpcaug
Last Updated: February 25, 2016
·
2.385K
· akalyaev
D83b63db79b42978fe35e120550ca180

Capybara & Poltergeist: get element's position or size

Getting element's position or size is not that simple in Capybara and Poltergeist. You will need to evaluate some JS to archive what you want.

getting position:

pos = page.driver.evaluate_script <<-EOS
  function() {
    var ele  = document.getElementById('someId');
    var rect = ele.getBoundingClientRect();
    return [rect.left, rect.top];
  }();
EOS

getting size:

size = page.driver.evaluate_script <<-EOS
  function() {
    var ele  = document.getElementById('someId');
    var rect = ele.getBoundingClientRect();
    return [rect.width, rect.height];
  }();
EOS

These samples could be easily combined if you want both pos and size.

Say Thanks
Respond

2 Responses
Add your response

16880
None

Using your methods above to getting an element's size, how would you go about asserting that the size has changed after an AJAX event?

over 1 year ago ·
16882
D83b63db79b42978fe35e120550ca180

@tristanm

old_size = get_element_size("#el")
# call action which triggers AJAX event
# wait for AJAX to finish
new_size = get_element_size("#el")
assert new_size != old_size
over 1 year ago ·