Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Selenium with PHP?

I'd like to use Selenium to automate a few web tasks (not for testing). I think I have Selenium RC Server installed, but have no way of writing "test scripts" since I can't find a client driver in PHP (see: http://seleniumhq.org/download/).

Is there a way for me to use Selenium with PHP? This seems to suggest I need PHPUnit http://www.phpunit.de/manual/current/en/selenium.html. I just want to automate a few tasks, not get involved with a full suite of testing.

like image 809
StackOverflowNewbie Avatar asked Jul 06 '11 00:07

StackOverflowNewbie


People also ask

Is C++ supported by Selenium?

You can use selenium server and JsonWireProtocol. In C++ you can implement CURL requests to selenium server and do web automation with C++. Use this link first: My fork of Webdriver++.


1 Answers

facebook/php-webdriver is an awesome client for selenium and php.

You can use it to automate web tasks (as the OP wanted), or you can simply integrate php-webdriver to your testing framework. There are some project already providing this:

  • Steward integrates php-webdriver directly to PHPUnit.
  • Codeception testing framework provides BDD-layer on top of php-webdriver.
  • You can also check out this blogpost + demo project, describing custom PHPUnit integration.

Install Everything

  1. Download and install facebook/php-webdriver. composer require facebook/webdriver

  2. Download Selenium & Start it. java -jar selenium-server-standalone-#.jar

  3. Download Quick Java and place it into your project directory.


Usage

In this example, we use the extension quickjava to disable everything except javascript and cookies.

View more preference settings here:
https://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js

View more example commands here:
https://github.com/facebook/php-webdriver/wiki/Example-command-reference

use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

// Change this to the path of you xpi
$extensionPath = $this->container->getParameter('kernel.root_dir').'/../bin/selenium/quickjava-2.0.6-fx.xpi';

// Build our firefox profile
$profile = new FirefoxProfile();
$profile->addExtension($extensionPath);
$profile->setPreference('thatoneguydotnet.QuickJava.curVersion', '2.0.6.1');
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Images', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.CSS', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Cookies', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Flash', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Java', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.JavaScript', 2);
$profile->setPreference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2);

// Create DC + Driver
$dc = DesiredCapabilities::firefox();
$dc->setCapability(FirefoxDriver::PROFILE, $profile);

$driver = RemoteWebDriver::create($host, $dc);
$driver->get('http://stackoverflow.com');

// Do stuff - https://github.com/facebook/php-webdriver/wiki/Example-command-reference
//$driver->findElement(WebDriverBy::id("element-id"));

// The HTML Source code
$html = $driver->getPageSource();

// Firefox should be open and you can see no images or css was loaded
like image 175
Anil Avatar answered Sep 23 '22 02:09

Anil