Selenium Webdriver for Erlang Quick Start

written in Erlang

Selenium is the industry standard for automated testing of web applications. Together with Webdriver, a ‘remote control’ API for all major browsers, it enables you to create robust integration test for the browser.

The great people of QuviQ, creators of the unique Quickcheck test framework, created an Erlang Webdriver client implementation (Github repository).

It’s trivial to get started with the following steps:

Step 1: Add webdrv to the rebar.config of your project

Open rebar.config in your favorite editor, and make sure webdrv is listed as dependency. I use a fork of the original repository that support rebar:

1
2
3
{deps, [
    {webdrv, "", {git, "https://github.com/ehedenst/webdrv.git", {branch, "master"}}},
       ]}.

Step 2: Get and compile webdrv

Go to the root of your Erlang project and execute:

1
$ rebar get-deps compile

Step 3: Get & start the Google chromedriver

For this quick start we will be using the Google Chromedriver. Get the right package for your environment here. I’m now on a Mac, so:

1
2
3
$ curl -O http://chromedriver.storage.googleapis.com/2.9/chromedriver_mac32.zip
$ unzip chromedriver_mac32.zip
$ ./chromedriver

The last line starts up the Chromedriver server and if all went well, you should get the following output:

1
Starting ChromeDriver (v2.9.248307) on port 9515

Important! This server needs to be running during test execution.

Step 4: Your first Erlang webdrvr test!

Save the following module in src/random_org_test.erl. In this test we open a page, fill in a form, submit the form, and check if an expected piece of text is indeed present in the response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-module(random_org_test).

-compile(export_all).

-include_lib("webdrv/include/webdrv.hrl").

-define(CHROMEDRIVER, "http://localhost:9515/").

test() ->
    {ok, _Pid} = webdrv_session:start_session(test, ?CHROMEDRIVER,  webdrv_cap:default_chrome(), 10000),
    webdrv_session:set_url(test, "http://www.random.org/integers/"),
    {ok, Emin} = webdrv_session:find_element(test, "name", "min"),
    webdrv_session:clear_element(test, Emin),
    webdrv_session:send_value(test, Emin, "5"),
    {ok, Emax} = webdrv_session:find_element(test, "name", "max"),
    webdrv_session:clear_element(test, Emax),
    webdrv_session:send_value(test, Emax, "15"),
    webdrv_session:submit(test, Emax),
    {ok, PageSource} = webdrv_session:get_page_source(test),
    string:str(PageSource, "Here are your random numbers") > 0,
    webdrv_session:stop_session(test).

Step 5: Run the test

Run your test by opening up the Erlang shell..

1
$ erl -pa ebin deps/*/ebin

..and execute the test function

1
1> random_org_test:test().

You should see the Chrome browser opening in the background, quickly flashing some pages, closing, and on the Erlang shell the anticlamatic output ok.

Further information


Comments