Last updated by mlathe 3 years ago
Selenium lets you easily test your web applications. It has a simple test syntax and small learning curve.

This plugin makes it easy to add Selenium tests to your application.

Please create issues in JIRA if you encounter problems.

Introduction

When you install this plugin, it will download the Selenium-Core distribution (currently version 0.8.3 - 1.5MB) and extract it into your grails application (into <grails project>/web-app/selenium/core). Now you simply create your Selenium tests - putting them in <grails project>/web-app/selenium/tests.

This plugin will automatically generate your test suite by listing all of the files in the selenium/tests directory. You can create tests as standard HTML as per the selenium documentation (and easily created using SeleniumIDE). Or, you can for-go the HTML for pipe delimited text files (*.psv) - this plugin will convert the pipe delimited files on the fly to HTML.

The suite page displays a list of all of the directories in the tests location. The default (top level) suite shows all tests in all directories. You can click anywhere in this suite hierarchy to load only those tests in that directory.

Once you have your tests in place, start your application and then the Selenium Test Runner:

  • at http://localhost:8080/<CONTEXT_PATH>/selenium/core/TestRunner.html?test=..%2F..%2Fselenium/suite
  • or use 'grails run-selenium'
NOTE: Using Selenium-core means that selenium and your tests are part of your web application - if you do not want your tests packaged in your WAR when using 'grails war' you must add this to your applications BuildConfig.groovy:
grails.war.resources = {stagingDir, args ->
    delete(dir: "${stagingDir}/selenium") 
}
This will remove the 'selenium' directory from the war - removing selenium-core and your tests. The SeleniumController class files are still included in your war - I haven't found a cleaner way to completely remove the plugin yet.

Example Use

To test-drive this plugin, you can follow this procedure (here is a shell script which does the same):

grails create-app selenium-test
cd selenium-test
grails install-plugin selenium
grails create-domain-class book
grails create-controller book
Now, modify the domain class and controller:
class Book {
    String title
    static constraints ={title(unique:true)}
}

class BookController { def scaffold = Book }

Now create the tests:

as HTML (grails create-selenium-test mytest.html)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test1</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td colspan="3">test1</td></tr>
</thead><tbody>
<tr><td>open</td><td>/selenium-test/</td><td></td></tr>
<tr><td>clickAndWait</td><td>link=BookController</td><td></td></tr>
<tr><td>clickAndWait</td><td>link=New Book</td><td></td></tr>
<tr><td>type</td><td>title</td><td>book1</td></tr>
<tr> <td>clickAndWait</td> <td>//input[@value='Create']</td> <td></td></tr>
<tr> <td>clickAndWait</td> <td>link=Book List</td> <td></td></tr>
<tr> <td>verifyTextPresent</td> <td>book1</td> <td></td></tr>
</tbody></table>
</body>
</html>
or as pipe delimited (grails create-selenium-test mytest.psv)
open|/selenium-test
clickAndWait|link=BookController
clickAndWait|link=New Book
type|title|book2
clickAndWait|//input[@value='Create']
clickAndWait|link=Book List
verifyTextPresent|book2
or as a GSP (grails create-selenium-test mytest.gsp)
<g:set var="bookTitle" value="book0d"/>
<sel:test name="MyTest">
<sel:row command="open" target="${request.contextPath}"/>
<sel:row line="clickAndWait|link=BookController"/>
<sel:row line="clickAndWait|link=New Book"/>
<sel:row command="type" target="title" value="${bookTitle}"/>
<sel:row line="clickAndWait|//input[@value='Create']"/>
<sel:row line="clickAndWait|link=Book List"/>
<sel:row line="verifyTextPresent|${bookTitle}"/>
</sel:test>
Now run your application with grails run-app and launch the test runner with 'grails run-selenium' or point your browser to http://localhost:8080/selenium-test/selenium/core/TestRunner.html?test=..%2F..%2Fselenium/suite?

At the top left, you'll see your test suite - on the right, you'll see the 'execute tests' where you can run the entire suite or just the selected test.

Scripts included with grails-selenium

run-selenium

Runs Selenium in the specified browser.

Specify the path to your browser as a command line parameter i.e. 'grails run-selenium /usr/bin/firefox' or, if the executable is on the path you would just need 'grails run-selenium firefox'

In your application.properties, you can specify:

  • selenium.auto=true
  • selenium.close=true
  • selenium.multiWindow=true
  • selenium.highlight=true
  • selenium.resultsUrl=/your/url/here (defaults to ${appContext}/selenium/postResults)
  • selenium.runInterval=1000
  • selenium.baseUrl=
See http://selenium.openqa.org/installing.html? (section titled Continuous Integration) for more information on selenium and continuous integration.

create-selenium-test

Generates a new empty selenium test.

Supply the path of the test you want to create, relative to 'web-app/selenium/tests'. The extension of this must be one of html, psv, gsp. The test file will be created using the syntax based on the file extension.

Example use:

grails create-selenium-test registration/errors/InvalidUsername.gsp
creates
web-app/selenium/tests/registration/errors/InvalidUsername.gsp

create-selenium-domain-test

Generates a new selenium test for the given domain class.

Supply the name of the domain class you want to test, followed by and extension that identifies the sytax to use. The extension of this must be one of html, psv, gsp.

Example use:

grails create-selenium-domain-test book.gsp
creates
web-app/selenium/tests/book.gsp

The generated test exercises the default grails scaffolding for CRUD functionality.

postResults page

A basic postResults page is provided, and if required the results can be saved to xml.

When using grails run-selenium, you can add properties to application.properties:

  • selenium.auto=true
  • selenium.resultsUrl=/your/url/here (defaults to ${appContext}/selenium/postResults)
  • selenium.saveXmlTo=/tmp/selenium.xml
By default resultsUrl is set as ${appContext}/selenium/postResults which is provided as a basic HTML implementation. Nothing fancy.

For more on Selenium

Reference

History

April 2010 grails-selenium-0.8 released to support grails-1.2.2