{"id":157,"date":"2024-04-26T07:46:31","date_gmt":"2024-04-26T07:46:31","guid":{"rendered":"https:\/\/blog.acini.pl\/?p=157"},"modified":"2024-05-20T07:49:17","modified_gmt":"2024-05-20T07:49:17","slug":"navigating-the-quirks-of-html-date-input-with-selenium-selenide-and-beyond","status":"publish","type":"post","link":"https:\/\/blog.acini.pl\/index.php\/2024\/04\/26\/navigating-the-quirks-of-html-date-input-with-selenium-selenide-and-beyond\/","title":{"rendered":"Navigating the Quirks of HTML Date Input with Selenium\/Selenide and Beyond"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Recently, I found myself facing a seemingly simple task: filling in an HTML input of type <em><code>Date<\/code> <\/em>using Selenium and Selenide. While my experience was with Selenium, I suspect the challenges I encountered would be similar if I were using Cypress or other testing frameworks.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Standard Approach: $(bySelector).setValue(theNewValue)<\/h4>\n\n\n\n<p>In most cases, the straightforward approach of using Selenide&#8217;s <code>setValue<\/code> method works like a charm:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(bySelector).setValue(theNewValue);<\/pre>\n\n\n\n<p>However, I hit a roadblock when Firefox decided to play hard to get. Firefox presents a neat calendar popup for the <code>Date<\/code> input, but the calendar itself is not part of the web page&#8217;s source code. If you inspect the page source, you&#8217;ll only find <code><em>&lt;input type=\"date\"&gt;<\/em><\/code> and nothing more.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Thinking Outside the Box: Hacking the Calendar Popup<\/h4>\n\n\n\n<p>In my desperation, I considered trying to hack Firefox&#8217;s calendar popup by displaying it and clicking the appropriate date. I even thought about using Java&#8217;s <em><code>Robot<\/code> <\/em>class and <code>mousePress<\/code> function. But let&#8217;s face it: that approach would be error-prone, a maintenance nightmare, and wouldn&#8217;t impress any of our other beloved browsers.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Exploring Alternatives: From Selenide to JavaScript<\/h4>\n\n\n\n<p>At this point, I decided to abandon the Selenide\/Java route and dive deeper into the underlying technologies. I transitioned from Selenide to Selenium and eventually found myself delving into JavaScript.<\/p>\n\n\n\n<p>Initially, I thought I could quickly whip up a dirty script to find a JavaScript variable and inject my value directly. However, I quickly realized that this wouldn&#8217;t work with React. Once again, I was forced to abandon my favorite shortcuts and follow best practices for simulating events on the input element.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Final Solution: Executing JavaScript with Selenide<\/h4>\n\n\n\n<p>After much trial and error, I came up with a solution that involved executing JavaScript with Selenium. Here&#8217;s the code snippet that finally did the trick:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Selenide.webdriver().driver().executeJavaScript(\n    \"var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, \\\"value\\\").set;\"\n    + \"nativeInputValueSetter.call(arguments&#91;0], arguments&#91;1]);\"\n    + \"var ev2 = new Event('change', { bubbles: true});\"\n    + \"arguments&#91;0].dispatchEvent(ev2);\"\n, input, formattedDate);<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">&#8220;Dirty Shortcuts&#8221; conclusion<\/h4>\n\n\n\n<p>In conclusion, navigating the complexities of HTML <code>Date<\/code> input across different browsers and testing frameworks presented its fair share of challenges. From initially relying on Selenide&#8217;s <code>setValue<\/code> method to exploring the JavaScript and ultimately executing custom scripts, it is a journey of trial and error. While shortcuts can be tempting, following best practices often proves to be the most reliable approach in the long run.<\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/automatization\/\" rel=\"tag\">Automatization<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/input\/\" rel=\"tag\">input<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/selenide\/\" rel=\"tag\">selenide<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/selenium\/\" rel=\"tag\">selenium<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/blog.acini.pl\/index.php\/tag\/tests\/\" rel=\"tag\">tests<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Our coding expert and enthusiast Krzysztof Mroczek on all shades of Selenium and more!<\/p>\n","protected":false},"author":1,"featured_media":130,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[25],"tags":[37,36,33,32,35],"class_list":["post-157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-our-tips-tricks","tag-automatization","tag-input","tag-selenide","tag-selenium","tag-tests"],"_links":{"self":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":6,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/posts\/157\/revisions\/163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/media\/130"}],"wp:attachment":[{"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.acini.pl\/index.php\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}