Selenium Simplified - Automated Browser Testing With Selenium WebDriver Made Simple

Aug 3, 2012 - 3 minute read - Selenium Simplified Blog WebDriver

Sporting Event Tribute Using Selenium WebDriver

As I write this, the capital city of England currently hosts a large sporting event. I can mention no names nor dates, for that sequence of words forms part of the protected thou shalt not use list. I don’t even know if ‘they’ will allow me to link to that page.

Regardless, I created a small test app which pays tribute to sporting events through the years.


Since I grew up in the 80s, programming on ZX Spectrums, I have a fondness for scrolly text that judders. I decided to replicate this effect using the browser title and hacking in my own text, overwriting the official text in page element.</p> <pre>((JavascriptExecutor)driver).executeScript("document.title='" + displayBanner + "'");</pre> <p> </p> <p>I don’t do this a lot when writing automated tests, but I have to confess - I don’t let the application functionality stopping me testing it. So I do sometimes force elements to be visible so that I can access them through automation. I mainly do this for exploratory automation work.</p> <p>If you have never done this before then I shall recommend now that you try it. Rule nothing out in your hunt for test automation effectiveness.</p> <p>I primarily wrote this little script to experiment with the WebDriver window positioning commands:</p> <ul> <li>(WebDriver) .manage().window.setSize</li> <li>(WebDriver) .manage().window().getPosition()</li> <li>WebDriver) .manage().window().setPosition()</li> </ul> <p>The listing from the video is below:</p> <pre>@Test public void bounceThatWindow(){ WebDriver driver = new FirefoxDriver(); driver.get("file://" + System.getProperty("user.dir") + "/jsrunner.html"); driver.manage().window().maximize(); Dimension fullScreenSize = driver.manage().window().getSize(); int changeWidth = 200; int changeHeight = 210; int xDir = 8; int yDir = 8; int xDirIncrement = xDir; int yDirIncrement = yDir; driver.manage().window().setSize(new Dimension(changeWidth,changeHeight)); Point position = driver.manage().window().getPosition(); String banner = "***BANG****........ AND THEY ARE OFF........ Automation can be fun. " + " present a javascript and browser" + " animation using Selenium 2 WebDriver tribute to the" + " sporting event that cannot be named lest we be sued"; int bannerStart = 0; for(int bounceIterations = 0; bounceIterations < 1000; bounceIterations ++){ position = position.moveBy(xDir,yDir); driver.manage().window().setPosition(position); if(position.getX()>(fullScreenSize.getWidth() - changeWidth)){ xDir = -1 * xDirIncrement; } if(position.getX()<0){ xDir = xDirIncrement; } if(position.getY()>(fullScreenSize.getHeight() - changeHeight)){ yDir = -1 * yDirIncrement; } if(position.getY()<0){ yDir = yDirIncrement; } String displayBanner = banner.substring(bannerStart,bannerStart+30); ((JavascriptExecutor)driver).executeScript("document.title='" + displayBanner + "'"); bannerStart++; if(bannerStart > banner.length()-35){banner += banner;} } driver.quit(); }</pre> <p>Since I only planned to let the demo run for a short period of time you can see that I just let the banner string extend and extend and extend by continually appending the text to it. Seemed like a nice sensible shortcoming given the nature of the demo. Just a warning to you in case you decide to put this into a loop for an hour or so.</p> <p>You can find the full source listing at <a href="" target="_blank"></a></p> <p>Specifically <a href="" target="_blank">here</a>.</p> <p>The ascii animation was created using <a href="" target="_blank"></a></p> <p>A fun extension, should you want a challenge, would be to create 3 windows, size them, and then have WebDriver play a game of <a href="" target="_blank">PONG</a> with them.</p> <p>Enjoy.</p> </div> <footer> <p class="meta"> <span class="byline author vcard">Posted by <span class="fn">Alan Richardson</span></span> <time>Aug 3, 2012</time> </span> </p> <p class="meta"> <a class="basic-alignment left" href="" title="Second Edition of Selenium Simplified is out">Second Edition of Selenium Simplified is out</a> <a class="basic-alignment right" href="" title="A minimal WebDriver based DSL">A minimal WebDriver based DSL</a> </p> </footer> </article> </div> <aside class="sidebar thirds"> <section class="first odd"> <h1>Selenium Simplified</h1> <p> <p><strong>Online WebDriver Training</strong></p> <p><a href="" target="_blank">Online Selenium 2 WebDriver & Java Course for only $229</a> <a href="" target="_blank"><img src="" alt="Online Selenium 2 WebDriver API training with Java Course" /></a></p> <p><a href="" target="_blank">Reviews: “Best technical course ever”, “Excellent!”, “Superb course!”</a></p> <p><strong>Install WebDriver Now</strong></p> <p>You can quickly and easily, install Selenium WebDriver & Java by following along with our <a href="/speedrun-installs">speedrun checklists and videos</a>.</p> <p><strong><a href="" target="_blank">Join Our Mailing List</a></strong></p> <p><strong>Books</strong></p> <p><a href="" title="Buy Java For Testers from leanpub" target="_blank"><img src="" alt="Buy Java For Testers Book" /></a> <a href="" title="Selenium Simplified Book - Selenium-RC" target="_blank"><img src="" alt="Buy Selenium Simplified Book" /></a></p> <p><a href="" target="_blank">Selenium Migration Book</a></p> <p><strong>Get Started Course</strong></p> <p><a href="" target="_blank"><img src="" alt="Free Selenium WebDriver Introductory Getting Started Course" /></a></p> </p> </section> <ul class="sidebar-nav"> <li class="sidebar-nav-item"> <a target="_blank" href="" title=""><i class="fa fa-github fa-3x"></i></a> <a target="_blank" href="" title=""><i class="fa fa-twitter fa-3x"></i></a> <a target="_blank" href="" title=""><i class="fa fa-linkedin fa-3x"></i></a> <a target="_blank" href="" title=""><i class="fa fa-facebook fa-3x"></i></a> <a target="_blank" href="" title=""><i class="fa fa-youtube fa-3x"></i></a> <a target="_blank" href="" title=""><i class="fa fa-instagram fa-3x"></i></a> </li> </ul> <section class="odd"> <li> <a href="" title="Evil Tester" >Evil Tester</a> </li> <li> <a href="" title="Java For Testers Blog" >Java For Testers Blog</a> </li> <li> <a href="" title="Compendium Developments" >Compendium Developments</a> </li> </section> <section class="even"> <h1>Recent Posts</h1> <ul id="recent_posts"> <li class="post"> <a href="/post/2018-06-06-os-selenium-migration-book/">Open Source Selenium Migration book</a> </li> <li class="post"> <a href="/post/2017-03-16-changes-in-selenium-webdriver-3-1-0-3-2-0-3-3-0-and-3-3-1-for-java/">Changes in Selenium WebDriver 3.1.0, 3.2.0, 3.3.0 and 3.3.1 for Java</a> </li> <li class="post"> <a href="/post/2016-11-21-question-can-selenium-be-used-for-real-world-testing-does-it-take-longer-than-manual/">Question: Can selenium be used for real world testing? & Does it take longer than manual?</a> </li> <li class="post"> <a href="/post/2016-10-20-q-should-you-use-selenium-3-a-probably-i-have/">Q: Should you use Selenium 3? A: Probably. I have.</a> </li> <li class="post"> <a href="/post/2016-10-20-sometimes-a-headless-browser-might-meet-your-needs/">Sometimes a headless browser might meet your needs</a> </li> <li class="post"> <a href="/post/2016-10-14-update-on-marionette-geckodriver-v0-11-1-q-should-you-try-it-a-yes-you-should/">Update on Marionette GeckoDriver v0.11.1 - Q: Should you try it? A: Yes you should.</a> </li> <li class="post"> <a href="/post/2016-10-14-upgrading-to-selenium-3-with-my-first-selenium-project/">Upgrading to Selenium 3 with My First Selenium Project</a> </li> <li class="post"> <a href="/post/2016-10-05-updated-webdriver-speedrun-install-videos/">Updated WebDriver SpeedRun Install Videos</a> </li> <li class="post"> <a href="/post/2016-08-25-how-to-select-a-web-element-with-an-id-containing/">How to Select a Web Element with an id containing '.'</a> </li> <li class="post"> <a href="/post/2016-08-17-how-to-debug-webdriver-javascriptexecutor-in-java/">How to Debug WebDriver JavascriptExecutor in Java</a> </li> <li class="post"> <a href="/post/2016-08-16-how-to-debug-chained-webdriver-findelement-methods-in-java/">How to debug chained WebDriver `findElement` methods in Java</a> </li> <li class="post"> <a href="/post/2016-08-05-selenium-webdriver-3-0-0-beta2-and-junit/">Selenium WebDriver 3.0.0-beta2 and JUnit and GeckoDriver v 0.10.0</a> </li> <li class="post"> <a href="/post/2016-08-05-webdriver-windows-10-anniversary-build-and-edge-14393/">WebDriver Windows 10 Anniversary Build and Edge 14393</a> </li> <li class="post"> <a href="/post/2016-08-04-question-what-is-the-best-page-object-framework-for-java/">Question: What is the best Page Object framework for Java?</a> </li> <li class="post"> <a href="/post/2016-07-29-first-impressions-of-selenium-webdriver-3-0-0-beta-1/">First impressions of Selenium WebDriver 3.0.0 beta 1</a> </li> </ul> </section> </aside> </div> </div> <footer role="contentinfo"> <p>Copyright © 2018 Alan Richardson, Compendium Developments Ltd. - <a href="">Privacy Policy</a> | <a href="">Contact Us</a> </p> </footer> <script async src=""></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-102378399-5', { 'anonymize_ip': true }); </script> <script src="//"></script> </body> </html>