Ejemplo n.º 1
0
 public DriverService()
 {
     _library_dir = IOExt.GetAssemblyDirectory();
     _arguments   = new List <string>();
     _temp_folder = GetTempFolder();
     _endpoint    = EndPointExt.Create(IPAddress.Loopback, false);
 }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
0
        public void Dispose()
        {
            if (_endpoint != null)
            {
                _endpoint.Dispose();
                _endpoint = null;
            }

            if (_firefox_process != null)
            {
                if (!_firefox_process.HasExited)
                {
                    _firefox_process.Kill();
                    _firefox_process.WaitForExit();
                }
                _firefox_process.Dispose();
                _firefox_process = null;
            }

            if (_profile_dir != null && !this.Persistant && Directory.Exists(_profile_dir))
            {
                IOExt.DeleteDirectoryByShell(_profile_dir);
                _profile_dir = null;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 导出脚本
        /// <para>procList:key-存储过程名称,value-存储过程文本</para>
        /// </summary>
        public virtual void ExportSQL(List <KeyValuePair <string, string> > procList, string folder)
        {
            procList.ForEach(proc =>
            {
                string path = folder + proc.Key + ".sql";

                IOExt.Write(path, proc.Value);
            });
        }
Ejemplo n.º 5
0
        private static string ReplaceSQL(string templetName, Dictionary <string, string> paramList)
        {
            string sql = IOExt.Read($@"{Directory.GetCurrentDirectory()}\SQL模板\{templetName}.sql");

            paramList.ForEach(param =>
            {
                sql = sql.ReplaceNoCase($"{{{param.Key}}}", param.Value);
            });
            return(sql);
        }
Ejemplo n.º 6
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);
 }
Ejemplo n.º 7
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.º 8
0
        static string GetBinaryLocation(Dictionary capabilities)
        {
            string path;

            if (!capabilities.TryGetValue("firefox_binary", out path))
            {
                path = IOExt.GetApplicationPath(APP_FILENAME);
            }

            if (!File.Exists(path))
            {
                throw new Errors.FileNotFoundError(path);
            }

            return(path);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 private XmlDocument GetConfigXml()
 {
     IOExt.CreateXml(ConfigFilePath, "ArrayOfDataBase");
     return(IOExt.ReadXml(ConfigFilePath));
 }
Ejemplo n.º 11
0
 public void ClearHistory()
 {
     IOExt.DeleteFile(ConfigFilePath);
     LoadConfig();
 }
Ejemplo n.º 12
0
        /// <summary>
        /// 导出文档
        /// <para>resultDic:key-系统名称,value-对应表信息</para>
        /// </summary>
        public virtual void ExportDoc(Dictionary <string, List <ComparaResult> > resultDic, string folder)
        {
            // 导出Excel
            var excel = new HSSFWorkbook();

            // 单元格样式
            ICellStyle cellStyle = excel.CreateCellStyle();

            cellStyle.VerticalAlignment = VerticalAlignment.Center;
            cellStyle.BorderTop         = BorderStyle.Thin;
            cellStyle.BorderBottom      = BorderStyle.Thin;
            cellStyle.BorderLeft        = BorderStyle.Thin;
            cellStyle.BorderRight       = BorderStyle.Thin;

            // 标题样式
            ICellStyle titleStyle = excel.CreateCellStyle();

            titleStyle.CloneStyleFrom(cellStyle);
            titleStyle.Alignment = HorizontalAlignment.Center;
            IFont font = excel.CreateFont();

            font.Boldweight = (short)FontBoldWeight.Bold;
            titleStyle.SetFont(font);

            // 备注样式
            // 样式尽可能定义好并复用,动态创建可能会导致导出的Excel样式错乱(和NPOI内部实现有关)
            ICellStyle remarkStyle = excel.CreateCellStyle();

            remarkStyle.CloneStyleFrom(cellStyle);
            remarkStyle.FillPattern         = FillPattern.SolidForeground;
            remarkStyle.FillForegroundColor = HSSFColor.Yellow.Index;

            // 设置
            resultDic.ForEach(dic =>
            {
                ISheet sheet = excel.CreateSheet(dic.Key);

                // 创建标题
                IRow rowTitle = sheet.CreateRow(0);
                CreateCell(ref rowTitle, 0, titleStyle, "表中文名");
                CreateCell(ref rowTitle, 1, titleStyle, "对比表名");
                CreateCell(ref rowTitle, 2, titleStyle, "目标表名");
                CreateCell(ref rowTitle, 3, titleStyle, "标志");
                CreateCell(ref rowTitle, 4, titleStyle, "是否清空");
                CreateCell(ref rowTitle, 5, titleStyle, "备注");

                // 创建内容
                dic.Value.ForEach((result, i) =>
                {
                    var e = (ComparaInfo)result;

                    IRow row = sheet.CreateRow(i + 1);
                    CreateCell(ref row, 0, cellStyle, e.TableNameChn);
                    CreateCell(ref row, 1, cellStyle, e.ComparaTableName);
                    CreateCell(ref row, 2, cellStyle, e.TargetTableName);
                    CreateCell(ref row, 3, cellStyle, e.Flag);
                    CreateCell(ref row, 4, cellStyle, e.IsClear);
                    CreateCell(ref row, 5, result.IsAssert ? remarkStyle : cellStyle, e.Remark);
                });

                // 列宽自适应
                for (int i = 0; i < rowTitle.Cells.Count; i++)
                {
                    sheet.AutoSizeColumn(i);
                }
            });

            // 写入文件
            string path = folder + "清空升级.xls";

            IOExt.CreateFile(path);
            File.SetAttributes(path, FileAttributes.Normal);

            using (var fs = new FileStream(path, FileMode.Create))
            {
                excel.Write(fs);
            }
        }
Ejemplo n.º 13
0
 public FirefoxService()
 {
     _this_assembly_dir = IOExt.GetAssemblyDirectory();
     _working_dir       = DriverService.GetTempFolder();
     _profil_cache_path = Path.Combine(_working_dir, PROFILE_CACHE_FILENAME);
 }
Ejemplo n.º 14
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);
            }
        }