Ejemplo n.º 1
0
 private static string ExpandProfile(string profile, bool remote)
 {
     if (!remote)
     {
         if (IOExt.IsPath(profile))
         {
             profile = IOExt.ExpandPath(profile);
         }
         else
         {
             profile = IOExt.AppDataFolder + @"\Opera Software\Opera\Profiles\" + profile;
         }
         Directory.CreateDirectory(profile);
     }
     return(profile);
 }
Ejemplo n.º 2
0
        private void SetupProfile()
        {
            if (string.IsNullOrEmpty(this.Profile))
            {
                //Gets a profile
                if (this.Persistant)
                {
                    _profile_dir = Path.Combine(_working_dir, PROFILE_PERSISTANT_FOLDER);;
                }
                else
                {
                    //create temp profile
                    _profile_dir = Path.Combine(_working_dir, PROFILE_PREFIX_TEMP_FOLDER + IOExt.GetRandomName());
                }
            }
            else
            {
                //Use an existing profile or create one
                if (IOExt.IsPath(this.Profile))
                {
                    //if profile by path
                    _profile_dir = IOExt.ExpandPath(this.Profile);
                }
                else
                {
                    //if profile by name
                    string appdata_dir;
                    if (!this.Capabilities.TryGetValue("firefox_appdata", out appdata_dir))
                    {
                        appdata_dir = Path.Combine(IOExt.AppDataFolder, APP_PROFILES_FOLDER);
                    }

                    if (!Directory.Exists(appdata_dir))
                    {
                        throw new SeleniumError("Firefox profile folder is missing: {0}", appdata_dir);
                    }

                    Dictionary profiles = ParseExistingProfiles(appdata_dir);
                    if (!profiles.TryGetValue(this.Profile, out _profile_dir))
                    {
                        CreateProfile(_firefox_path, appdata_dir, this.Profile, out _profile_dir);
                    }
                }
                if (!this.Persistant)
                {
                    //create the temporary profile
                    _profile_dir = Path.Combine(_working_dir, PROFILE_PREFIX_TEMP_FOLDER + IOExt.GetRandomName());
                    IOExt.Copy(new DirectoryInfo(_profile_dir), new DirectoryInfo(_profile_dir), "parent.lock");
                }
            }

            Directory.CreateDirectory(_profile_dir);

            //Read existing prefs if any
            Dictionary prefs      = new Dictionary();
            string     prefs_path = Path.Combine(_profile_dir, "user.js");

            if (File.Exists(prefs_path))
            {
                using (var stream = File.Open(prefs_path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    CopyProfilePrefs(stream, ref prefs);
            }

            //adds webdriver prefs
            prefs["webdriver_firefox_port"] = this.IPEndPoint.Port;
            using (var stream = typeof(FirefoxService).Assembly.GetManifestResourceStream(PREFS_RESOURCE_FILENAME))
                CopyProfilePrefs(stream, ref prefs);

            //adds proxy to prefs if any
            Dictionary proxy;

            if (this.Capabilities.TryGetValue("proxy", out proxy) && proxy.Count > 0)
            {
                CopyProxyPrefs(proxy, ref prefs);
            }

            //set download dir
            string download_dir = _profile_dir + @"\downloads";

            Directory.CreateDirectory(download_dir);
            prefs["browser.download.dir"] = download_dir;

            //adds custom prefs if any
            foreach (DictionaryItem item in this.Preferences)
            {
                prefs[item.Key] = item.Value;
            }

            //writes the new prefs
            SaveProfilePrefs(prefs, prefs_path);

            //Install the WebDriver extention
            string ext_wd_path = Path.Combine(_this_assembly_dir, EXT_WEBDRIVER_FILENAME);

            InstallExtension(ext_wd_path, _profile_dir, EXT_WEBDRIVER_RID);

            //Install the provided extensions
            if (this.Extensions != null && !this.Persistant)
            {
                foreach (DriverExtension extension in this.Extensions)
                {
                    InstallExtension(extension.Path, _profile_dir);
                }
            }

            //delete logs
            foreach (string file in Directory.GetFiles(_profile_dir, "*.txt"))
            {
                File.Delete(file);
            }
        }