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.
Written by Anton Kalyaev
Related protips
2 Responses
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
·
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Poltergeist
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#