-
Notifications
You must be signed in to change notification settings - Fork 38
Viewers
Celerity uses HtmlUnit, a headless Java browser library, to interact with web pages, which makes it faster than driving a real browser at the cost of not having any visual feedback from your tests. To work around this you can use one of apps from the CelerityViewers repo to simulate driving a browser.
Celerity won’t actually drive these, but sends an HTML string to the viewer every time the page changes, which takes care of rendering it. The results are far from perfect, but better than nothing.
Some of the viewers have the ability to save a screenshot to disk if told to:
browser.viewer.save(path)
You can choose which DOM representation Celerity sends to the viewers with the render option:
Celerity::Browser.new(:render => :html)
Celerity::Browser.new(:render => :xml)
This will send the output from either Browser#html or Browser#xml depending on the parameter passed.
Browser#html returns the initial HTML string that was received from the server, while Browser#xml returns HtmlUnit’s internal DOM representation (after JavaScript manipulation). The XML is usually a more accurate representation of the page state, but won’t always render correctly in a real browser.
You can specify the host/port used with the viewer option:
Celerity::Browser.new(:viewer => "10.0.0.3:1234")
It also lets you disable the functionality altogether:
Celerity::Browser.new(:viewer => false)
By default, the Browser will try to connect to “localhost:6429” when instantiated. If the connection fails, the browser will not raise any errors and not check again for the lifetime of the object (i.e. not until you create a new Browser).
The protocol used by the viewers can be used to implement services that will receive HTML dumps from Celerity each time the page changes (e.g. HTML validation, logging etc.). Please note that the service will be called on every page change, including minor JavaScript manipulations, so servers should take care to process the dumped HTML asynchronously.
Also note that you can listen for these changes yourself with Browser#add_checker. This is useful if you just need to work in the same process as the Browser instance, want to use another another protocol etc.
Celerity will open a TCP connection to the server and keep the connection open until Browser#close is called. The protocol is similar to HTTP, i.e.:
"Content-Length: «number of bytes»\n\n«json data»"
Example (can’t get GH to render this 100%):
Content-Length: 81
{"html":“…<\/html>”,“url”:“http:\/\/example.com”,“method”:"page_changed"}
So the server should read until it sees two newlines, parse the Content-Length header, then read that length. Currently the two methods called by Celerity are page_changed and save:
{"method":"save","path":"my_image.png"}
{"html":"<html>...<\/html>","url":"http:\/\/example.com","method":"page_changed"}