{"id":127,"date":"2014-08-08T21:32:29","date_gmt":"2014-08-09T03:32:29","guid":{"rendered":"http:\/\/xiaan.com\/blog\/?p=127"},"modified":"2024-02-09T15:14:52","modified_gmt":"2024-02-09T22:14:52","slug":"selenide-extending-com-codeborne-selenide-condition","status":"publish","type":"post","link":"https:\/\/xiaan.com\/blog\/2014\/08\/selenide-extending-com-codeborne-selenide-condition\/","title":{"rendered":"Selenide: Extending com.codeborne.selenide.Condition"},"content":{"rendered":"\n<p>Of all the libraries I&#8217;ve used over the years, <a href=\"http:\/\/selenide.org\">Selenide<\/a> has come to occupy a top spot in my developer&#8217;s toolkit second only to that of jQuery. Selenide has played a crucial role in making Selenium (while functional) easy to use.<\/p>\n\n\n\n<p>I&#8217;ve been using Selenide for a few months on a current project; I&#8217;ve finally got enough code coverage to where I&#8217;m able to take a few minutes and refactor the test code to be more efficient and readable. I&#8217;ve been suffering from some intermittent test failures due to timing issues with JUnit asserts, and finally found the time to address them.<\/p>\n\n\n\n<p>Previously, I&nbsp;had written:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#ffffff\"><span style=\"background:#2f363c;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#ffffff\">Java<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"assertTrue(&quot;Element should be displayed.&quot;, $(elem).isDisplayed());\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6F42C1\">assertTrue<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;Element should be displayed.&quot;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #6F42C1\">$<\/span><span style=\"color: #24292E\">(elem).<\/span><span style=\"color: #6F42C1\">isDisplayed<\/span><span style=\"color: #24292E\">());<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>But with a few months of Selenide under my belt, I\u00a0now know that it&#8217;s always better to use the <code>SelenideElement.should()<\/code> methods where possible. With <code>should()<\/code>, Selenide will by default check for a state every 100ms until 4 seconds have expired. But, Selenide does not include a check for whether an application is in fullscreen mode. By extending\u00a0the existing <code>com.codeborne.selenide.Condition<\/code> class, I get:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#ffffff\"><span style=\"background:#2f363c;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#ffffff\">Java<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"package dg.mydg.selenide;\nimport org.openqa.selenium.WebElement;\nimport static com.codeborne.selenide.Selenide.executeJavaScript;\n\npublic abstract class Condition extends com.codeborne.selenide.Condition {\n\n  \/**\n  * Checks to see whether the application is in fullscreen mode.\n  *\/\n  public static final Condition fullscreen = new Condition(&quot;fullscreen&quot;) {\n    @Override public boolean apply(WebElement element) {\n      Boolean isFullscreen = executeJavaScript(&quot;return (window.innerWidth == screen.width)&quot;);\n      return ((isFullscreen == null) ? false : isFullscreen);\n    }\n    \n    @Override public String toString() {\n      return &quot;is in fullscreen mode&quot;;\n    }\n  };\n}\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D73A49\">package<\/span><span style=\"color: #24292E\"> dg.mydg.selenide;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D73A49\">import<\/span><span style=\"color: #24292E\"> org.openqa.selenium.WebElement;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D73A49\">import<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">static<\/span><span style=\"color: #24292E\"> com.codeborne.selenide.Selenide.executeJavaScript;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D73A49\">public<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">abstract<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">class<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">Condition<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">extends<\/span><span style=\"color: #24292E\"> com.codeborne.selenide.<\/span><span style=\"color: #6F42C1\">Condition<\/span><span style=\"color: #24292E\"> {<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\">  \/**<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\">  * Checks to see whether the application is in fullscreen mode.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\">  *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  <\/span><span style=\"color: #D73A49\">public<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">static<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">final<\/span><span style=\"color: #24292E\"> Condition fullscreen <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">new<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">Condition<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;fullscreen&quot;<\/span><span style=\"color: #24292E\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    @<\/span><span style=\"color: #D73A49\">Override<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">public<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">boolean<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">apply<\/span><span style=\"color: #24292E\">(WebElement <\/span><span style=\"color: #E36209\">element<\/span><span style=\"color: #24292E\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      Boolean isFullscreen <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">executeJavaScript<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&quot;return (window.innerWidth == screen.width)&quot;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      <\/span><span style=\"color: #D73A49\">return<\/span><span style=\"color: #24292E\"> ((isFullscreen <\/span><span style=\"color: #D73A49\">==<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">null<\/span><span style=\"color: #24292E\">) <\/span><span style=\"color: #D73A49\">?<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">false<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> isFullscreen);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    @<\/span><span style=\"color: #D73A49\">Override<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">public<\/span><span style=\"color: #24292E\"> String <\/span><span style=\"color: #6F42C1\">toString<\/span><span style=\"color: #24292E\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      <\/span><span style=\"color: #D73A49\">return<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #032F62\">&quot;is in fullscreen mode&quot;<\/span><span style=\"color: #24292E\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Now I can use it within the Selenide should framework:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#ffffff\"><span style=\"background:#2f363c;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#ffffff\">Java<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"$(elem).shouldBe(fullscreen);\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6F42C1\">$<\/span><span style=\"color: #24292E\">(elem).<\/span><span style=\"color: #6F42C1\">shouldBe<\/span><span style=\"color: #24292E\">(fullscreen);<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Of all the libraries I&#8217;ve used over the years, Selenide has come to occupy a top spot in my developer&#8217;s toolkit second only to that of jQuery. Selenide has played a crucial role in making Selenium (while functional) easy to use. I&#8217;ve been using Selenide for a few months on a current project; I&#8217;ve finally &#8230;<a class=\"post-readmore\" href=\"https:\/\/xiaan.com\/blog\/2014\/08\/selenide-extending-com-codeborne-selenide-condition\/\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19,18],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-java","category-selenide"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p467qb-23","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":9,"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xiaan.com\/blog\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}