Starting from Chrome 59 (60 in Windows), there are two things really shine Chrome in the field of UX automation and testing –
the devtools protocol. I was worked on a project to bring the UX automation and integration test for my team’s web service. We started with
CasperJS is a good wrapper tool on top of PhantomJS which provides good interface for testing.
Selenium, another widely adopted tool. Selenium can drive different kind of browsers, from IE to Chrome. It relief us for a few weeks until the performance and reliablity issues keep hitting us. So we have to move on to a better solution.
Then we learned Chrome already supports headless mode, which means you don’t need anything else to render a web apge in the background process and it runs as fast as Chrome headful mode. Our needs for fast rendering is cirtical. We have a bunch of developers are actively contributing to the web service, everyday the code base is changing. So we want to run our integration test in browser every single time a developer check in a change. We cannot tolerate a slow render engine to finish the work because it slows down our agility. We also need to use the same technology to periodicaly check our pages so that we are aware of live site issues earlier than our customers. So we run some rendering checks against our production service as well, and we want to hear the results as fast as possible.
Chrome DevTools Protocol (CDP), a protocol that uses JSON data as the commands to control Chrome browser remotely via websockets. This is like our normal F12 debugging window, but you can just program the behavior through your code.
Combining the headless and CDP makes a powerful tool for UX automation and testing. There are three scenarios need UX automation and testing in our project.
- localhost. A develop may want to quickly run some page checks before he/she wants to check in the code.
- CI Envrionment. Once a pull request completed, we will run the entire site again to check every page on our continuous integration environment. We will move the commit to release branch once the integration test passed.
- Production Envrionment. A period check is needed for our production service to alert us any live site issues.
The fundamental idea is to run this as a REST service that the caller could pass a page URL, then we use CDP to remotely automate the rendering and page validation (e.g. the picture above). At localhost, you will need to run Chrome from the commandline like the following.
$ chrome --remote-debugging-port=9222 --user-data-dir=remote-profile
If you want to run headless mode just add
--headless in the commandline.
$ chrome --remote-debugging-port=9222 --headless --user-data-dir=remote-profile
The port 9222 is open for devtools connection.
http://localhost:9222/json is your management place.
At localhost, you will see the chrome window get activated and navigated to different pages. You can create multiple tabs to run your pages in parallel and close them all later. The corresponding commands are the following.
At non-localhost environment, we still need a place with chrome installed and exposed. With the headless mode, we can use a very lightweight docker container that only runs Google Chrome inside. To easily scale out this service, we will need multiple chrome containers and use Kubernetes to manage it.
So the new architecture becomes like the below graph. You may have multiple web service instance and a cluster of containers to run chrome headless. Both web service and container cluster can be scaled independently and very easily.
const CDP = require('chrome-remote-interface');
using (var session = new ChromeSession("ws://...")
If authentication is enabled on your page and you are using OAuth based autentication mechanism, you will need to be a little bit more careful because these authentication needs to access browser’s localstorage for authentication token and a each container’s multiple tab share the same local storage.
It’s possible that multiple requests will ask for the execution of the same page on the same container, and you will need to inject authentication token for each page. Since the local storage is shared within the container, you may undermined other page’s execution when a page is trying to flush the local storage for authentication. Therefore, a distributed locking is preferred here to lock those local storage resources. You can easily use some distributed locking system such as Redlock to accomplish this goal.