Preview of a new Selenium test suite

Published on and tagged with cakephp  selenium  testing

Some time ago I wrote about using Selenium with SimpleTest. It is a nice solution, but after working with it I had to experience that it doesn’t fit to my working style. I often forgot to start the Selenium server before running the tests, and as it is rather slow, it is not the ideal tool for doing TDD ;-)

So I had to go back to the Selenium helper. But somehow it felt no longer right to place the tests in app/views/pages/tests and to write them as thtml files. And so I wrote the Selenium test suite, which uses a different approach as you will see.

Let’s have a look at a simple example. Before we can start with the example, we have to install the test suite (please be aware that the test suite only works with the not yet released CakePHP 1.2):

  • Download the Zip file from the download section
  • Unpack the Zip file
  • Add the following route to app/config/routes.php:
    Router::connect('/selenium/*', array('controller' => 'selenium', 'action' => 'display'));

With that we are ready to start with the example. As the tests are organized in test suites, we start with such a test suite:

// app/tests/selenium/my_test_suite.php
class MyTestSuite extends SeleniumTestSuite {
    var $title = 'My tests';
		
    function execute() {
        $this->addTestCase('Login', 'cases/LoginTest');
    }
}

Each file placed in app/tests/selenium is considered to be a test suite, and must extend the class SeleniumTestSuite and implement the execute() function. This function is very simple: you just add the tests to the test suite.

The next step is to create our test case. A test case must extend SeleniumTestCase and, as a test suite, implement the execute() function. This function contains the test logic.

// app/tests/selenium/cases/login_test.php
class LoginTest extends SeleniumTestCase {
    var $title = 'Login';
    var $fixtures = array('Users');

    function setUp() {
        echo 'setUp';
    }

    function tearDown() {
        echo 'tearDown';
    }

    function execute() {
        $this->open('http://myproject.localhost');
        $this->type('name=data[User][username]', $this->dho['username']);
        $this->type('name=data[User][password]', $this->dho['password']);
        $this->clickAndWait('submit');
        $this->verifyLocation('http://myproject.localhost/projects');
    }
}

You probably noticed the $fixtures array. It allows you to write some records to the database before the test case is executed. These records are also available in the test case (see $this->dho). Each fixture is a simple class with the same name as the database table:

// app/tests/fixtures/users.php
class Users {
    var $columns = array('id', 'username', 'password');
    var $dho = array(1, 'dho', 'geheim');
}

With that we are ready to execute the test suite. As I use the Selenium IDE, I just have to enter the following url in Firefox to load the test suite:

chrome://selenium-ide/content/selenium/TestRunner.html?test=http://myproject.localhost/selenium/MyTestSuite

And the tests can be executed :)

Please keep in mind that it is just a preview, so it is possible that some things will change in future versions. As always your feedback is welcome :)

72 comments baked

  • vamsi

    please provide the script
    how to verify any field value is existed in the database or not

  • cakebaker

    @mila: Hm, maybe this thread helps.

    @gayathri, vamsi: I’m sorry, but I don’t understand what you are trying to do…

  • Abhimanyu Grover

    @Daniel: Are all files in Downloads section are updated?

    I find a lot of things missing after installing this.

  • cakebaker

    @Abhimanyu: The Selenium helper is up-to-date, and I’m working on an update of the test suite.

  • Ramesh

    Can you provide the steps how we can get teh Captcha information in environement so that i can use it in the Code. Or tell me how we can ignore the Captcha, without failing the test.

  • Ramesh

    Can you provide the code how to ignore the Captcha from teh registration and run the test successfully, registed successfully

  • cakebaker

    @Ramesh: I’m sorry, but I can’t help you as I don’t have any experience with testing captchas. See the comments above where I described two possible approaches to test them.

    Hope that’s a starting point for you!

  • Ramesh

    Thanks for response

    But the above Appraochs are good, but how to implement is problem. How to ignore the Captcha and make my registration successfull and proceed. Or how to capture the text in Captcha, Can any one Help me out.

    Thanks in advance

  • cakebaker

    @Ramesh: Well, one idea is to modify your captcha verification method, so that it accepts any input when you run your application in the testing environment.

    Hope that helps!

  • Ramesh

    Thanks a lot

  • Freenty

    My IE not work.. after command “open” all stoped.. What is the reason, you know?

  • cakebaker

    @Freenty: Hm, any errors? And does it work with Firefox?

  • Seleniumfan

    Guys,
    I am relatively new to selenium. need help in the following areas:
    1. I have a PHP application (SUGAR CRM) which needs to be automated.
    2. I have created some basic login logout scripts using IDE and exported them as PHP test cases.
    3. individually i can run these test cases using the Phpunit command and they work fine.
    4. I installed selenium Grid on my machine and was able to start the hub and RC on different machines.
    I want to run these scripts as a test suite using grid, such that all the scripts mentioned in the suite are executed one after the other. I am not sure how to do this.

    Secondly i also tried using bromine, but there too it executes only one test case from a requirement.

    Any help is highly appreciated.

  • cakebaker

    @Seleniumfan: I’m sorry, but I can’t help you with Selenium Grid as I never used it so far. Maybe you will have more luck in the Selenium Users Google Group?

  • Geeta

    Hello,

    I am exporting all my testcases in PHP format from Selenium IDE and running the test case in NetBeans combined with PHPunit.

    I want to create a test suite to be able to run a batch of test cases.

    Can you please help out with what functions and classes i need to call or give me a sample?

    I dont see any SeleniumTestSuite.php in the extensions folder of PHPunit.

    Which file to include in the testsuite.php?

    Thanks in advance.

    Geeta

  • cakebaker

    @Geeta: I’m sorry, but I don’t have any experience with PHPunit. Maybe the chapter Organizing Tests in the PHPunit documentation is helpful for you.

  • Neeta

    Hello, I have one scenario. Please tell me if it can be automated. If yes then how it can be done.

    Scenario: I have s shopping site. I search for some products. Now my application allows me to sort the items displayed by price, brand etc.
    So I want to know how I can validate the process of sorting, if it has been done correctly or not. Please help!

  • cakebaker

    @Neeta: Sure, it’s possible to test such a scenario. You could use assertText() to verify whether the results are shown in the correct order.

    Hope this helps!

  • Henn0r

    Hey Guys,

    I have trouble getting my Selenium TestCase working using a HTMLTestSuite.

    I am trying to use a Testsuite, which inhabits many TestCases like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
      <title>Test Suite</title>
    </head>
    <body>
    <table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"><tbody>
    <tr><td><b>Test Suite</b></td></tr>
    <tr><td><a href="case/test1.html">test1</a></td></tr>
    <tr><td><a href="case/test2.html">test2</a></td></tr>
    <tr><td><a href="case/test3.html">test3</a></td></tr>
    <tr><td><a href="case/test4.html">test4</a></td></tr>
    </tbody></table>
    </body>
    </html>

    Now I want to use this Test Suite with the ‘PHPUnit_Extensions_SeleniumTestCase’ extension from PHPUnit. Here is what I’ve set up so far.

    <?php
    require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
    
    class WebTest extends PHPUnit_Extensions_SeleniumTestCase {
    
        protected function setUp()
        {
            $this->setBrowser("*firefox");
            $this->setBrowserUrl('http://myUrl');
        }
    
        public function testSelenese()
        {
            $filename = 'PathToSuite.html';
            $this->runSelense($filename);
        }
    }
    ?>

    That ain’t working this way.. I know that this is going to work if i just use one specific test and not a Test Suite, but that is not what I’ve been looking for. I need to use this Test Suite…

    Does anyone got a solution for this?

    Greatings
    Henn0r

  • cakebaker

    @Henn0r: Hm, I don’t know whether this is possible. However, did you see the $seleneseDirectory property?

  • Henn0r

    I’ve tried the selenseDirectory property, but there is a problem using this… The Testcases need to be played in an order, they should not be played randomly, thats why i’ve got the Testsuite (like Testcase1 -> Login, Testcase2 -> do something in Webpage, Testcase3 -> Lasts steps and Ending).

    Anyway those propertys never worked in all of my Testclasses in PHP. I’ve got no Idea why, but thats how it is… :-(

    Do you have any other Idea?

    I’ve got now this solution, but i’am not very happy with it…

    public function testPHPUnitSuite()
        {
            PHPUnit_Extensions_SeleniumTestCase::shareSession(true);
            $suite = new PHPUnit_Framework_TestSuite('LoginTest');
            $suite->addTestSuite('do1Test');
            $suite->addTestSuite('do2Test');
            $suite->addTestSuite('lastStepTest');
            $suite->run();
        }

    Greets
    Henn0r

  • cakebaker

    @Henn0r: No, I’m sorry, I don’t have any other idea… Maybe someone on the #phpunit IRC channel could provide some idea?

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License