2011-09-07 162 views
9

Selenium2默認情況下以新配置文件啓動Firefox。我喜歡這種默認配置,但出於一些很好的原因(訪問我的書籤,保存的密碼,使用我的加載項等)我想從我的默認配置文件開始。Selenium2 firefox:使用默認配置文件

supposed to be一個屬性控制這個,但我認爲文檔與源不同步,因爲據我所知webdriver.firefox.bin是唯一有效的。例如。啓動硒:

java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin=not-there 

工程(即它抱怨)。但這並沒有影響:

java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.profile=default 

(「默認」是profiles.ini的名字,但我也試圖與「PROFILE0」,這是在profiles.ini節的名稱)。

我使用PHPWebdriver(使用JsonWireProtocol)訪問:

$webdriver = new WebDriver("localhost", "4444"); 

$webdriver->connect("firefox"); 

我嘗試從PHP側做:

$webdriver->connect("firefox","",array('profile'=>'default')); 

或:

$webdriver->connect("firefox","",array('profile'=>'Profile0')); 

與沒有成功(Firefox啓動,但沒有使用我的配置文件)。

我也嘗試創建一個批處理文件的黑客的做法:用

#!/bin/bash 
/usr/bin/firefox -P default 

然後開始硒: Java的罐子硒的服務器獨立-2.5.0.jar -Dwebdriver.firefox.bin =「/ usr/local/src/selenium/myfirefox」

Firefox啓動,但沒有使用默認配置文件,更糟的是,一切都掛起:硒啓動時似乎無法與firefox進行通信。

P.S.我看到Selenium - Custom Firefox profile我嘗試這樣做:

java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate "not-there" 

,拒不執行!興奮,想着我可能會做點什麼,我試過了:

java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate /path/to/0abczyxw.default/ 

這沒什麼用。即它仍然:-(

回答

8

西蒙斯圖爾特answered this on the mailing list對我來說。

總結他的回覆:你把你的firefox檔案,拉鍊(zip,不是tgz),base64編碼,然後發送整個東西作爲/session json request(把base64字符串放在Capabilities的firefox_profile鍵中目的)。

一個例子辦法做到這一點在Linux上:

cd /your/profile 
zip -r profile * 
base64 profile.zip > profile.zip.b64 

,然後如果你正在連接時,請務必使用PHPWebDriver:

$webdriver->connect("firefox", "", array("firefox_profile" => file_get_contents("/your/profile/profile.zip.b64"))) 

注意:它仍然不會成爲我真實的個人資料,而不是它的副本。所以書籤不會被記住,緩存不會被填滿等。

+0

謝謝,這真的很有幫助。如果PHP代碼壓縮配置文件然後編碼爲base64,那麼這樣做會很好,因此人們可以在PHP中執行所有操作。 – David

+0

重新確認已經陳述的內容......這是要走的路。當我們想通過代理來管理所有的硒請求時,我們的工作完美無缺。 –

5

這裏新的配置文件開始就是Java中,我相信有類似的東西可以在PHP。

ProfilesIni profile = new ProfilesIni(); 
FirefoxProfile ffprofile = profile.getProfile("default"); 
WebDriver driver = new FirefoxDriver(ffprofile); 

如果要產生額外的擴展,你可以做這樣的事情爲好。

ProfilesIni profile = new ProfilesIni(); 
FirefoxProfile ffprofile = profile.getProfile("default"); 
ffprofile.addExtension(new File("path/to/my/firebug.xpi")); 
WebDriver driver = new FirefoxDriver(ffprofile); 
+0

謝謝@nilesh。我正確地認爲這是直接使用WebDriver;你沒有啓動'selenium-server-standalone.jar',因此不使用JsonWireProtocol? –

+0

這是直接使用webdriver。我不知道你的意思是從獨立的罐子開始。 WebDriver本身使用JsonWireProtocol來溝通http://code.google.com/p/selenium/wiki/JsonWireProtocol – nilesh

+0

啊,有趣。所以你的上面的java代碼片段變成了JSON和Web服務調用?如果我能夠追蹤這些調用的內容,那麼我可以在PHP庫中實現它。但是無論它在做什麼都沒有記錄在你提供的URL上,我還沒有看到任何其他的JsonWireProtocol文檔。我想我會聯繫硒開發人員。 –

1

我很好奇這個問題,以及和我得到了什麼工作是很簡單的。 我使用命令/Applications/Firefox.app/Contents/MacOS/firefox-bin -P,彈出文件管理器。之後,我發現我需要用我用的配置文件下面的代碼t o激活簡介browser = Selenium::WebDriver.for :firefox, :profile => "batman"

這拉了我所有與該配置文件關聯的書籤和插件。

希望這會有所幫助。

+0

謝謝@Curtis。該命令使用哪種語言/庫? –

+0

@達倫:語言是紅寶石。但我相信PHP有一個等價物。 –

1

從我的理解,不可能使用-Dwebdriver.firefox.profile=<name>命令行參數,因爲它不會在您的用例中考慮到,因爲目前的代碼設計。由於我面臨同樣的問題,並且每次創建新會話時都不想上傳配置文件目錄,因此我實現了this patch,該參數引入了一個新的firefox_profile_name參數,該參數可用於JSON功能,以針對特定的Firefox配置文件遠程服務器。希望這可以幫助。

+0

很棒@Stéphane - 如果我已經理解了你的補丁,那正是我以前的樣子。我還沒有測試過,但安全嗎? (即在整潔階段硒會刪除它創建的臨時配置文件目錄;您是否需要明確的代碼來阻止這種情況發生?) –

+0

是的,它是安全的。 Selenium實際上會製作此配置文件的臨時副本並使用它(如模板)。 –

1

我做到了,在Zend的是這樣的:

public function indexAction(){ 
    $appdata = 'C:\Users\randomname\AppData\Roaming\Mozilla\Firefox' . "\\"; 
    $temp = 'C:\Temp\\'; 
    $hash = md5(rand(0, 999999999999999999)); 
    if(!isset($this->params['p'])){ 
     shell_exec("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\" -CreateProfile " . $hash); 
    }else{ 
     $hash = $this->params['p']; 
    } 
    $ini = new Zend_Config_Ini('C:\Users\randomname\AppData\Roaming\Mozilla\Firefox\profiles.ini'); 
    $path = false; 
    foreach ($ini as $key => $value){ 
     if(isset($value->Name) && $value->Name == $hash){ 
      $path = $value->Path; 
      break; 
     } 
    } 
    if($path === false){ 
     die('<pre>No profile found with name: ' . $hash); 
    } 
    echo "<pre>Profile : $hash \nProfile Path : " . $appdata . "$path \n"; 
    echo "Files: \n"; 
    $filesAndDirs = $this->getAllFiles($appdata . $path); 
    $files = $filesAndDirs[0]; 
    foreach ($files as $file){ 
     echo " $file\n"; 
    } 
    echo "Dirs : \n"; 
    $dirs = array_reverse($filesAndDirs[1]); 
    foreach ($dirs as $dir){ 
     echo " $dir\n"; 
    } 
    echo 'Zipping : '; 
    $zip = new ZipArchive(); 
    $zipPath = md5($path) . ".temp.zip"; 
    $zipRet = $zip->open($temp .$zipPath, ZipArchive::CREATE); 
    echo ($zipRet === true)?"Succes\n":"Error $zipRet\n"; 
    echo "Zip name : $zipPath\n"; 
    foreach ($dirs as $dir){ 
     $zipRet = $zip->addEmptyDir($dir); 
     if(!($zipRet === true)){ 
      echo "Error creating folder: $dir\n"; 
     } 
    } 
    foreach ($files as $file){ 
     $zipRet = $zip->addFile($appdata . $path ."\\". $file,$file); 
     if(!($zipRet === true && file_exists($appdata . $path . "\\". $file) && is_readable($appdata . $path . "\\". $file))){ 
      echo "Error zipping file: $appdata$path/$file\n"; 
     } 
    } 
    $zipRet = $zip->addFile($appdata . $path ."\\prefs.js",'user.js'); 
    if(!($zipRet === true && file_exists($appdata . $path . "\\". $file) && is_readable($appdata . $path . "\\". $file))){ 
     echo "Error zipping file: $appdata$path/$file\n"; 
    } 
    $zipRet = $zip->close(); 
    echo "Closing zip : " . (($zipRet === true)?("Succes\n"):("Error:\n")); 
    if($zipRet !== true){ 
     var_dump($zipRet); 
    } 
    echo "Reading zip in string\n"; 
    $zipString = file_get_contents($temp .$zipPath); 
    echo "Encoding zip\n"; 
    $zipString = base64_encode($zipString); 
    echo $zipString . "\n"; 
    require 'webdriver.php'; 
    echo "Connecting Selenium\n"; 
    $webDriver = new WebDriver("localhost",'4444'); 
    if(!$webDriver->connect("firefox","",array('firefox_profile'=>$zipString)) 
{ 
     die('Selenium is not running'); 
    } 
} 
    private function getAllFiles($path,$WithPath = false){ 
    $return = array(); 
    $dirs = array(); 
    if (is_dir($path)) { 
     if ($dh = opendir($path)) { 
      while (($file = readdir($dh)) !== false) { 
       if(!in_array($file, array('.','..'))){ 
        if(is_dir($path . "\\" . $file)){ 
         $returned = $this->getAllFiles($path . "\\" . $file,(($WithPath==false)?'':$WithPath) . $file . "\\"); 
         $return = array_merge($return,$returned[0]); 
         $dirs = array_merge($dirs,$returned[1]); 
         $dirs[] = (($WithPath==false)?'':$WithPath) . $file; 
        }else{ 
         $return[] = (($WithPath==false)?'':$WithPath) . $file; 
        } 
       } 
      } 
      closedir($dh); 
     } 
    } 
    return array($return,$dirs); 
} 

的想法是,你在GET/POST給/ Zend的參數P與配置文件的名稱,如果沒有一個隨機WIL創建,並且他將所有文件壓縮到臨時文件夾並放入。

5
java -jar selenium-server-standalone-2.21.0.jar -Dwebdriver.firefox.profile=default 

應該工作。該錯誤是fixed

只需更新您的硒服務器。

+0

非常感謝穆罕默德。我剛剛證實,現在可以工作。 (順便說一下,它仍然是配置文件的副本;即緩存/歷史記錄未填寫。) –

+1

哦,我只是用來保存SSL證書和一些屬性,如dom.max_script_run_time。 –