c# selenium Driver启动
来源:互联网 发布:淘宝没有销售属性 编辑:程序博客网 时间:2024/06/06 08:43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.Extensions;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using Protractor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using NetBrain.Common.Utility;
using System.Threading;
namespace NetBrain.Framework.Browser
{
public class Browser
{
#region properties
public static IWebDriver Driver;
public static NgWebDriver NgDriver;
#endregion
/// <summary>
/// Launch Browser according to the given browser type, and max the window.
/// </summary>
/// <param name="Type">IE,FF or CH</param>
/// <returns></returns>
public static void LaunchBrowser(string Type, string URL)
{
string DriverPath = Environment.CurrentDirectory + @"\..\..\..\NetBrain.Framework.Browser\bin\Debug";
string downloadPath = EnvSettings.downloadPath;
if (Type.ToLower()=="chrome")
{
ChromeOptions o = new ChromeOptions();
o.AddUserProfilePreference("download.default_directory", downloadPath);
o.AddUserProfilePreference("intl.accept_languages", "nl");
o.AddUserProfilePreference("disable-popup-blocking", "true");
//o.AddArguments("--disable-extensions");
o.AddArguments("--start-maximized");
o.AddArguments("--disable-notifications");
o.AddArguments("disable-infobars");
//WebDriver driver = new ChromeDriver(o);
Driver = new ChromeDriver(DriverPath, o);
}
if (Type.ToLower() == "ie")
{
InternetExplorerOptions Options = new InternetExplorerOptions();
Options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
Driver = new InternetExplorerDriver(Options);
}
if (Type.ToLower() == "firefox")
{
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", downloadPath);
//profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
///概率性出现OpenQA.Selenium.WebDriverException: Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses: 127.0.0.1:7055. 增加retry步骤
try
{
//Driver = new FirefoxDriver(profile);
string sBrowserExe = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxBinary Bin = new FirefoxBinary(sBrowserExe);
//mDriver = new FirefoxDriver(Bin, Prof);
Driver = new FirefoxDriver(Bin, profile);
}
catch (WebDriverException e)
{
// if(e.Message.Contains("Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses:"))
// {
//Driver.Quit();
//FirefoxProfile Prof = new FirefoxProfile();
string sBrowserExe = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxBinary Bin = new FirefoxBinary(sBrowserExe);
//mDriver = new FirefoxDriver(Bin, Prof);
Driver = new FirefoxDriver(Bin, profile);
//}
}
}
Driver.Manage().Window.Maximize();
//Login前先清除下cookie,确保账号密码登录
Driver.Manage().Cookies.DeleteAllCookies();
Driver.Navigate().GoToUrl(URL);
//NgDriver = new NgWebDriver(Driver, "nbApp");
NgDriver = new NgWebDriver(Driver);
BrowserSetting();
//return NgDriver;
//return NgDriver;
}
private static void BrowserSetting()
{
//Driver.Manage().Cookies.DeleteAllCookies();
//NgDriver.IgnoreSynchronization = false;
NgDriver.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 20);
NgDriver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 5);
NgDriver.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 20);
// NgDriver.WaitForAngular();
//NgDriver.IgnoreSynchronization = true;
Driver.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 20);
Driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 5);
Driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 20);
}
public static List<string> GetWindowsHandles()
{
return NgDriver.WindowHandles.ToList();
}
public static void QuitBrowser()
{
NgDriver.Quit();
}
public static void DisposeBrowser()
{
NgDriver.Dispose();
}
public static void KillDriverProcess(string browserType)
{
switch (browserType.ToLower())
{
case "chrome": if (TestUtility.ExistProcess("chrome")) TestUtility.KillProcess("chrome"); break;
case "firefox": if (TestUtility.ExistProcess("firefox")) TestUtility.KillProcess("firefox"); break;
case "ie": if (TestUtility.ExistProcess("IEDriverServer")) TestUtility.KillProcess("IEDriverServer"); break;
}
}
/// <summary>
/// Delete All cookies
/// </summary>
public static void DeleteAllCookies()
{
NgDriver.Manage().Cookies.DeleteAllCookies();
}
public static Screenshot TakeScreenShot()
{
return NgDriver.WrappedDriver.TakeScreenshot();
}
/// <summary>
/// Execute Java Script
/// </summary>
/// <param name="strJS"></param>
public static void ExecuteJaveScript(string strJS)
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript(strJS);
}
/// <summary>
/// Execute Jave Script return string
/// </summary>
/// <param name="strJS"></param>
/// <returns></returns>
public static string ExecuteJaveScriptReturnString(string strJS)
{
//IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
IJavaScriptExecutor js = Driver as IJavaScriptExecutor;
return (string)js.ExecuteScript(strJS);
}
public static ChromeOptions ChromeSettings()
{
ChromeOptions profile = new ChromeOptions();
string path = ConfigurationManager.AppSettings["DownloadPath"];
//profile.AddArguments("disable-popup-blocking", "true");
//profile.AddArguments("download.default_directory",path);
//profile.AddArguments("download.directory_upgrade", "true");
//profile.AddArguments("download.prompt_for_download", "false");
profile.AddUserProfilePreference("download.default_directory", path);
profile.AddUserProfilePreference("intl.accept_languages", "nl");
profile.AddUserProfilePreference("disable-popup-blocking", "true");
return profile;
}
//设置Firefox浏览器下载zip文件不弹框,默认下载到指定文件夹中
public static FirefoxProfile FirfoxSettings()
{
FirefoxProfile firefoxProfile = new FirefoxProfile();
string path = ConfigurationManager.AppSettings["DownloadPath"];
firefoxProfile.AcceptUntrustedCertificates = true;
firefoxProfile.SetPreference("browser.download.dir", path);
firefoxProfile.SetPreference("browser.download.folderList", 2);
firefoxProfile.SetPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.SetPreference("browser.download.alertOnEXEOpen", false);
firefoxProfile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/json,text/xml,text/csv, text/plain, text/log, application/zlib, application/x-compressed, application/x-gtar, multipart/x-gzip, application/tgz, application/gnutar, application/x-tar, application/gzip");
firefoxProfile.SetPreference("pdfjs.disabled", true);
return firefoxProfile;
}
/// <summary>
/// Navigation 到指定的URL
/// </summary>
/// <param name="URL"></param>
public static void NavigateToURL(string URL)
{
Browser.NgDriver.Navigate().GoToUrl(URL);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.Extensions;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using Protractor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using NetBrain.Common.Utility;
using System.Threading;
namespace NetBrain.Framework.Browser
{
public class Browser
{
#region properties
public static IWebDriver Driver;
public static NgWebDriver NgDriver;
#endregion
/// <summary>
/// Launch Browser according to the given browser type, and max the window.
/// </summary>
/// <param name="Type">IE,FF or CH</param>
/// <returns></returns>
public static void LaunchBrowser(string Type, string URL)
{
string DriverPath = Environment.CurrentDirectory + @"\..\..\..\NetBrain.Framework.Browser\bin\Debug";
string downloadPath = EnvSettings.downloadPath;
if (Type.ToLower()=="chrome")
{
ChromeOptions o = new ChromeOptions();
o.AddUserProfilePreference("download.default_directory", downloadPath);
o.AddUserProfilePreference("intl.accept_languages", "nl");
o.AddUserProfilePreference("disable-popup-blocking", "true");
//o.AddArguments("--disable-extensions");
o.AddArguments("--start-maximized");
o.AddArguments("--disable-notifications");
o.AddArguments("disable-infobars");
//WebDriver driver = new ChromeDriver(o);
Driver = new ChromeDriver(DriverPath, o);
}
if (Type.ToLower() == "ie")
{
InternetExplorerOptions Options = new InternetExplorerOptions();
Options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
Driver = new InternetExplorerDriver(Options);
}
if (Type.ToLower() == "firefox")
{
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", downloadPath);
//profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
///概率性出现OpenQA.Selenium.WebDriverException: Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses: 127.0.0.1:7055. 增加retry步骤
try
{
//Driver = new FirefoxDriver(profile);
string sBrowserExe = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxBinary Bin = new FirefoxBinary(sBrowserExe);
//mDriver = new FirefoxDriver(Bin, Prof);
Driver = new FirefoxDriver(Bin, profile);
}
catch (WebDriverException e)
{
// if(e.Message.Contains("Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses:"))
// {
//Driver.Quit();
//FirefoxProfile Prof = new FirefoxProfile();
string sBrowserExe = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxBinary Bin = new FirefoxBinary(sBrowserExe);
//mDriver = new FirefoxDriver(Bin, Prof);
Driver = new FirefoxDriver(Bin, profile);
//}
}
}
Driver.Manage().Window.Maximize();
//Login前先清除下cookie,确保账号密码登录
Driver.Manage().Cookies.DeleteAllCookies();
Driver.Navigate().GoToUrl(URL);
//NgDriver = new NgWebDriver(Driver, "nbApp");
NgDriver = new NgWebDriver(Driver);
BrowserSetting();
//return NgDriver;
//return NgDriver;
}
private static void BrowserSetting()
{
//Driver.Manage().Cookies.DeleteAllCookies();
//NgDriver.IgnoreSynchronization = false;
NgDriver.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 20);
NgDriver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 5);
NgDriver.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 20);
// NgDriver.WaitForAngular();
//NgDriver.IgnoreSynchronization = true;
Driver.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 20);
Driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 5);
Driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 20);
}
public static List<string> GetWindowsHandles()
{
return NgDriver.WindowHandles.ToList();
}
public static void QuitBrowser()
{
NgDriver.Quit();
}
public static void DisposeBrowser()
{
NgDriver.Dispose();
}
public static void KillDriverProcess(string browserType)
{
switch (browserType.ToLower())
{
case "chrome": if (TestUtility.ExistProcess("chrome")) TestUtility.KillProcess("chrome"); break;
case "firefox": if (TestUtility.ExistProcess("firefox")) TestUtility.KillProcess("firefox"); break;
case "ie": if (TestUtility.ExistProcess("IEDriverServer")) TestUtility.KillProcess("IEDriverServer"); break;
}
}
/// <summary>
/// Delete All cookies
/// </summary>
public static void DeleteAllCookies()
{
NgDriver.Manage().Cookies.DeleteAllCookies();
}
public static Screenshot TakeScreenShot()
{
return NgDriver.WrappedDriver.TakeScreenshot();
}
/// <summary>
/// Execute Java Script
/// </summary>
/// <param name="strJS"></param>
public static void ExecuteJaveScript(string strJS)
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript(strJS);
}
/// <summary>
/// Execute Jave Script return string
/// </summary>
/// <param name="strJS"></param>
/// <returns></returns>
public static string ExecuteJaveScriptReturnString(string strJS)
{
//IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
IJavaScriptExecutor js = Driver as IJavaScriptExecutor;
return (string)js.ExecuteScript(strJS);
}
public static ChromeOptions ChromeSettings()
{
ChromeOptions profile = new ChromeOptions();
string path = ConfigurationManager.AppSettings["DownloadPath"];
//profile.AddArguments("disable-popup-blocking", "true");
//profile.AddArguments("download.default_directory",path);
//profile.AddArguments("download.directory_upgrade", "true");
//profile.AddArguments("download.prompt_for_download", "false");
profile.AddUserProfilePreference("download.default_directory", path);
profile.AddUserProfilePreference("intl.accept_languages", "nl");
profile.AddUserProfilePreference("disable-popup-blocking", "true");
return profile;
}
//设置Firefox浏览器下载zip文件不弹框,默认下载到指定文件夹中
public static FirefoxProfile FirfoxSettings()
{
FirefoxProfile firefoxProfile = new FirefoxProfile();
string path = ConfigurationManager.AppSettings["DownloadPath"];
firefoxProfile.AcceptUntrustedCertificates = true;
firefoxProfile.SetPreference("browser.download.dir", path);
firefoxProfile.SetPreference("browser.download.folderList", 2);
firefoxProfile.SetPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.SetPreference("browser.download.alertOnEXEOpen", false);
firefoxProfile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/json,text/xml,text/csv, text/plain, text/log, application/zlib, application/x-compressed, application/x-gtar, multipart/x-gzip, application/tgz, application/gnutar, application/x-tar, application/gzip");
firefoxProfile.SetPreference("pdfjs.disabled", true);
return firefoxProfile;
}
/// <summary>
/// Navigation 到指定的URL
/// </summary>
/// <param name="URL"></param>
public static void NavigateToURL(string URL)
{
Browser.NgDriver.Navigate().GoToUrl(URL);
}
}
}
阅读全文
0 0
- c# selenium Driver启动
- selenium:selenium-safari-driver
- Selenium and Web Driver
- Selenium - Web driver
- Selenium Web Driver
- selenium webdriver的各种driver
- selenium之三,各种driver
- selenium webdriver的各种driver
- selenium webdriver的各种driver
- selenium webdriver的各种driver
- perl Selenium::Remote::Driver使用
- 37、Selenium 之 help(driver)
- selenium chrome driver和 IE driver 常见问题(1)
- selenium chrome driver 和IE driver 常见问题(2)
- selenium使用更快的Driver--HtmlUnit Driver
- Windows XP配置Selenium Perl Driver
- Selenium RC 与 Web Driver 的区别
- 改写Selenium Web Driver Sample Code
- 一些简单的js技术 实现点击 的js隐藏显示
- String类型的时间转换成datetime类型,并实现减法
- 进程与线程——翻译文
- Recyclerview-自定义LayoutManager
- 快速排序
- c# selenium Driver启动
- 学习之路—可重用的命令行小程序v3.0
- 编写Shell程序,通过编写完成compress1、decompress1函数,选择“压缩”或“解压”以及输入文件名,来自动完成文件的压缩、解压过程。
- C#获取浏览器中的图片
- Sublime Text 3 格式化代码快捷键设置
- Spring的体系结构
- [COGS]2. 旅行计划 SPFA解法
- eclipse支持java9
- jenkins 系统设置(全局设置)实例