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
///
/// Launch Browser according to the given browser type, and max the window.
///
///
IE,FF or CH
///
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
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;
}
}
///
/// Delete All cookies
///
public static void DeleteAllCookies()
{
NgDriver.Manage().Cookies.DeleteAllCookies();
}
public static Screenshot TakeScreenShot()
{
return NgDriver.WrappedDriver.TakeScreenshot();
}
///
/// Execute Java Script
///
///
public static void ExecuteJaveScript(string strJS)
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript(strJS);
}
///
/// Execute Jave Script return string
///
///
///
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;
}
///
/// Navigation 到指定的URL
///
///
public static void NavigateToURL(string URL)
{
Browser.NgDriver.Navigate().GoToUrl(URL);
}
}
}