{"id":71412,"date":"2020-01-10T11:30:26","date_gmt":"2020-01-10T06:00:26","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=71412"},"modified":"2024-04-12T14:13:41","modified_gmt":"2024-04-12T08:43:41","slug":"integration-testing-4","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/","title":{"rendered":"Integration Testing"},"content":{"rendered":"<h1><strong>Learning Integration Testing<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg\" alt=\"Integration Testing\" width=\"569\" height=\"316\" \/><\/p>\n<p>Let&#8217;s learn Integration Testing. Integration tests are critically important because they exercise your application just like a real user. Therefore, depending on the full stack from your models up through your controllers, helpers, view templates, web server, database, and middleware.<\/p>\n<p>Integration tests can be brittle if they know too much about how those components work. Proper integration tests use your application as a black box. They should know as little as possible about what happens under the hood, they\u2019re just there to interact with the interface and observe the results.<\/p>\n<h2><strong>rack-test: Integration Testing<\/strong><\/h2>\n<p>First, a little background.<\/p>\n<p>Integration tests in Rails applications work by simulating a user\u2019s experience through the HTML interface. You load up your Rails application, and your integration test harness (such as Capybara) simulates a user clicking around on the site.<\/p>\n<p>Without JavaScript, Capybara uses rack-test as a driver by default and looks something like this:<\/p>\n<ul>\n<li>Firstly, You trigger user action by invoking one of Capybara\u2019s DSL methods, such as visit.<\/li>\n<li>Secondly, Capybara tells its driver (rack-test) to load the requested URL.<\/li>\n<li>Thirdly, The Rack response is parsed by rack-test and saved as the current page.<\/li>\n<\/ul>\n<h2><strong>Selenium, capybara-webkit, and Poltergeist: Integration Testing<\/strong><\/h2>\n<p>JavaScript drivers work a little differently. Because browser tools like WebKit are difficult to load inside of a Ruby process, these drivers boot up an external process which can interact with the browser engine. Because the external process doesn\u2019t have access to the in-memory instance of your Rails application, they must make actual HTTP requests.<\/p>\n<p>Interactions performed using Capybara\u2019s JavaScript drivers look something like this:<\/p>\n<ul>\n<li>You trigger user action by invoking one of Capybara\u2019s DSL methods, such as visit.<\/li>\n<li>Capybara tells its driver (such as capybara-webkit) to load the requested URL.<\/li>\n<li>The driver starts its external process to hold the browser engine.<\/li>\n<li>In order to serve actual HTTP requests, Capybara boots a new instance of your Rails application in a background thread.<\/li>\n<\/ul>\n<h2><strong>Capybara to the Rescue: Integration Testing<\/strong><\/h2>\n<p>Much of Capybara\u2019s source code is dedicated to battling this asynchronous problem. Capybara is smart enough to understand that a page may not have loaded by the time you try to interact with it. A typical interaction might look like this:<\/p>\n<table>\n<thead>\n<tr>\n<td width=\"319\"><strong>Test Thread <\/strong><\/td>\n<td width=\"319\"><strong>Application Thread<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"319\">Your test invokes visit.<\/td>\n<td width=\"319\">Waiting for a request.<\/td>\n<\/tr>\n<tr>\n<td width=\"319\">Capybara tells the driver to load the page.<\/td>\n<td width=\"319\">Waiting for a request.<\/td>\n<\/tr>\n<tr>\n<td width=\"319\">The driver performs a request.<\/td>\n<td width=\"319\">Waiting for a request.<\/td>\n<\/tr>\n<tr>\n<td width=\"319\">Your test invokes click_link.<\/td>\n<td width=\"319\">Your application receives the request.<\/td>\n<\/tr>\n<tr>\n<td width=\"319\">Capybara looks for the link on the page, but it isn\u2019t there.<\/td>\n<td width=\"319\">Your application sends a response.<\/td>\n<\/tr>\n<tr>\n<td width=\"319\">Capybara tries to find the element again, but it\u2019s not there.<\/td>\n<td width=\"319\">The driver receives the response.<\/td>\n<\/tr>\n<tr>\n<td width=\"319\">Capybara successfully finds the element from the response.<\/td>\n<td width=\"319\">Waiting for a request.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, Capybara handles this interaction gracefully, even though the test starts looking for a link to click on before the page has finished loading.<\/p>\n<p>However, if Capybara handles these asynchronous issues for you, why is it so easy to write flapping tests with Capybara, where sometimes the tests pass and sometimes they fail?<\/p>\n<p>There are a few tricks to properly using the Capybara API so as to minimize the number of possible race conditions.<\/p>\n<h3><strong>Find the first matching element<\/strong><\/h3>\n<p>Bad:<\/p>\n<p>first(&#8220;.active&#8221;).click<\/p>\n<p>If there isn\u2019t an .active element on the page yet, first will return nil and the click will fail.<\/p>\n<p>Good:<\/p>\n<p># If you want to make sure there&#8217;s exactly one<\/p>\n<p>find(&#8220;.active&#8221;).click<\/p>\n<p># If you just want the first element<\/p>\n<p>find(&#8220;.active&#8221;, match: :first).click<\/p>\n<p>Capybara will wait for the element to appear before trying to click. Note that match: :first is more brittle, because it will silently click on a different element if you introduce new elements which match.<\/p>\n<h3><strong>Interact with all matching elements<\/strong><\/h3>\n<p>Bad:<\/p>\n<p>all(&#8220;.active&#8221;).each(&amp;:click)<\/p>\n<p>If there are no matching elements yet, an empty array will be returned, and no elements will be affected.<\/p>\n<p>Good:<\/p>\n<p>find(&#8220;.active&#8221;, match: :first)<\/p>\n<p>all(&#8220;.active&#8221;).each(&amp;:click)<\/p>\n<p>Capybara will wait for the first matching element before trying to click on the rest.<\/p>\n<h3><strong>Directly interacting with JavaScript<\/strong><\/h3>\n<p>Bad:<\/p>\n<p>execute_script(&#8220;$(&#8216;.active&#8217;).focus()&#8221;)<\/p>\n<p>JavaScript expressions may be evaluated before the action is complete, and the wrong element or no element may be affected.<\/p>\n<p>Good:<\/p>\n<p>find(&#8220;.active&#8221;)<\/p>\n<p>execute_script(&#8220;$(&#8216;.active&#8217;).focus()&#8221;)<\/p>\n<h3><strong>Checking a field\u2019s value<\/strong><\/h3>\n<p>Bad:<\/p>\n<p>expect(find_field(&#8220;Username&#8221;).value).to eq(&#8220;Joe&#8221;)<\/p>\n<p>Capybara will wait for the matching element and then immediately return its value. If the value changes from a page load or Ajax request, it will be too late.<\/p>\n<p>Good:<\/p>\n<p>expect(page).to have_field(&#8220;Username&#8221;, with: &#8220;Joe&#8221;)<\/p>\n<p>Capybara will wait for a matching element and then wait until its value matches, up to two seconds.<\/p>\n<h3><strong>Checking an element\u2019s attribute<\/strong><\/h3>\n<p>Bad:<\/p>\n<p>expect(find(&#8220;.user&#8221;)[&#8220;data-name&#8221;]).to eq(&#8220;Joe&#8221;)<\/p>\n<p>Capybara will wait for the matching element and then immediately return the requested attribute.<\/p>\n<p>Good:<\/p>\n<p>expect(page).to have_css(&#8220;.user[data-name=&#8217;Joe&#8217;]&#8221;)<\/p>\n<p>Capybara will wait for the element to appear and have the correct attribute.<\/p>\n<h3><strong>Looking for matching CSS<\/strong><\/h3>\n<p>Bad:<\/p>\n<p>it &#8220;doesn&#8217;t have an active class name&#8221; do<\/p>\n<p>expect(has_active_class).to be_false<\/p>\n<p>end<\/p>\n<p>&nbsp;<\/p>\n<p>def has_active_class<\/p>\n<p>has_css?(&#8220;.active&#8221;)<\/p>\n<p>end<\/p>\n<p>Capybara will immediately return true if the element hasn\u2019t been removed from the page yet, causing the test to fail. It will also wait two seconds before returning false, meaning the test will be slow when it passes.<\/p>\n<p>Good:<\/p>\n<p>it &#8220;doesn&#8217;t have an active class name&#8221; do<\/p>\n<p>expect(page).not_to have_active_class<\/p>\n<p>end<\/p>\n<p>&nbsp;<\/p>\n<p>def have_active_class<\/p>\n<p>have_css(&#8220;.active&#8221;)<\/p>\n<p>end<\/p>\n<p>Capybara will wait up to two seconds for the element to disappear before failing, and will pass immediately when the element isn\u2019t on the page as expected.<\/p>\n<p><strong>Make your resume stand out and become a Certified Capybara Testing Professional. <a href=\"https:\/\/www.vskills.in\/practice\/index.php?route=test\/search&amp;search=capybara\">Try free practice tests here!<\/a><\/strong><\/p>\n<p><strong>A great career is just a certification away. So, practice and validate your skills to become a <a href=\"https:\/\/www.vskills.in\/certification\/testing\/capybara-testing-certification\">Certified Capybara Testing Professional<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning Integration Testing Let&#8217;s learn Integration Testing. Integration tests are critically important because they exercise your application just like a real user. Therefore, depending on the full stack from your models up through your controllers, helpers, view templates, web server, database, and middleware. Integration tests can be brittle if they know too much about how&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8248],"tags":[8125],"class_list":["post-71412","page","type-page","status-publish","hentry","category-capybara","tag-integration-testing"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Integration Testing - Vskills Tutorial<\/title>\n<meta name=\"description\" content=\"Enrich your profile with expert tutorials on Integration Testing. Prepare and become Certified Capybara Testing Professional Now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Integration Testing - Vskills Tutorial\" \/>\n<meta property=\"og:description\" content=\"Enrich your profile with expert tutorials on Integration Testing. Prepare and become Certified Capybara Testing Professional Now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:43:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/\",\"name\":\"Integration Testing - Vskills Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg\",\"datePublished\":\"2020-01-10T06:00:26+00:00\",\"dateModified\":\"2024-04-12T08:43:41+00:00\",\"description\":\"Enrich your profile with expert tutorials on Integration Testing. Prepare and become Certified Capybara Testing Professional Now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#primaryimage\",\"url\":\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg\",\"contentUrl\":\"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Integration Testing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Integration Testing - Vskills Tutorial","description":"Enrich your profile with expert tutorials on Integration Testing. Prepare and become Certified Capybara Testing Professional Now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/","og_locale":"en_US","og_type":"article","og_title":"Integration Testing - Vskills Tutorial","og_description":"Enrich your profile with expert tutorials on Integration Testing. Prepare and become Certified Capybara Testing Professional Now!","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:43:41+00:00","og_image":[{"url":"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/","name":"Integration Testing - Vskills Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg","datePublished":"2020-01-10T06:00:26+00:00","dateModified":"2024-04-12T08:43:41+00:00","description":"Enrich your profile with expert tutorials on Integration Testing. Prepare and become Certified Capybara Testing Professional Now!","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#primaryimage","url":"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg","contentUrl":"https:\/\/cdn.educba.com\/academy\/wp-content\/uploads\/2019\/04\/Integration-Testing.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integration-testing-4\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Integration Testing"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=71412"}],"version-history":[{"count":7,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71412\/revisions"}],"predecessor-version":[{"id":78053,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71412\/revisions\/78053"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=71412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=71412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=71412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}