本文转载自:http://www.cnblogs.com/qixue/p/3977135.html

Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装。

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;
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;
}
} /// <summary>
/// Move vertical scroll bar to bottom for an element
/// </summary>
/// <param name="element"></param>
public void ElementScrollToBottom(QA.IWebElement element)
{
string id = element.GetAttribute("id");
string name = element.GetAttribute("name");
var js = "";
if (!string.IsNullOrWhiteSpace(id))
{
js = "document.getElementById('" + id + "').scrollTop=10000";
}
else if (!string.IsNullOrWhiteSpace(name))
{
js = "document.getElementsByName('" + name + "')[0].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>
/// Get a screen shot of the current window
/// </summary>
/// <param name="savePath"></param>
public void TakeScreenshot(string savePath)
{
QA.Screenshot theScreenshot = null;
switch (this.browser)
{
case Browsers.IE:
{
theScreenshot = ((QA.IE.InternetExplorerDriver)wd).GetScreenshot();
}; break;
case Browsers.Chrome:
{
theScreenshot = ((QA.Chrome.ChromeDriver)wd).GetScreenshot();
}; break;
case Browsers.Firefox:
{
theScreenshot = ((QA.Firefox.FirefoxDriver)wd).GetScreenshot();
}; break;
case Browsers.Safari:
{
theScreenshot = ((QA.Safari.SafariDriver)wd).GetScreenshot();
}; break;
case Browsers.PhantomJS:
{
theScreenshot = ((QA.PhantomJS.PhantomJSDriver)wd).GetScreenshot();
}; break;
default:
{
theScreenshot = ((QA.IE.InternetExplorerDriver)wd).GetScreenshot();
}; break;
}
if (theScreenshot != null)
{
theScreenshot.SaveAsFile(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
} /// <summary>
/// Find the element of a specified id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public QA.IWebElement FindElementById(string id)
{
QA.IWebElement theElement = null;
theElement = (QA.IWebElement)wd.FindElement(QA.By.Id(id));
return theElement;
} /// <summary>
/// Find the element of a specified name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public QA.IWebElement FindElementByName(string name)
{
QA.IWebElement theElement = null;
theElement = (QA.IWebElement)wd.FindElement(QA.By.Name(name));
return theElement;
} /// <summary>
/// Find the element by xpath
/// </summary>
/// <param name="xpath"></param>
/// <returns></returns>
public QA.IWebElement FindElementByXPath(string xpath)
{
QA.IWebElement theElement = null;
theElement = (QA.IWebElement)wd.FindElement(QA.By.XPath(xpath));
return theElement;
} public QA.IWebElement FindElementByLinkText(string text)
{
QA.IWebElement theElement = null;
try
{
theElement = wd.FindElement(QA.By.LinkText(text));
}
catch { }
return theElement;
} public IList<QA.IWebElement> FindElementsByLinkText(string text)
{
IList<QA.IWebElement> theElement = null;
theElement = (IList<QA.IWebElement>)wd.FindElements(QA.By.LinkText(text));
return theElement;
} public IList<QA.IWebElement> FindElementsByPartialLinkText(string text)
{
IList<QA.IWebElement> theElement = null;
theElement = (IList<QA.IWebElement>)wd.FindElements(QA.By.PartialLinkText(text));
return theElement;
} public IList<QA.IWebElement> FindElementsByClassName(string clsName)
{
IList<QA.IWebElement> theElement = null;
theElement = (IList<QA.IWebElement>)wd.FindElements(QA.By.ClassName(clsName));
return theElement;
} public IList<QA.IWebElement> FindElementsByTagName(string tagName)
{
IList<QA.IWebElement> theElement = null;
theElement = (IList<QA.IWebElement>)wd.FindElements(QA.By.TagName(tagName));
return theElement;
} public IList<QA.IWebElement> FindElementsByCssSelector(string css)
{
IList<QA.IWebElement> theElement = null;
theElement = (IList<QA.IWebElement>)wd.FindElements(QA.By.CssSelector(css));
return theElement;
} public IList<QA.IWebElement> FindElementsByXPathName(string xpath)
{
IList<QA.IWebElement> theElement = null;
theElement = (IList<QA.IWebElement>)wd.FindElements(QA.By.XPath(xpath));
return theElement;
} /// <summary>
/// Executes javascript
/// </summary>
/// <param name="js"></param>
public void ExecuteJS(string js)
{
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;
}
} public void ClickElement(QA.IWebElement element)
{
(new QA.Interactions.Actions(wd)).Click(element).Perform();
} public void DoubleClickElement(QA.IWebElement element)
{
(new QA.Interactions.Actions(wd)).DoubleClick(element).Perform();
} public void ClickAndHoldOnElement(QA.IWebElement element)
{
(new QA.Interactions.Actions(wd)).ClickAndHold(element).Perform();
} public void ContextClickOnElement(QA.IWebElement element)
{
(new QA.Interactions.Actions(wd)).ContextClick(element).Perform();
} public void DragAndDropElement(QA.IWebElement source, QA.IWebElement target)
{
(new QA.Interactions.Actions(wd)).DragAndDrop(source, target).Perform();
} public void SendKeysToElement(QA.IWebElement element, string text)
{
(new QA.Interactions.Actions(wd)).SendKeys(element, text).Perform();
} /// <summary>
/// Quit this server, close all windows associated to it
/// </summary>
public void Cleanup()
{
wd.Quit();
}
#endregion
} public enum Browsers
{
IE,
Firefox,
Chrome,
Safari,
PhantomJS
}
}

【转】Selenium - 封装WebDrivers (C#)的更多相关文章

  1. Selenium - 封装WebDrivers (C#)

    Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...

  2. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  3. Selenium封装

    import os from selenium import webdriver from selenium.webdriver.common.by import By from selenium.w ...

  4. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  5. selenium 封装代码

    package pers.xeon.automate.auxt; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen ...

  6. Java Selenium封装--RemoteWebDriver

    package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...

  7. Java Selenium封装--RemoteWebElement

    package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...

  8. selenium之封装登陆操作

    # selenium 封装登录操作举例 import os, time # from selenium import webdriver class LoginPage(): '''登录模块''' d ...

  9. Python+Selenium框架设计之框架内封装基类和实现POM

    原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN    博客地址https ...

随机推荐

  1. ubuntu系统安装mysql登陆提示 解决Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqoAAABtCAIAAADmldQYAAAe/0lEQVR4nO1dveslR3btzJGFk02GmU

  2. 将xml 写到内存中再已string类型读出来

    System.IO.MemoryStream ms = new System.IO.MemoryStream(); xmlDoc.Save(ms); System.IO.StreamReader sr ...

  3. Web服务API

    Web服务API     内容 [ 隐藏 ]  1 概述 2个 services.php 3 详细教程 4 例子 5 另见 概观 Web服务API允许您将插件的功能(通常是外部函数)公开为Web服务. ...

  4. 《Android高级进阶》读书笔记

    <Android高级进阶>是据我所知的市面上唯一一本技术工具书,比较的高大全,作者的目的是为了对全领域有个初步的概念 No1: 在Android系统中,拥有事件传递处理能力的类有以下三种 ...

  5. 打印星星 - Python

    打印星星是经典面试题目,考察流程控制中的循环和条件.本文对相关方法进行总结. 到的方法只要有(1)嵌套循环(2)center(3)format(^)(4)字符串乘法 # -*- coding:utf- ...

  6. 004.NTP多层级架设

    一 环境需求 1.1 需求 User-client:局域网所有节点主机: IN-NTP Server:隐藏于局域网内部的NTP服务器: Border-NTP:边界NTP服务器,用于同步外部时钟,同时对 ...

  7. 湖南大学第十四届ACM程序设计新生杯(重现赛)

    RANK  0 题数 0 期末复习没有参加,补几道喜欢的题. A: AFei Loves Magic  签到 思路 :不需考虑 碰撞 直接计算最终状态即可. #include<bits/stdc ...

  8. 安装淘宝镜像cnpm时出现问题及解决方案

    问题: 解决方案: 安装完成:

  9. Squid正向代理(编译安装)

    编译安装 版本为squid-3.5.27 系统为Centos6.5 依赖环境 yum install -y perl gcc*autoconf automake make sudo wget libx ...

  10. 【持久化框架】Mybatis与Hibernate的详细对比(转发)

    前言 这篇博文我们重点分析一下Mybatis与Hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...