2011-07-06 95 views
12

我想使用Selenium自動執行一些Web任務(不用於測試)。我想我已經安裝了Selenium RC Server,但由於無法在PHP中找到客戶端驅動程序,因此無法編寫「測試腳本」(請參閱​​:http://seleniumhq.org/download/)。如何在PHP中使用Selenium?

有沒有辦法讓我在PHP中使用Selenium?這似乎表明我需要PHPUnit http://www.phpunit.de/manual/current/en/selenium.html。我只想自動完成一些任務,而不是參與一整套測試。

回答

11

嘗試下面的事情

  1. 獲取PHPUnit的安裝和工作
  2. 你的電腦上也有JAVA sdk & jre。
  3. 現在使用selenium IDE記錄測試用例。
  4. 將測試用例導出到php文件。
  5. 使用這些導出的函數創建一個測試用例庫。
  6. 創建從庫調用函數/測試的套件。
  7. 現在執行使用java命令啓動Selenium Server。
  8. 使用phpunit執行套件。

做參考如何寫這些文件,單擊here也嘗試git的樞紐

+1

[php-webdriver](https://github.com/facebook/php-webdriver)庫足夠簡單,使用[Steward](https://github.com)等工具可以更輕鬆地進行設置/ LMC-EU /管家)。我建議不要浪費時間使用Selenium IDE,只需在PHP中編寫所需的場景。 –

0

請檢查此鏈接: http://mvnrepository.com/artifact/org.seleniumhq.selenium.client-drivers

點擊硒的PHP客戶端驅動程序鏈接以獲得二進制

+0

這是一個JAR文件。我如何使它與PHP一起工作? – StackOverflowNewbie

+0

在硒中安裝jar ... –

+1

Marc - 我通常不會使用Java,所以我對這個JAR二進制有點困惑。我如何在Selenium中安裝這個JAR?那麼我如何才能從PHP運行Selenese腳本? – StackOverflowNewbie

1

您需要的硒服務器運行和網絡驅動程序庫與它進行交互。

正式硒不支持PHP,但在Nearsoft中,我們創建了一個庫來與Json Wire Protocol進行交互。我們的目標是儘可能地使其與來自官方網站的其他語言和驅動程序的示例相似,因此來自Java頁面的示例在php中的語法會非常類似。

檢查出來:https://github.com/Nearsoft/PHP-SeleniumClient

如果你喜歡它,分享它,涉足,叉,或請你做。

問候,馬克。

12

facebook/php-webdriver是一個很棒的客戶端硒和PHP。

您可以使用它來自動執行Web任務(如OP所需),或者您可以簡單地將php-webdriver集成到您的測試框架中。有一些項目已經提供了這一點:


安裝所有

  1. 下載並安裝facebook/php-webdrivercomposer require facebook/webdriver

  2. Download Selenium &啓動它。 java -jar selenium-server-standalone-#.jar

  3. Download Quick Java並將其放置到您的項目目錄中。


使用

在這個例子中,我們使用擴展quickjava禁用除了javascriptcookies一切。

這裏

查看更多偏好設置:這裏
https://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js

查看更多示例命令:
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