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 <title/> element.

((JavascriptExecutor)driver).executeScript("document.title='" + displayBanner + "';");

 

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.

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.

I primarily wrote this little script to experiment with the WebDriver window positioning commands:

  • (WebDriver) .manage().window.setSize
  • (WebDriver) .manage().window().getPosition()
  • WebDriver) .manage().window().setPosition()

The listing from the video is below:

@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();
}

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.

You can find the full source listing at https://github.com/eviltester/webDriverExperiments/

Specifically here.

The ascii animation was created using asciimator.net

A fun extension, should you want a challenge, would be to create 3 windows, size them, and then have WebDriver play a game of PONG with them.

Enjoy.

This entry was posted in Selenium Simplified Blog, WebDriver. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *