示例#1
0
        /// <summary>
        /// Saves as a file.
        /// </summary>
        /// <param name="filePath"></param>
        public void SaveAs(string filePath)
        {
            EnsureNotDisposed();
            PdfFont font = PdfFonts.Select(false, false);
            int     x    = -_pageNumberRightPt;
            int     y    = _pageNumberBottomPt;

            _writer.WritePagesNumbers(font, 9, x, y, 0);
            _writer.Close();
            try {
                if (filePath.IndexOf('{') != -1)
                {
                    filePath = string.Format(filePath, DateTime.UtcNow);
                }
                filePath = IOExt.ExpandPath(filePath);
                filePath = Path.GetFullPath(filePath);
                string folder = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                using (var fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write)) {
                    fs.Write(_writer.Buffer, 0, _writer.Length);
                }
            } catch (Exception ex) {
                throw new SeleniumException(ex);
            } finally {
                this.Dispose();
            }
        }
示例#2
0
 /// <summary>
 /// Load an image file
 /// </summary>
 /// <param name="filePath"></param>
 /// <returns>Self</returns>
 public Image Load(string filePath)
 {
     this.Dispose();
     filePath = IOExt.ExpandPath(filePath);
     if (!File.Exists(filePath))
     {
         throw new Errors.ArgumentError("File not found " + filePath);
     }
     _bitmap = (Bitmap)Bitmap.FromFile(filePath);
     return(this);
 }
示例#3
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);
 }
示例#4
0
        /// <summary>
        /// Save the image to a file. Supported format: png, bmp, gif and jpg.
        /// </summary>
        /// <param name="filePath">File path. Ex: "C:\capture_{yyyyMMdd-HHmmss}.png"</param>
        /// <param name="autoDispose">Release the image resources once done</param>
        /// <returns>Full file path</returns>
        public string SaveAs(string filePath, bool autoDispose = true)
        {
            EnsureNotDisposed();
            try {
                if (filePath.IndexOf('{') != -1)
                {
                    filePath = Regex.Replace(filePath, @"\{([^}]+)\}",
                                             (m) => DateTime.UtcNow.ToString(m.Groups[1].Value));
                }

                filePath = IOExt.ExpandPath(filePath);
                filePath = Path.GetFullPath(filePath);
                string folder = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                File.Create(filePath).Dispose();

                ImageFormat format;
                string      ext = Path.GetExtension(filePath).ToLower();
                switch (ext)
                {
                case ".bmp": format = ImageFormat.Bmp; break;

                case ".png": format = ImageFormat.Png; break;

                case ".jpg": format = ImageFormat.Jpeg; break;

                case ".jpeg": format = ImageFormat.Jpeg; break;

                case ".gif": format = ImageFormat.Gif; break;

                default:
                    throw new Errors.ImageError("Format not supported. Supported: bmp, png, jpg, gif.");
                }
                _bitmap.Save(filePath, format);

                if (autoDispose)
                {
                    Dispose();
                }
            } catch (Exception ex) {
                throw new Errors.ImageError(ex.Message);
            }
            return(filePath);
        }
示例#5
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);
            }
        }