DavidWinterbottom
DavidWinterbottom

Reputation: 6540

How to skip tests in PHPUnit if Selenium server is not running?

I want to add a suite of Selenium tests as part of a global PHPUnit test suite for an application. I have hooked the suite of Selenium tests into the global AllTests.php file and everything runs fine whilst the Selenium server is running.

However, I would like the script to skip the Selnium tests if the Selenium server isn't running so other developers aren't forced to install Selenium server in order for the tests to run. I would normally try to connect within the setUp method of each testcase and mark the tests as skipped if this failed, but this seems to throw a RuntimeException with message:

The response from the Selenium RC server is invalid: ERROR Server Exception: sessionId should not be null; has this session been started yet?

Does anyone have a method for marking the Selenium tests as skipped in this scenario?

Upvotes: 4

Views: 2427

Answers (3)

kta
kta

Reputation: 20110

You can try skipWithNoServerRunning() For more information follow this link

Upvotes: 0

Bob Gregor
Bob Gregor

Reputation: 1161

My preferred selenium / PHPUnit Configuration:

Maintaining integration (selenium) tests can be a lot of work. I use the firefox selenium IDE for developing test cases, which doesn't support exporting test suites to PHPUnit, and only supports individual test cases. As such - if I had to maintain even 5 tests, that'd be a lot of manual work to re-PHPUnit them every time they needed to be updated. That is why I setup PHPUnit to use Selenium IDE's HTML Test files! They can be reloaded & reused between PHPUnit & selenium IDE

<?php 
class RunSeleniumTests extends PHPUnit_Extensions_SeleniumTestCase {
    protected $captureScreenshotOnFailure = true;
    protected $screenshotPath = 'build/screenshots';
    protected $screenshotUrl = "http://localhost/site-under-test/build/screenshots";
    //This is where the magic happens! PHPUnit will parse all "selenese" *.html files
    public static $seleneseDirectory = 'tests/selenium';
    protected function setUp() {
            parent::setUp();
            $selenium_running = false;
            $fp = @fsockopen('localhost', 4444);
            if ($fp !== false) {
                    $selenium_running = true;
                    fclose($fp);
            }
            if (! $selenium_running)
                $this->markTestSkipped('Please start selenium server');

            //OK to run tests
            $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://localhost/");
    $this->setSpeed(0);
    $this->start();
            //Setup each test case to be logged into WordPress
            $this->open('/site-under-test/wp-login.php');
            $this->type('id=user_login', 'admin');
            $this->type('id=user_pass', '1234');
            $this->click('id=wp-submit');
            $this->waitForPageToLoad();
    }
    //No need to write separate tests here - PHPUnit runs them all from the Selenese files stored in the $seleneseDirectory above!
} ?>

Upvotes: 0

Anti Veeranna
Anti Veeranna

Reputation: 11583

You could use test dependencies that were introduced in PHPUnit 3.4.

Basically

  1. write a test that checks whether Selenium is up.
  2. If not, call $this->markTestAsSkipped().
  3. Make all your selenium requiring tests depend on this one.

Upvotes: 2

Related Questions