How to setup Selenium (WebDriver and Server) with PHP bindings

15,061

Solution 1

To get this running locally, follow the instructions here:

https://github.com/facebook/php-webdriver#getting-started

Here is a sample PHP webdriver script that you can use. It will open firefox, take you to google's page and submit a search query:

    // you'll need to modify this path so it points to the composer autoloader
    require_once __DIR__ . '/vendor/autoload.php';

    /**
     * since I'm running the selenium jar locally, this is all I need.
     * I just run it in the background and my php scripts connect to it and
     * the tests
     */ 
    $host = 'http://localhost:4444/wd/hub';

    $driver = RemoteWebDriver::create($host, DesiredCapabilities::firefox());

    $driver->get('http://google.com');
    $element = $driver->findElement(WebDriverBy::name('q'));
    $element->sendKeys('Cheese');
    $element->submit();

Is this the sort of detail you're looking for?

Solution 2

If you a visual learner like me, this

Youtube Video

Selenium WebDriver set up with PHP - Selenium PHP traininig

will help you do that. :)

Share:
15,061

Related videos on Youtube

Marc Baas
Author by

Marc Baas

Updated on June 05, 2022

Comments

  • Marc Baas
    Marc Baas about 2 years

    I'm trying to find specific information on how to setup Selenium with PHP bindings in a 'client-server' type of setup. I'm not even sure if what I want is possible, but I will try to do my best to describe the objective of what I am trying to achieve.

    I do QA on a Web development project, where we are working with distributed team members. We need automated front end testing, and have decided that (due to a number of factors) Selenium makes the best candidate for the job. Our team is specialized in PHP, so it makes sense to use Selenium with PHP bindings.

    My biggest challenge is:

    1) How do I install those PHP bindings?

    2) How do I create and execute a Selenium script in PHP? This one might seem obvious, but I need to know if I need to create some sort of 'project' in PHP, or whether this requires different steps. Manuals are very clear and detailed when it concerns the default JAVA bindings, but hopelessly lacking on the PHP bindings.

    3) How do I do all this, while wanting to invoke a test from a client, but having it executed by a 'server/VM'? (Keeping in mind that if the possibility were there, I would also like to be able to create tests on the server, that can execute/invoke testing activities on the desktop of the client.)

    4) How do I setup a server that meets all requirements to run Selenium Server with PHP bindings?

    The objective is to be able to initially create a VM (probably a Vagrant box) that would contain Selenium Server (and if needed other components) with the actual test scripts, which can be shared among team members. This VM should both be able to execute headless tests, but ideally should also be able to drive tests on the host (if at all possible).

    Technically it should support the scenario where QA finds an issue in the product, and should be able to just specify the required script to reproduce it. The developer that has the task to fix the issue should only have to run the script on his machine to actually reproduce the found error.

    Eventually we would want to migrate the VM to an actual server, hence the reason we want to set it up like this from the start. This will keep things more simple once we are ready to move to a physical server.

    I've been looking all over the internet for detailed documentation, but in just about any documentation many assumptions are made about already configured and set up environments. I really need a step by step explanation of how to set things up.

    PHPUnit seems a bit of a weird choice to pair with Selenium, since they both cover completely different areas of testing. I have seen (again incomplete) instructions on the PHPUnit site, but that seems very clunky and our development team is not very keen on this setup. We have people suggesting Jenkins, but I personally do not see how Jenkins would eliminate the normal setup of Selenium, which one has to go through from the start anyway.

    I already have Selenium Server running as a service in a VM, I just need to know what else I need, and how I need to set it up, how to configure it. how to make things communicate, etc.

    Any help/ideas would be highly appreciated.

  • Marc Baas
    Marc Baas over 9 years
    Hi ymas, I need a moment to check this out, but it does show some stuff I was looking for. I will get back on this one for sure though.
  • Marc Baas
    Marc Baas over 9 years
    Well, I have gone through some extensive testing and trying out to get webdriver to work with chromedriver and xvfb for a headless setup. Your example does seem to work, but somehow I am getting (what looks like) web page output in my console when running my test. I have to say that it is an immense challenge to get all components setup properly so that it all works together, and I find it really sad that there is no single complete instruction set on how to set things up from start to finish. Thanks again for your answer ymas. It was not 'the solution' but did get me going.