{"id":2588,"date":"2022-08-30T15:26:07","date_gmt":"2022-08-30T15:26:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/check-the-dimensions-of-an-img-with-rspec-capybara-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:07","modified_gmt":"2022-08-30T15:26:07","slug":"check-the-dimensions-of-an-img-with-rspec-capybara-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/check-the-dimensions-of-an-img-with-rspec-capybara-collection-of-common-programming-errors\/","title":{"rendered":"Check the dimensions of an img with RSpec\/Capybara?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to test the dimensions of an img tag without explicit &#8220;height&#8221; and &#8220;width&#8221; attributes, but I&#8217;m not sure how to do that exactly.<\/p>\n<pre><code>subject { page }\n\ndescribe \"profile page\" do\n  let(:user) { FactoryGirl.create(:user) }\n  before { visit user_path(user) }\n\n  describe \"gravatar image\" do\n    it \"should exist\" do\n      expect(page.first(\".gravatar\")).not_to be nil\n    end\n\n    it \"should be 50x50\" do\n      gravatar = page.first(\".gravatar\")\n      expect(gravatar[:height]).to eq(50)\n      expect(gravatar[:width]).to eq(50)\n    end\n  end\nend\n<\/code><\/pre>\n<p>The test for existence works, but the dimension test tells me that the attributes &#8220;height&#8221; and &#8220;width&#8221; are nil, which I can sort of understand because I never set them explicitly. But how do I get the actual height\/width of an image?<\/p>\n<ol>\n<li>\n<p>You can use a combination of <code>evaluate_script<\/code> and client side javascript to get that result:<\/p>\n<pre><code>page.evaluate_script(\"$('img')[0].clientHeight\")\n<\/code><\/pre>\n<p>This would require that your test is executed via a JS runtime like poltergeist etc.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 02:07:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to test the dimensions of an img tag without explicit &#8220;height&#8221; and &#8220;width&#8221; attributes, but I&#8217;m not sure how to do that exactly. subject { page } describe &#8220;profile page&#8221; do let(:user) { FactoryGirl.create(:user) } before { visit user_path(user) } describe &#8220;gravatar image&#8221; do it &#8220;should exist&#8221; do expect(page.first(&#8220;.gravatar&#8221;)).not_to be nil end it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[],"class_list":["post-2588","post","type-post","status-publish","format-standard","hentry","category-capybara","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2588"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2588\/revisions"}],"predecessor-version":[{"id":8744,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2588\/revisions\/8744"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}