{"id":71413,"date":"2020-01-10T11:30:30","date_gmt":"2020-01-10T06:00:30","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=71413"},"modified":"2024-04-12T14:13:42","modified_gmt":"2024-04-12T08:43:42","slug":"javascript-testing","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/","title":{"rendered":"JavaScript Testing"},"content":{"rendered":"<h1><strong>Learning JavaScript Testing<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png\" alt=\"JavaScript Testing\" width=\"655\" height=\"343\" \/><\/p>\n<p>Let&#8217;s start JavaScript Testing. By default, Capybara uses Rack::Test which is a headless browser emulator. It gives us great speed, but we sacrifice the ability to run JavaScript. If you need to test JS as part of your integration suite, then you need to use another driver.<\/p>\n<p>If you\u2019re using Linux, you\u2019ll need to set up xvfb in order to use either Selenium or Capybara-Webkit. Here are the Ubuntu commands to install xvfb and additional fonts to get rid of some warnings.<\/p>\n<p>sudo apt-get install xvfb<\/p>\n<p>sudo apt-get install x11-xkb-utils<\/p>\n<p>In addition, sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic<\/p>\n<p>In order to use xvfb with your specs, you will need to run xvfb-run bundle exec rake (an alias may be in order). Now selunium and capybara-webkit will use xvfb when launching a browser.<\/p>\n<h2><strong>Using Selenium: JavaScript Testing<\/strong><\/h2>\n<p>The most popular driver is Selenium. It uses an actual browser window and you can watch the test happen. It uses the browser\u2019s actual JavaScript engine, so it\u2019s identical to having a human Q\/A department interacting with your application.<\/p>\n<p>If you have Firefox 4 installed then there are no extra setup steps. It\u2019s possible to use Chrome or another WebKit-based browser, but it is more work.<\/p>\n<p><strong>Modifying Your Examples<\/strong><\/p>\n<p>If you want to only use the browser for a few specific tests, you can add js: true into the it line like this<\/p>\n<p>it &#8220;should list the article titles on the index&#8221;, js: true do<\/p>\n<p>@articles.each do |article|<\/p>\n<p>page.should have_link(article.title)<\/p>\n<p>end<\/p>\n<p>end<\/p>\n<p>More commonly you\u2019ll have a group of tests that you want to run in the browser. Rather than litter js: true all over the place, do this:<\/p>\n<ul>\n<li>Create a describe block that contains the examples that need JavaScript<\/li>\n<li>Add a before-all block like this:<\/li>\n<\/ul>\n<p>before(:all) do<\/p>\n<p>Capybara.current_driver = :selenium<\/p>\n<p>end<\/p>\n<ul>\n<li>Put your examples after the before block<\/li>\n<li>Add an after-all block like this:<\/li>\n<\/ul>\n<p>after(:all) do<\/p>\n<p>Capybara.use_default_driver<\/p>\n<p>end<\/p>\n<p>Now any example added inside that describe will use Selenium. If you forget the after(:all) block, all subsequent tests will continue using the :selenium driver, which will work, albeit more slowly than with the default, headless driver.<\/p>\n<h2><strong>Selenium Methods: JavaScript Testing<\/strong><\/h2>\n<p>If you\u2019re just triggering AJAX actions via JavaScript you can probably get by with the normal Capybara actions. But Selenium itself has many actions that are not directly supported by Capybara.<\/p>\n<p>But that\u2019s ok! If you ask Capybara for page.driver.browser while in a Selenium-powered test, it\u2019ll give you the Selenium::WebDriver::Driver object. You can then access any Selenium method according to the API here: http:\/\/selenium.googlecode.com\/svn\/trunk\/docs\/api\/rb\/Selenium\/WebDriver\/Driver.html<\/p>\n<p>Checking for an Alert<\/p>\n<p>Here\u2019s a complete example of how you could use Selenium to check that an alert pops up when we attempt to delete an article:<\/p>\n<p>describe &#8220;on the show page for an article&#8221; do<\/p>\n<p>before(:all) do<\/p>\n<p>Capybara.current_driver = :selenium<\/p>\n<p>end<\/p>\n<p>&nbsp;<\/p>\n<p>before(:each) do<\/p>\n<p>@article = @articles.first<\/p>\n<p>visit article_path(@article)<\/p>\n<p>end<\/p>\n<p>it &#8220;pops up a confirm dialog when we click delete&#8221; do<\/p>\n<p>page.click_link(&#8220;Delete&#8221;)<\/p>\n<p>dialog = page.driver.browser.switch_to.alert<\/p>\n<p>dialog.text.should == &#8220;Delete &#8216;#{@article.title}&#8217;?&#8221;<\/p>\n<p>dialog.dismiss<\/p>\n<p>end<\/p>\n<p>after(:all) do<\/p>\n<p>Capybara.use_default_driver<\/p>\n<p>end<\/p>\n<p>end<\/p>\n<p>You could also use dialog.accept to click the OK button where dialog.dismiss clicks CANCEL.<\/p>\n<p>Selenium Alternatives<\/p>\n<p>Selenium, for one reason or another, makes developers a little uneasy. It sometimes has issues in the development versions but, most importantly, it\u2019s slow. It would be awesome if we could run JavaScript without actually waiting for the slow rendering of a GUI window.<\/p>\n<h2><strong>Using WebKit: JavaScript Testing<\/strong><\/h2>\n<p>The WebKit framework powers Chrome, Safari, and most mobile phone browsers. It\u2019s a popular open source project and is really at the vanguard of web browsers.<\/p>\n<p>The team at ThoughtBot, a Rails consultancy in Boston, put together the capybara-webkit gem: https:\/\/github.com\/thoughtbot\/capybara-webkit<\/p>\n<p>It uses the WebKit framework as a headless browser. We get almost all the speed of being headless with Rack::Test, but the power of a full, real-world JavaScript interpreter.<\/p>\n<p>Add the Gem<\/p>\n<p>Open your Gemfile and add the following in your development dependencies:<\/p>\n<p>gem &#8216;capybara-webkit&#8217;<\/p>\n<p>At the time of this writing, it was necessary to use the 1.0 branch of capybara-webkit like this:<\/p>\n<p>gem &#8220;capybara-webkit&#8221;, git: &#8220;https:\/\/github.com\/thoughtbot\/capybara-webkit.git&#8221;, branch: &#8220;1.0&#8221;<\/p>\n<p>Tell Capybara about Capybara-Webkit<\/p>\n<p>Then you\u2019d hop into your spec\/spec_helper.rb and add this line inside the RSpec.configure block:<\/p>\n<p>Capybara.javascript_driver = :webkit<\/p>\n<p>Run Your Examples<\/p>\n<p>Now all you do is run your examples! We just swapped the driver, but the way we tell Capybara to use it is exactly the same as Selenium. If you want to run a single test with WebKit, add js: true to the it line.<\/p>\n<p>If you have a set of examples to run with JavaScript, wrap them in a describe block with a before-all and after-all like this:<\/p>\n<p>describe &#8220;run with webkit&#8221; do<\/p>\n<p>before(:all) do<\/p>\n<p>Capybara.current_driver = :webkit<\/p>\n<p>end<\/p>\n<p>it &#8220;runs something fancy with javascript&#8221;<\/p>\n<p>after(:all) do<\/p>\n<p>Capybara.use_default_driver<\/p>\n<p>end<\/p>\n<p>end<\/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 JavaScript Testing Let&#8217;s start JavaScript Testing. By default, Capybara uses Rack::Test which is a headless browser emulator. It gives us great speed, but we sacrifice the ability to run JavaScript. If you need to test JS as part of your integration suite, then you need to use another driver. If you\u2019re using Linux, you\u2019ll&#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":[8290],"class_list":["post-71413","page","type-page","status-publish","hentry","category-capybara","tag-javascript-testing"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Testing - Vskills Tutorial<\/title>\n<meta name=\"description\" content=\"Enrich your profile with expert tutorials on JavaScript 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\/javascript-testing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Testing - Vskills Tutorial\" \/>\n<meta property=\"og:description\" content=\"Enrich your profile with expert tutorials on JavaScript Testing . Prepare and become Certified Capybara Testing Professional Now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/\" \/>\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:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png\" \/>\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\/javascript-testing\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/\",\"name\":\"JavaScript Testing - Vskills Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png\",\"datePublished\":\"2020-01-10T06:00:30+00:00\",\"dateModified\":\"2024-04-12T08:43:42+00:00\",\"description\":\"Enrich your profile with expert tutorials on JavaScript Testing . Prepare and become Certified Capybara Testing Professional Now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#primaryimage\",\"url\":\"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png\",\"contentUrl\":\"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript 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":"JavaScript Testing - Vskills Tutorial","description":"Enrich your profile with expert tutorials on JavaScript 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\/javascript-testing\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Testing - Vskills Tutorial","og_description":"Enrich your profile with expert tutorials on JavaScript Testing . Prepare and become Certified Capybara Testing Professional Now!","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:43:42+00:00","og_image":[{"url":"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png","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\/javascript-testing\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/","name":"JavaScript Testing - Vskills Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#primaryimage"},"thumbnailUrl":"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png","datePublished":"2020-01-10T06:00:30+00:00","dateModified":"2024-04-12T08:43:42+00:00","description":"Enrich your profile with expert tutorials on JavaScript Testing . Prepare and become Certified Capybara Testing Professional Now!","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#primaryimage","url":"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png","contentUrl":"https:\/\/crossbrowsertesting.com\/blog\/wp-content\/uploads\/2018\/01\/CBT-Blog-Javascript-frameworks-3.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/javascript-testing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"JavaScript 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\/71413","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=71413"}],"version-history":[{"count":6,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71413\/revisions"}],"predecessor-version":[{"id":78055,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71413\/revisions\/78055"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=71413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=71413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=71413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}