Wednesday, April 06, 2005

Using Selenium to test a Plone site (part 2)

In this post I'll talk about some Plone-specific features available in Selenium, such as setup, tearDown and postResults methods. See
verifyTextPresent Home page area that contains the items created and collected by ${username}
setVariable homepage_url '${base_url}/Members/${username}/index_html/document_view' click //a[@href='${homepage_url}']
verifyTextPresent This item does not have any body text, click the edit tab to change it.
setVariable edit_homepage_url '${base_url}/Members/${username}/index_html/document_edit_form' click //a[@href='${edit_homepage_url}']
verifyTextPresent Fill in the details of this document.
type text Hello World! click form.button.Save
verifyTextPresent Document changes saved
verifyTextPresent Hello World!
setVariable member_url '${base_url}/Members/${username}' open ${member_url}
verifyTextPresent Home page for ${username}
verifyTextPresent Hello World!
open ./selenium_ft_tool/tearDown?user=${username}

The test table starts by assigning a random value to the username, then it calls the setup method with the user argument to create that user. It continues by logging in into Plone via the "log in" form on the main Plone page, then it opens the "my folder" page, it goes to the user's home page and fills in "Hello, World!" as the text of the page. It finally saves the home page, then opens the Members/username Plone page to verify that the user's home page is there. Along the way, the test uses the verifyTextPresent assertion to check that expected text values are indeed present on the various pages that it opens. The last line in the test table uses the tearDown method to delete the newly-created user.

The Plone product version of Selenium ships with a test suite that contains one test table called testJoinPortal. The test suite file (called PloneTestSuite.html.dtml) and the testJoinPortal file (called testJoinPortal.html.dtml) are both in /var/lib/plone2/main/Products/Selenium/skins/ftests_browser_driven. I called my custom test table testEditHomePage.html.dtml and I edited PloneTestSuite.html.dtml to add a new row corresponding to testEditHomePage.html. I then restarted Plone and I went to http://www.example.com:8080/Plone/TestRunner.html?test=PloneTestSuite.html&auto=true.
The test suite got executed automatically (because auto was set to true), and postResults got called automatically at the end of the test suite run (also because auto was set to true).

The frame where the emebedded browser runs shows this at the end of the test suite run:

Results have been successfully posted to the server here:
/var/lib/plone2/main/Products/Selenium/skins/selenium_test_results


verifyTextPresent Home page area that contains the items created and collected by ${username}
setVariable homepage_url '${base_url}/Members/${username}/index_html/document_view' click //a[@href='${homepage_url}']
verifyTextPresent This item does not have any body text, click the edit tab to change it.
setVariable edit_homepage_url '${base_url}/Members/${username}/index_html/document_edit_form' click //a[@href='${edit_homepage_url}']
verifyTextPresent Fill in the details of this document.
type text Hello World! click form.button.Save
verifyTextPresent Document changes saved
verifyTextPresent Hello World!
setVariable member_url '${base_url}/Members/${username}' open ${member_url}
verifyTextPresent Home page for ${username}
verifyTextPresent Hello World!
open ./selenium_ft_tool/tearDown?user=${username}

Clicking on the selenium-results-metadata-Firefox.txt link shows the test total/pass/fail count
for the current test suite run:

totalTime: 7
numTestFailures: 0
numCommandPasses: 11
numCommandFailures: 0
numCommandErrors: 0
result: passed
numTestPasses: 2
Conclusion

The new Plone test tool added in the latest version of the Plone product implementation of Selenium provides valuable functionality for test management via the setup/tearDown/postResults methods. Even though these methods are Plone-specific, the ideas behind them should be easily adaptable to other Web applications. The only requirement is for the application to provide API hooks for user management functionality. This requirement is easily met by Plone, and I have to say I found it very easy to extend the Plone tool, even though I had no prior experience in writing Plone-specific code.

As a future Selenium enhancement, I'd like to see a way to specify 'SetUp' and 'TearDown' test tables at the test suite level that would automatically run at the beginning and at the end of each test suite run. In my example above, the log in process could be encapsulated in a SetUp table, and all other tables in the suite would then implicitly use the functionality in the SetUp table.

The alternative of course would be to extend the FunctionalTestTool and create a log_in method that would use the Plone API to log in the newly-created user. The current setup method in FunctionalTestTool could then get an extra parameter (such as login=true) that would call the log_in method if specified.

3 comments:

Anonymous said...

hi! great thanks! I just try to integrate Selenium into my site and this article is timely for me,thanks!

Anonymous said...

Thanks for article!

reggi said...

Big up, tnx, very good article for me, as new tester)

Modifying EC2 security groups via AWS Lambda functions

One task that comes up again and again is adding, removing or updating source CIDR blocks in various security groups in an EC2 infrastructur...