{"id":28,"date":"2020-01-15T11:23:27","date_gmt":"2020-01-15T11:23:27","guid":{"rendered":"https:\/\/cebucodesolutions.com\/blog\/?p=28"},"modified":"2020-01-15T11:23:27","modified_gmt":"2020-01-15T11:23:27","slug":"disabling-html5-form-validation-for-laravel-dusk-tests","status":"publish","type":"post","link":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/","title":{"rendered":"Disabling HTML5 form validation for Laravel Dusk tests"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you\u2019re testing form validation using Laravel Dusk, you\u2019ve probably\n hit a scenario where the form submission is blocked by Chrome before \nit\u2019s sent to the server because of HTML5 form validation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">HTML5 form validation blocks a submission when you add an attribute such as <code>required<\/code> to an input element, or you use <code>type=\"email\"<\/code>.  In your Laravel Dusk tests, you\u2019ll probably see this manifesting as a  client-side popup dialog in your failing test screenshots.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"241\" src=\"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-1024x241.png\" alt=\"\" class=\"wp-image-29\" srcset=\"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-1024x241.png 1024w, https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-300x71.png 300w, https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-768x181.png 768w, https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM.png 1054w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This is a really tricky thing to test. Since it happens completely \nclient-side without modifying the DOM, it\u2019s hard to assert that the \nerror appears. Even if you do work out a hacky way to do it, it\u2019s not \nactually testing that your web application\u2019s form validation is working.\n You\u2019re essentially just testing that Chrome supports HTML5 form \nvalidation, and I\u2019m sure the Google\/Chromium teams have their own tests \nfor that. \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Your first thought may be to try to disable HTML5 form validation using <code>ChromeOptions<\/code> in <code>DuskTestCase<\/code>. Unfortunately there is no such option. That really only leaves one way: adding the <code>novalidate<\/code>\n property to your HTML forms. This attribute, when applied to a HTML \nform, disables the browser\u2019s native form validation. But this has its \nown drawback in that it disables a perfectly useful feature that \nprobably provides value to your users.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thankfully, Dusk allows you to <strong>inject JavaScript onto the page at runtime!<\/strong> Using this feature, we can dynamically inject a small code snippet that iterates over the forms on the page and adds the <code>novalidate<\/code> attribute automatically, <strong>just for your tests<\/strong>. By adding this browser macro to <code>DuskTestCase<\/code>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thankfully, Dusk allows you to <strong>inject JavaScript onto the page at runtime!<\/strong> Using this feature, we can dynamically inject a small code snippet that iterates over the forms on the page and adds the <code>novalidate<\/code> attribute automatically, <strong>just for your tests<\/strong>. By adding this browser macro to <code>DuskTestCase<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static function prepare()\n{\n    static::startChromeDriver();\n\n    Browser::macro('disableClientSideValidation', function () {\n        $this->script('for(var f=document.forms,i=f.length;i--;)f[i].setAttribute(\"novalidate\",i)');\n\n        return $this;\n    });\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\nyou can then call it within your tests like so:\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$this->browse(function (Browser $browser) {\n    $browser->visit('\/register')\n        ->disableClientSideValidation()\n        ->type('email', 'notvalidemail')\n        ->click('@submit')\n\n        ->assertPathIs('\/register')\n        ->assertSee('The email must be a valid email address.');\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\nAnd viola! The result is you\u2019re actually testing your application\u2019s form validation, and <strong>you don\u2019t have to give up HTML5 form validation to do it<\/strong>.\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re testing form validation using Laravel Dusk, you\u2019ve probably hit a scenario where the form submission is blocked by Chrome before it\u2019s sent to the server because of HTML5 form validation. HTML5 form validation blocks a submission when you add an attribute such as required to an input element, or you use type=&#8221;email&#8221;. In &hellip; <a href=\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Disabling HTML5 form validation for Laravel Dusk tests&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-updates"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Disabling HTML5 form validation for Laravel Dusk tests - Web Development Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Disabling HTML5 form validation for Laravel Dusk tests - Web Development Blogs\" \/>\n<meta property=\"og:description\" content=\"If you\u2019re testing form validation using Laravel Dusk, you\u2019ve probably hit a scenario where the form submission is blocked by Chrome before it\u2019s sent to the server because of HTML5 form validation. HTML5 form validation blocks a submission when you add an attribute such as required to an input element, or you use type=&quot;email&quot;. In &hellip; Continue reading &quot;Disabling HTML5 form validation for Laravel Dusk tests&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Development Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-15T11:23:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-1024x241.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/\",\"url\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/\",\"name\":\"Disabling HTML5 form validation for Laravel Dusk tests - Web Development Blogs\",\"isPartOf\":{\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-1024x241.png\",\"datePublished\":\"2020-01-15T11:23:27+00:00\",\"dateModified\":\"2020-01-15T11:23:27+00:00\",\"author\":{\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/#\/schema\/person\/094505f311153db12b4f0185cdef52fa\"},\"breadcrumb\":{\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#primaryimage\",\"url\":\"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM.png\",\"contentUrl\":\"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM.png\",\"width\":1054,\"height\":248},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cebucodesolutions.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Disabling HTML5 form validation for Laravel Dusk tests\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/#website\",\"url\":\"https:\/\/cebucodesolutions.com\/blog\/\",\"name\":\"Web Development Blogs\",\"description\":\"Daily updates of latest tricks\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cebucodesolutions.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/#\/schema\/person\/094505f311153db12b4f0185cdef52fa\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cebucodesolutions.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/58f32b4c3c414f0630b2359b5bb9b668a8d670e7e163fbc227f4f5611acacdbc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/58f32b4c3c414f0630b2359b5bb9b668a8d670e7e163fbc227f4f5611acacdbc?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/cebucodesolutions.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Disabling HTML5 form validation for Laravel Dusk tests - Web Development Blogs","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:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/","og_locale":"en_US","og_type":"article","og_title":"Disabling HTML5 form validation for Laravel Dusk tests - Web Development Blogs","og_description":"If you\u2019re testing form validation using Laravel Dusk, you\u2019ve probably hit a scenario where the form submission is blocked by Chrome before it\u2019s sent to the server because of HTML5 form validation. HTML5 form validation blocks a submission when you add an attribute such as required to an input element, or you use type=\"email\". In &hellip; Continue reading \"Disabling HTML5 form validation for Laravel Dusk tests\"","og_url":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/","og_site_name":"Web Development Blogs","article_published_time":"2020-01-15T11:23:27+00:00","og_image":[{"url":"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-1024x241.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/","url":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/","name":"Disabling HTML5 form validation for Laravel Dusk tests - Web Development Blogs","isPartOf":{"@id":"https:\/\/cebucodesolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#primaryimage"},"image":{"@id":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#primaryimage"},"thumbnailUrl":"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM-1024x241.png","datePublished":"2020-01-15T11:23:27+00:00","dateModified":"2020-01-15T11:23:27+00:00","author":{"@id":"https:\/\/cebucodesolutions.com\/blog\/#\/schema\/person\/094505f311153db12b4f0185cdef52fa"},"breadcrumb":{"@id":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#primaryimage","url":"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM.png","contentUrl":"https:\/\/cebucodesolutions.com\/blog\/wp-content\/uploads\/2020\/01\/Screenshot-2020-01-02-at-7.58.23-PM.png","width":1054,"height":248},{"@type":"BreadcrumbList","@id":"https:\/\/cebucodesolutions.com\/blog\/2020\/01\/15\/disabling-html5-form-validation-for-laravel-dusk-tests\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cebucodesolutions.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Disabling HTML5 form validation for Laravel Dusk tests"}]},{"@type":"WebSite","@id":"https:\/\/cebucodesolutions.com\/blog\/#website","url":"https:\/\/cebucodesolutions.com\/blog\/","name":"Web Development Blogs","description":"Daily updates of latest tricks","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cebucodesolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cebucodesolutions.com\/blog\/#\/schema\/person\/094505f311153db12b4f0185cdef52fa","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cebucodesolutions.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/58f32b4c3c414f0630b2359b5bb9b668a8d670e7e163fbc227f4f5611acacdbc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/58f32b4c3c414f0630b2359b5bb9b668a8d670e7e163fbc227f4f5611acacdbc?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/cebucodesolutions.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":1,"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cebucodesolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}