2011-09-07 43 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。 –

相关问题