cpcaug
Last Updated: February 25, 2016
·
2.785K
· akalyaev

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.

2 Responses
Add your response

16880

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

@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 ·