How to use Selenium with PHP?

孤者浪人 提交于 2019-11-27 03:59:41

问题


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.


回答1:


Try Following things

  1. Get Phpunit installed and working
  2. Also have JAVA sdk & jre on your pc.
  3. Now record test cases using selenium IDE.
  4. Export the testcases to php files.
  5. Using these exported functions create an library of test cases.
  6. Create suite which calls the functions/tests from library.
  7. Now to execute Start Selenium Server using java command.
  8. Using phpunit Execute the suite.

for refrence how to write these files click here and also try on git hub




回答2:


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



回答3:


You need the selenium server running and a web driver library to interact with it.

Officially selenium has no support for PHP but in Nearsoft we created a library to interact with the Json Wire Protocol. We aimed to make it as similar as possible to the examples from other languages and drivers from the official site so an example from the page in Java would have a very similar syntax in php.

Check it out: https://github.com/Nearsoft/PHP-SeleniumClient

If you like it, share it, get involved, fork it or do as you please.

Regards, Mark.




回答4:


Please check this link: http://mvnrepository.com/artifact/org.seleniumhq.selenium.client-drivers

Click on selenium-php-client-driver link to get the binary




回答5:


I think the guy asked mainly how to use IDE generated files.

There is a formater for PHP: You then just have to export as PHPunit.

Selenium IDE: PHP Formatters :: Add-ons for Firefox https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/



来源:https://stackoverflow.com/questions/6590360/how-to-use-selenium-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!