51Testing软件测试论坛
标题:
Selenium - 封装WebDrivers (C#)
[打印本页]
作者:
感悟时分
时间:
2018-4-12 15:20
标题:
Selenium - 封装WebDrivers (C#)
Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDri
ver, 并将常用操作封装。
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QA = OpenQA.Selenium;
using UI = OpenQA.Selenium.Support.UI;
namespace XWebDriver
{
public class OneDriver
{
private QA.IWebDriver wd = null;
private Browsers browser = Browsers.IE;
public OneDriver(Browsers theBrowser)
{
this.browser = theBrowser;
wd = InitWebDriver();
}
private QA.IWebDriver InitWebDriver()
{
QA.IWebDriver theDriver = null;
switch (this.browser)
{
case Browsers.IE:
{
QA.IE.InternetExplorerOptions _ieOptions = new QA.IE.InternetExplorerOptions();
_ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
theDriver = new QA.IE.InternetExplorerDriver(_ieOptions);
}; break;
case Browsers.Chrome:
{
theDriver = new QA.Chrome.ChromeDriver();
}; break;
case Browsers.Firefox:
{
theDriver = new QA.Firefox.FirefoxDriver();
}; break;
case Browsers.Safari:
{
theDriver = new QA.Safari.SafariDriver();
};break;
case Browsers.PhantomJS:
{
theDriver = new QA.PhantomJS.PhantomJSDriver();
};break;
default:
{
QA.IE.InternetExplorerOptions _ieOptions = new QA.IE.InternetExplorerOptions();
_ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
theDriver = new QA.IE.InternetExplorerDriver(_ieOptions);
}; break;
}
return theDriver;
}
#region public members
/// <summary>
/// Effects throughout the life of web driver
/// Set once only if necessary
/// </summary>
/// <param name="seconds"></param>
public void ImplicitlyWait(double seconds)
{
wd.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(seconds));
}
/// <summary>
/// Wait for the expected condition is satisfied, return immediately
/// </summary>
/// <param name="expectedCondition"></param>
public void WaitForPage(string title)
{
UI.WebDriverWait _wait = new UI.WebDriverWait(wd, TimeSpan.FromSeconds(10));
_wait.Until((d) => { return d.Title.ToLower().StartsWith(title.ToLower()); });
//to do
}
/// <summary>
///
/// </summary>
/// <param name="we"></param>
public void WaitForElement(string id)
{
UI.WebDriverWait _wait = new UI.WebDriverWait(wd, TimeSpan.FromSeconds(10));
_wait.Until((d) => { return OpenQA.Selenium.Support.UI.ExpectedConditions.ElementExists(QA.By.Id(id));});
}
/// <summary>
/// Load a new web page in current browser
/// </summary>
/// <param name="url"></param>
public void GoToUrl(string url)
{
wd.Navigate().GoToUrl(url);
}
public void Refresh()
{
wd.Navigate().Refresh();
}
public void Back()
{
wd.Navigate().Back();
}
public void Forward()
{
wd.Navigate().Forward();
}
/// <summary>
/// Get the url of current browser window
/// </summary>
/// <returns></returns>
public string GetUrl()
{
return wd.Url;
}
/// <summary>
/// Get page title of current browser window
/// </summary>
/// <returns></returns>
public string GetPageTitle()
{
return wd.Title;
}
/// <summary>
/// Get all cookies defined in the current page
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetAllCookies()
{
Dictionary<string, string> cookies = new Dictionary<string, string>();
switch (this.browser)
{
case Browsers.IE:
{
var allCookies= ((QA.IE.InternetExplorerDriver)wd).Manage().Cookies.AllCookies;
foreach (QA.Cookie cookie in allCookies)
{
cookies[cookie.Name] = cookie.Value;
}
}; break;
case Browsers.Chrome:
{
var allCookies = ((QA.Chrome.ChromeDriver)wd).Manage().Cookies.AllCookies;
foreach (QA.Cookie cookie in allCookies)
{
cookies[cookie.Name] = cookie.Value;
}
}; break;
case Browsers.Firefox:
{
var allCookies = ((QA.Firefox.FirefoxDriver)wd).Manage().Cookies.AllCookies;
foreach (QA.Cookie cookie in allCookies)
{
cookies[cookie.Name] = cookie.Value;
}
}; break;
复制代码
作者:
感悟时分
时间:
2018-4-12 15:21
case Browsers.Safari:
{
var allCookies = ((QA.Safari.SafariDriver)wd).Manage().Cookies.AllCookies;
foreach (QA.Cookie cookie in allCookies)
{
cookies[cookie.Name] = cookie.Value;
}
}; break;
case Browsers.PhantomJS:
{
var allCookies = ((QA.PhantomJS.PhantomJSDriver)wd).Manage().Cookies.AllCookies;
foreach (QA.Cookie cookie in allCookies)
{
cookies[cookie.Name] = cookie.Value;
}
}; break;
default:
{
var allCookies = ((QA.IE.InternetExplorerDriver)wd).Manage().Cookies.AllCookies;
foreach (QA.Cookie cookie in allCookies)
{
cookies[cookie.Name] = cookie.Value;
}
}; break;
}
return cookies;
}
/// <summary>
/// Delete all cookies from the page
/// </summary>
public void DeleteAllCookies()
{
switch (this.browser)
{
case Browsers.IE:
{
((QA.IE.InternetExplorerDriver)wd).Manage().Cookies.DeleteAllCookies();
}; break;
case Browsers.Chrome:
{
((QA.Chrome.ChromeDriver)wd).Manage().Cookies.DeleteAllCookies();
}; break;
case Browsers.Firefox:
{
((QA.Firefox.FirefoxDriver)wd).Manage().Cookies.DeleteAllCookies();
}; break;
case Browsers.Safari:
{
((QA.Safari.SafariDriver)wd).Manage().Cookies.DeleteAllCookies();
}; break;
case Browsers.PhantomJS:
{
((QA.PhantomJS.PhantomJSDriver)wd).Manage().Cookies.DeleteAllCookies();
}; break;
default:
{
((QA.IE.InternetExplorerDriver)wd).Manage().Cookies.DeleteAllCookies();
}; break;
}
}
/// <summary>
/// Set focus to a browser window with a specified title
/// </summary>
/// <param name="title"></param>
/// <param name="exactMatch"></param>
public void GoToWindow(string title, bool exactMatch)
{
string theCurrent = wd.CurrentWindowHandle;
IList<string> windows = wd.WindowHandles;
if (exactMatch)
{
foreach (var window in windows)
{
wd.SwitchTo().Window(window);
if (wd.Title.ToLower() == title.ToLower())
{
return;
}
}
}
else
{
foreach (var window in windows)
{
wd.SwitchTo().Window(window);
if (wd.Title.ToLower().Contains(title.ToLower()))
{
return;
}
}
}
wd.SwitchTo().Window(theCurrent);
}
/// <summary>
/// Set focus to a frame with a specified name
/// </summary>
/// <param name="name"></param>
public void GoToFrame(string name)
{
QA.IWebElement theFrame = null;
var frames = wd.FindElements(QA.By.TagName("iframe"));
foreach (var frame in frames)
{
if (frame.GetAttribute("name").ToLower() == name.ToLower())
{
theFrame = (QA.IWebElement)frame;
break;
}
}
if (theFrame != null)
{
wd.SwitchTo().Frame(theFrame);
}
}
public void GoToFrame(QA.IWebElement frame)
{
wd.SwitchTo().Frame(frame);
}
/// <summary>
/// Switch to default after going to a frame
/// </summary>
public void GoToDefault()
{
wd.SwitchTo().DefaultContent();
}
/// <summary>
/// Get the alert text
/// </summary>
/// <returns></returns>
public string GetAlertString()
{
string theString = string.Empty;
QA.IAlert alert = null;
alert = wd.SwitchTo().Alert();
if (alert != null)
{
theString = alert.Text;
}
return theString;
}
/// <summary>
/// Accepts the alert
/// </summary>
public void AlertAccept()
{
QA.IAlert alert = null;
alert = wd.SwitchTo().Alert();
if (alert != null)
{
alert.Accept();
}
}
/// <summary>
/// Dismisses the alert
/// </summary>
public void AlertDismiss()
{
QA.IAlert alert = null;
alert = wd.SwitchTo().Alert();
if (alert != null)
{
alert.Dismiss();
}
}
/// <summary>
/// Move vertical scroll bar to bottom for the page
/// </summary>
public void PageScrollToBottom()
{
var js = "document.documentElement.scrollTop=10000";
switch (this.browser)
{
case Browsers.IE:
{
((QA.IE.InternetExplorerDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.Chrome:
{
((QA.Chrome.ChromeDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.Firefox:
{
((QA.Firefox.FirefoxDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.Safari:
{
((QA.Safari.SafariDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.PhantomJS:
{
((QA.PhantomJS.PhantomJSDriver)wd).ExecuteScript(js, null);
}; break;
default:
{
((QA.IE.InternetExplorerDriver)wd).ExecuteScript(js, null);
}; break;
}
}
/// <summary>
/// Move horizontal scroll bar to right for the page
/// </summary>
public void PageScrollToRight()
{
var js = "document.documentElement.scrollLeft=10000";
switch (this.browser)
{
case Browsers.IE:
{
((QA.IE.InternetExplorerDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.Chrome:
{
((QA.Chrome.ChromeDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.Firefox:
{
((QA.Firefox.FirefoxDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.Safari:
{
((QA.Safari.SafariDriver)wd).ExecuteScript(js, null);
}; break;
case Browsers.PhantomJS:
{
((QA.PhantomJS.PhantomJSDriver)wd).ExecuteScript(js, null);
}; break;
default:
{
((QA.IE.InternetExplorerDriver)wd).ExecuteScript(js, null);
}; break;
}
}
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2