Selenium2 firefox: use the default profile

前端 未结 6 1737
挽巷
挽巷 2020-12-05 12:16

Selenium2, by default, starts firefox with a fresh profile. I like that for a default, but for some good reasons (access to my bookmarks, saved passwords, use my add-ons, et

6条回答
  •  不思量自难忘°
    2020-12-05 12:48

    I did It in Zend like this:

        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('
    No profile found with name: ' . $hash);
        }
        echo "
    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);
    }
    

    The Idea is that you give in the get/post/zend parameters P with the name of the profile if not a random wil be created, and he will zip all the files put it in the temp folder and put it in.

提交回复
热议问题