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);
}
}
}
c# selenium Driver啓動
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
C# 基於SharpPcap的局域網數據包捕獲 之二 監聽代碼
優惠碼大使
2019-02-24 19:41:12
[C#]“冒泡排序”示例代碼
五子連星
2019-02-23 13:23:12
C# 中的委託和事件
zachary_woo
2019-02-23 00:15:11
告警程序實例(c#實現)
xiaoshu838
2019-02-22 23:23:50
看陳廣老師c#參考視頻總結(第七篇)
xiaoshu838
2019-02-22 23:23:46
面向對象設計模式與原則
xiaoshu838
2019-02-22 23:23:46
看陳廣老師c#參考視頻總結(第一篇)
xiaoshu838
2019-02-22 23:23:46
61條面向對象的設計原則
xiaoshu838
2019-02-22 23:23:45
如何閱讀別人的代碼
xiaoshu838
2019-02-22 23:23:45
看陳廣老師c#參考視頻總結(第二篇)
xiaoshu838
2019-02-22 23:23:23
看陳廣老師c#參考視頻總結(第六篇)
xiaoshu838
2019-02-22 23:23:19
Java Web自动化测试 Selenium基础到企业实际应用
wy53780
2020-04-23 14:04:28
Nagios集成Selenium
Jason_川川
2019-02-22 23:46:42