1

I am using PHPUnit and Selenium to test my web application.

At the moment I have 2 test classes - UserTest and PermissionsTest.

  • In UserTest I have methods which test that the program can successfully create a new user.
  • In PermissionsTest I turn certain permissions on and off and test the outcome.

For instance, I might turn the 'Create User' permission off and then test that the 'Create User' button is disabled. However, if I turn the 'Create User' permission back on, I want to test that it is possible to create a user.

All of the logic for being able to create a user is already in the UserTest class - so is there any way of running tests from the UserTest class from the PermissionsTest class?

At the moment I am trying the following code:

public function testUserPermission(){
  $userTest = new UserTest();
  if($this->hasPermission = true){
    $userTest->testCanCreateUser();
  }
}

However when I run this test, I get the error "There is currently no active session to execute the 'execute' command. You're probably trying to set some option in setup() with an incorrect setter name..."

Thanks!

3 Answers 3

3

It sounds to me like you're missing separation of your test implementation with its logic - I'm not talking about PHP issue but general test model.It will allow you to reuse your test components in various test cases.

You can take a look on some material about Page Objects in PHP here or general selenium wiki.

Sign up to request clarification or add additional context in comments.

Comments

1

The solution was as follows:

//instantiate and set up other test class
$userTest = new UserTest();
$userTest->setUpSessionStrategy($this::$browsers[0]);
$userTest->prepareSession();

//carry out a test from this class
$userTest->testCanCreateUser();

This works nicely. I can't see why using functionality from another test class is a bad idea in this case, because if I didn't do that I'd have to just rewrite that functionality into my new class, which seems less 'pure'...

5 Comments

How did you include the class? Was it autoloaded by PHPUnit or did it require a manual directive?
I used autoloading to include the "UserTest" class. Specifically I used composer to create an autoload file and included this file in the PHPUnit "bootstrap" file.
Thanks, do you know if something similar would work for Selenium 1? I just discovered that setUpSessionStrategy and prepareSession are Webdriver-specific commands only.
Sorry, I have no idea about Selenium 1 as I have never used it - I hope you find the answer!
I found the necessary commands to emulate what you did in your answer - thanks for the help!
1

For Selenium 1 (RC),

I made the following modifications instead (as well as applying the Page Object design pattern):

Specific Test class

//instantiate and set up other test class
$userTest = new UserTest($this->getSessionId());

//carry out a test from this class
$userTest->createUser();

//asserts as normal
$userTest->assertTextPresent();
...

Base Page Object class

class PageObject extends PHPUnit_Extensions_SeleniumTestCase {
    public function __construct($session_id) {
        parent::__construct();
        $this->setSessionId($session_id);
        $this->setBrowserUrl(BASE_URL);
    }
}

Specific Page Object class

class UserTest extends PageObject {

    public function createUser() {
        // Page action
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.