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. " + " EvilTester.com 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="https://github.com/eviltester/webDriverExperiments/" target="_blank">https://github.com/eviltester/webDriverExperiments/</a></p> <p>Specifically <a href="https://github.com/eviltester/webDriverExperiments/blob/master/src/test/java/com/seleniumsimplified/webdriver/sports/SportsEventTributeTest.java" target="_blank">here</a>.</p> <p>The ascii animation was created using <a href="http://asciimator.net/" target="_blank">asciimator.net</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="http://en.wikipedia.org/wiki/Pong" 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="http://seleniumsimplified.com/post/2012-06-15-second-edition-of-selenium-simplified-is-out/" title="Second Edition of Selenium Simplified is out">Second Edition of Selenium Simplified is out</a> <a class="basic-alignment right" href="http://seleniumsimplified.com/post/2012-08-03-a-minimal-webdriver-based-dsl/" 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="http://unow.be/at/javawebdriverapi" target="_blank">Online Selenium 2 WebDriver & Java Course for only $229</a> <a href="http://unow.be/at/javawebdriverapi" target="_blank"><img src="http://seleniumsimplified.com/image/courses/selenium_2_webdriver_75pc.jpg" alt="Online Selenium 2 WebDriver API training with Java Course" /></a></p> <p><a href="http://seleniumsimplified.com/selenium-2-webdriver-with-java-course-reviews/" 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="https://www.compendiumdev.co.uk/page/join" target="_blank">Join Our Mailing List</a></strong></p> <p><strong>Books</strong></p> <p><a href="https://leanpub.com/javaForTesters" title="Buy Java For Testers from leanpub" target="_blank"><img src="http://www.seleniumsimplified.com/image/smallJavaForTestersCover.png" alt="Buy Java For Testers Book" /></a> <a href="http://www.compendiumdev.co.uk/selenium" title="Selenium Simplified Book - Selenium-RC" target="_blank"><img src="http://www.seleniumsimplified.com/image/cover_small_2nd.gif" alt="Buy Selenium Simplified Book" /></a></p> <p><strong>Get Started Course</strong></p> <p><a href="http://seleniumsimplified.com/get-started" target="_blank"><img src="http://seleniumsimplified.com/image/courses/start_using_selenium_webdriver_75pc.jpg" 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="https://github.com/eviltester" title="https://github.com/eviltester"><i class="fa fa-github fa-3x"></i></a> <a target="_blank" href="https://twitter.com/eviltester" title="https://twitter.com/eviltester"><i class="fa fa-twitter fa-3x"></i></a> <a target="_blank" href="https://www.linkedin.com/in/eviltester/" title="https://www.linkedin.com/in/eviltester/"><i class="fa fa-linkedin fa-3x"></i></a> <a target="_blank" href="https://www.facebook.com/eviltester/" title="https://www.facebook.com/eviltester/"><i class="fa fa-facebook fa-3x"></i></a> <a target="_blank" href="https://www.youtube.com/user/EviltesterVideos" title="https://www.youtube.com/user/EviltesterVideos"><i class="fa fa-youtube fa-3x"></i></a> <a target="_blank" href="https://www.instagram.com/eviltester/" title="https://www.instagram.com/eviltester/"><i class="fa fa-instagram fa-3x"></i></a> </li> </ul> <section class="odd"> <li> <a href="http://eviltester.com" title="Evil Tester" >Evil Tester</a> </li> <li> <a href="http://javafortesters.com" title="Java For Testers Blog" >Java For Testers Blog</a> </li> <li> <a href="https://compendiumdev.co.uk" 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/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> <li class="post"> <a href="/post/2016-07-29-comparing-webdriver-2-53-1-execution-for-firefox-chrome-ie-htmlunit-edge-and-marionette/">Comparing WebDriver 2.53.1 execution for Firefox, Chrome, IE, HTMLUnit, Edge and Marionette</a> </li> </ul> </section> </aside> </div> </div> <footer role="contentinfo"> <p>Copyright © 2018 Alan Richardson, Compendium Developments Ltd. - <a href="https://www.compendiumdev.co.uk/page.php?title=privacy">Privacy Policy</a> | <a href="https://www.compendiumdev.co.uk/page.php?title=contact_us">Contact Us</a> </p> </footer> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-102378399-5"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-102378399-5'); </script> <script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US&adInstanceId=95a37ca4-4500-4277-a60b-a3ad533f36d9"></script> </body> </html>