MxBlog

Howto: test Mendix apps using the Robot Framework

The Robot framework is a test framework which allows you to create test suites through simple test tables in normal text files. Tests can target all sorts of functionality, including functional user interface tests of webapplications. To do this you can combine the Robot framework with Selenium.

One very interesting feature of the Robot Framework is the fact that you can combine test steps into reusable keywords, so you can easily reuse these steps in multiple test cases.

An easy way to get started with the Robot framework is by running it from a maven project:

The following example tests a simple Mendix application, consisting of 1 entity, 1 overview page and 1 edit page.

The overview page looks like this:

Products Overview

And the edit page looks like this:

![Products NewEdit][5]

In IntelliJ i created a new Maven project:

![Maven project][6]

The pom.xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mendix</groupId>
    <artifactId>RobotExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.robotframework</groupId>
                <artifactId>robotframework-maven-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.github.markusbernhardt</groupId>
                        <artifactId>robotframework-selenium2library-java</artifactId>
                        <version>1.4.0.6</version>
                        <scope>compile</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Now you have all you need to start writing tests.

The following acceptance test, seleniumtest.txt, adds a new product and removes it.

*** Settings ***
Suite Setup       Open Page
Suite Teardown    Close Browser
Library           Selenium2Library
Library           String

*** Variables ***
${URL Application}              http://localhost:8080/

*** Keywords ***
Open Page
    Open Browser                        ${URL Application}          Firefox     mainbrowser
    Log System Info
    Log Remote Session Id
    Log Remote Capabilities
Add New Product
    [Arguments]                         ${p_EAN}    ${p_Name}   ${p_Description}
    Click Button                        xpath=//button[text()='New']
    Log                                 Entering new product
    Wait Until Page Contains Element    xpath=//input[ancestor::tr/th/label='EAN']
    Input Text                          xpath=//input[ancestor::tr/th/label='EAN']              ${p_EAN}
    Input Text                          xpath=//input[ancestor::tr/th/label='Name']             ${p_Name}
    Input Text                          xpath=//input[ancestor::tr/th/label='Description']      ${p_Description}
    Element Should Not Be Visible       css=div.alert-error
    Click Button                        xpath=//button[text()='Save']

*** Test Cases ***
Should be able to add and remove a new product
    ${dateString}                       Get Time                                                epoch           NOW
    ${bookEAN}                          Generate Random String                                  8               [NUMBERS]
    ${bookString}                       Generate Random String                                  10              [LETTERS]
    ${bookName}                         Catenate                                                ${dateString}   ${bookString}
    ${bookDescription}                  Catenate                                                ${dateString}   ${bookString}   description
    Go To                               ${URL Application}
    Wait Until Page Contains Element    xpath=//button[text()='New']
    Add New Product                     ${bookEAN}                                              ${bookString}   ${bookDescription}
    Wait Until Page Contains Element    xpath=//button[text()='New']
    Table Should Contain                css=table.mx-datagrid-body-table                        ${bookEAN}

[6]: