2

My question is quite simple. I'm coming from Python world, where it's very simple to execute Selenium testing code within a program, just writing something like:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.close()

When using PHP, things are getting harder: I wrote something like that

require 'vendor/autoload.php';

class MyTest extends PHPUnit_Extensions_Selenium2TestCase {

    public function setUp() {
        $this->setBrowser('Firefox');
        $this->setBrowserUrl('http://www.python.org');
    }

    public function testToto() {
        $this->url('/');
    }

}

...which kinda works when I execute phpunit MyTest.php.

But what I would like to do is to instanciate my test class in PHP code, and execute my Selenium commands "programmatically", like:

$myTest = new MyTest();
$myTest->testToto();

And here it sucks :(

PHP Fatal error: Uncaught exception 'PHPUnit_Extensions_Selenium2TestCase_Exception' with message 'There is currently no active session to execute the 'url' command.

So is there a way to execute Selenium code directly from PHP script without executing command line things with phpunit?

Edit: What am I trying to achieve? My project is to build a testing application which must be able to launch tests within a UI built by a end user thanks to a user friendly drag and drop builder (the user chooses which test he wants to execute first, then another, and so on). So I would like to avid ececuting phpunit commands with a ugly PHP exec: to me, the best option is to launch test case methods programmatically!

2 Answers 2

2

I think the pain comes from trying to use the PHPUnit Webdriver integration, without really using PHPUnit.

You can write code like your Python example by using a standalone Webdriver implementation (that does not need PHPUnit). I recommend the one written by Facebook:

https://github.com/facebook/php-webdriver

but there are some more:

http://docs.seleniumhq.org/docs/03_webdriver.jsp#php

You can also use these implementations inside PHPUnit tests. I do that as I don't like the PHPUnit Webdriver implementation.

With these it's trivial to write your example in PHP.

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

Comments

1

Well, a very nice question first of all. The short answer is yes you can, but it's too much pain. PHPUnit is just a modestly complicated, huge, scary and amazing library with a gadzillion of extensions. In the nutshell it reads the configuration, finds the tests, and runs them.

You can put a break point inside your test and trace to the top what it does, what parameters it accepts and literally simulate the whole thing. That would be the "proper" and crazy way, and the most complex too.

The simpler way would be by finding out what the test case class needs in order to run (break point & trace are always your best friends), in this particular case it turned out to be just this:

$myTest = new MyTest();
$myTest->setUp();           // Your setup will always be called prior the test.
$myTest->prepareSession();  // Specific to Selenium test case, called from `runTest` method.
$myTest->testToto();

But, even in PHPUnit_Extensions_Selenium2TestCase there is a lot of stuff that are not publicly accessible and it feels just a strike of luck. But you get the idea. Besides, simply calling a method of a test case class will result in two things: nothing happens, or you get an exception. All the fancy result tracing happens higher in the hierarchy.

I can only guess what you are trying to achieve, but probably if you ask the question about the actual problem we'd be able to help more.

Edit

exec might seem ugly indeed, but it's there for a very good reason: process isolation. There are situations when one piece of the code that is being tested changes the environment and it becomes conflicting with another piece of code, e.g., session-related, sent headers, etc. When you come across one of them, you will be praying on exec.

In your case, the easiest would be to launch the PHPUnit from the command line, but you might need to write a custom formatter to get the data in the necessary format out of it, unless you are happy with the existing ones.

Another option would be to use the the existing client for the WebDriver / Selenium and simply send commands directly to the Selenium server, I assume that's what you really need? You can find out the piece of code responsible for that in the PHPUnit extension or there's another cool project called Behat (and Mink). I believe their client is in the Behat/MinkSelenium2Driver repository. And if you don't like those, I'm sure there are other php wrappers you can find on the github, or can create your own using the existing ones as an example.

PS: Share a link to the project when it's up and running if its public.

2 Comments

Thank you very much Ian for your detailed and usefull answer. I edited my initial post to add a description of what I'm actually trying to achieve.
In addition, a precision: I did manage to do the job thanks to Selenium RC (PHPUnit_Extensions_SeleniumTestCase vs. PHPUnit_Extensions_Selenium2TestCase). Too bad, PHPUnit_Extensions_SeleniumTestCase is deprecated...

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.