Automating Automated Testing

One of the popular tools to test websites these days is Selenium, which is used to pass scripted actions to a browser such as Firefox or Chrome.   Unfortunately, it’s been a pain to set up, and you need to get a virtual frame buffer running for the browser (or perhaps run headless phantomjs, which is cool, but still WORK), and the browser itself… not to mention patch as updates come out.

Enter Docker: with docker, I can pull down the latest docker image provided by SeleniumHQ, with either Firefox or Chrome, and bingo!  It’s running and ready to go.   (That’s the general magic of docker, by the way)  Here’s an example:

[root@dockerhost] ~# docker run -d -p 4444:4444 selenium/standalone-chrome
Unable to find image 'selenium/standalone-chrome:latest' locally
Pulling repository selenium/standalone-chrome
c806a5e36041: Download complete 
511136ea3c5a: Download complete 
f0dde87450ec: Download complete 
76b658ecb564: Download complete 
4faa69f72743: Download complete 
2103b00b3fdf: Download complete 
60436a106b63: Download complete 
a5c56ead162a: Download complete 
1bcd40b41d9f: Download complete 
827b3070b898: Download complete 
f4f79c0be042: Download complete 
16bd409ea0a4: Download complete 
cd8ff3fed89b: Download complete 
4d67331e6a88: Download complete 
25e1b30f6eed: Download complete 
96ce19254976: Download complete 
8f0aaca2aae7: Download complete 
8e8240458885: Download complete 
cc1baa889ab6: Download complete 
2056ca638414: Download complete 
0606bc3f54f6: Download complete 
31a41159beb8: Download complete 
b532c7ea89cb: Download complete 
4129af115033: Download complete 
ba449c72b933: Download complete 
6f5a2f2e02a8: Download complete 
ba2fb7eae244: Download complete 
12317e85b372: Download complete 
4e75ed61c12f: Download complete 
5c9def4180f1: Download complete 
464ec9e0e9fb: Download complete 
9aaa498f52ed: Download complete 
639eff742ba8: Download complete 
a6fa8f2703b2: Download complete 
1715776d49ae: Download complete 
d834fd67171e: Download complete 
4b588c5bce51: Download complete 
cd22dea8848a: Download complete 
Status: Downloaded newer image for selenium/standalone-chrome:latest
c29d453b4d73e8196c3ef0e76f894d543e3ed0e5061b5c866d1a9ca4d6398833

[root@dockerhost] ~# docker ps
CONTAINER ID        IMAGE                               COMMAND                CREATED             STATUS              PORTS                    NAMES
c29d453b4d73        selenium/standalone-chrome:latest   "/opt/bin/entry_poin   2 minutes ago       Up 2 minutes        0.0.0.0:4444->4444/tcp   naughty_wright   

There! Up and running…. Now I just need to use it… there are several languages that will do so – I’ll show the Node.JS way. In this case, I’ll be running node.js from another machine (having installed the selenium webdriver in the standard /usr/lib/node_modules dir), and it will talk to my docker container running on the host above (or Amazon, or wherever I wanted it).

Here’s my script: note it connects to dockerhost:4444 as set up above…

[root@DEV] tmp# cat nat.js 
var webdriver = require("selenium-webdriver");
 
function createDriver() {
    var driver = new webdriver.Builder()
        .usingServer('http://dockerhost:4444/wd/hub')
        .withCapabilities(webdriver.Capabilities.chrome())
        .build();
    driver.manage().timeouts().setScriptTimeout(10000);
    return driver;
}
 
var driver = createDriver();
driver.get("http://www.google.com");
 
driver.getTitle().then(function (title) {
    console.log(title);
});
 
driver.quit();

Then all I need to do is run it! It should fetch the page and spit out the title.

[root@DEV] tmp# export NODE_PATH=/usr/lib/node_modules
[root@DEV] tmp# node nat.js 
Google

Now I have a lot more incentive to learn how to use Selenium better, since the barriers to getting started are lower.

Share and Enjoy:
  • Print
  • Facebook
  • Twitter
  • PDF

One Reply to “Automating Automated Testing”

Leave a Reply

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