Webdriver(selenium2.0)+NUnit+C# (二)

namespace SeleniumTests
{
    [TestFixture]
    public class Login
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;
        
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "URL";
            verificationErrors = new StringBuilder();
        }
        
        [TearDown]
        public void TeardownTest()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        
        [Test]
        public void TheLoginTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/login");
            driver.FindElement(By.Name("username")).Clear();
            driver.FindElement(By.Name("username")).SendKeys("USERNAME");
            driver.FindElement(By.Name("password")).Clear();
            driver.FindElement(By.Name("password")).SendKeys("PASSWORD");
            driver.FindElement(By.XPath("//button[@type=‘submit‘]")).Click();
        }
     }
  }

上面是用selenium ide 录制的某个页面的登录操作代码。像是输入用户名和密码的代码就有点重复多余繁琐,那就可以封装一个叫SendKeys的方法(包括clear和sendkeys的动作),而不需要每次去找这个element,先clear,然后再重复去找这个element再sendkeys。类似这种常用的操作都可以封装起来,放在一个Common类里(Common项目)而一些操作case放在另外的项目中。下面就是对上述例子进行封装操作。

namespace TestSelenium.Test
{
    [TestFixture]
    class Test
    {
        TestSelenium.Common.Common Testcorde = new Common.Common();
        [SetUp]
        public void Setup()
        {
            Testcorde.SetupTest();
        }
        [TearDown]
        public void TearDown()
        {
            Testcorde.TeardownTest();
        }
        [Test]
        public void Test01()
        {     
          Testcorde.TheLoginTest("URL","USERNAME","PASSWORD" );            
        }
    }
}

上面Test01就是登录操作的case,TheLoginTest(string baseurl, string username, string password)就是整个登录操作的方法。像是SetupTest、TeardownTest、SendKeys、Click、TheLoginTest都放在下面的Common类下。

namespace TestSelenium.Common
{
    public class Common
    {
        public IWebDriver driver;
        public void SetupTest()
        {
            driver = new InternetExplorerDriver(@"C:/AUTO");
            driver.Manage().Window.Maximize();
        }
        public void TeardownTest()
        {
            driver.Quit();
        }
        public void SendKeys(By by, string Message)
        {
            driver.FindElement(by).Clear();
            driver.FindElement(by).SendKeys(Message);
        }
        public void Click(By by)
        {
            driver.FindElement(by).Click();
        }
        public void TheLoginTest(string baseurl, string username, string password)
        {
            driver.Navigate().GoToUrl(baseurl + "/login");  
            SendKeys(By.Name("username"),username);      
            SendKeys(By.Name("password"), password);
            Click(By.XPath("//button[@type=‘submit‘]"));
        }
    }
}

ps.Common项目为类库输出类型,case项目需要引用Common项目并且保持都是ANYCPU生成。

时间: 2024-08-25 07:44:27

Webdriver(selenium2.0)+NUnit+C# (二)的相关文章

Webdriver(selenium2.0)+NUnit+C# (一)

之前在第一家公司的时候有接触过selenium web自动化,可是没有自己搭建过环境以及配置到现在差不多都忘光.为了再拾起它,特地整理了这篇博文...文笔不好,纯粹是为了记录,方便今后自己能看的懂.如有写的不正确的地方,请直接提出 准备: 安装Firefox(45.0.2)浏览器,并安装插件Firebug.FirePath Selenium IDE(2.9.1 https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/),安装成功后如

selenium2.0(WebDriver) API - 转载自:http://www.cnblogs.com/puresoul/p/3477918.html

1.1  下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guide:  http://seleniumhq.org/docs/ 官方API:  http://selenium.googlecode.com/git/docs/api/java/index.html 1.2.1  用webdriver打开一个浏览器 打开firefox浏览器: WebDriver driver

转:Selenium2.0介绍——WebDriver两种驱动浏览器的方式

如果之前熟悉Selenium RC,理解了Selenium RC是如何工作的,那么,当第一次接触Selenium WebDriver的时候,看到WebDriver居然可以不需要指定远端服务器的IP地址和端口号的,一定会惊讶的. 事实上,WebDriver有两种方式“驱动”浏览器的方式.1. Selenium Server:和Selenium RC一样的,通过指定远端服务器IP地址和端口号,由这个远端服务器来驱动浏览器.2. 直接调用:无须指定任何服务器IP地址和端口号.直接使用本地的浏览器(只要

selenium2.0(WebDriver) API

1.1  下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guide:  http://seleniumhq.org/docs/ 官方API:  http://selenium.googlecode.com/git/docs/api/java/index.html 1.2.1  用webdriver打开一个浏览器 打开firefox浏览器: WebDriver driver

转:Selenium2.0之grid学习总结

(一)介绍: Grid的功能: 并行执行 通过一个中央管理器统一控制用例在不同环境.不同浏览器下运行 灵活添加变动测试机 (二)快速开始 这个例子将介绍如何使用selenium2.0的grid,并且注册一个WebDriver节点.如何使用java调用grid.这里Hub和节点都跑在同一台机器上面,但是如有你需要的话可以复制selenium-server-standalone到分布式机器上去实现. 注:selenium-server-standalone的jar包中已经包含了grid,WebDri

Selenium2.0介绍

Selenium1.0和WebDriver合并创建Selenium2.0. Selenium1.0是第一个基于浏览器的开源自动化测试工具,适用于任何支持javascript浏览器.正是由于它的这一特点,导致了他的缺点,每一个浏览器对于执行javascript都有很严格的安全限制,不支持Javascript调用任何当前页面所在服务器以外的其他任何东西,以防止用户被恶意脚本攻击.这也导致了selenium在某些场景下的测试工作变得很困难,比如可以在瞬间打开一个新Chrome浏览器,但不能上传文件或者

转:WebDriver(Selenium2) 判断页面是否刷新的方法

Java代码   public static boolean waitPageRefresh(WebElement trigger) { int refreshTime = 0; boolean isRefresh = false; try { for (int i = 1; i < 60; i++) { refreshTime = i; trigger.getTagName(); Thread.sleep(1000); } } catch (StaleElementReferenceExcep

在selenium2.0中使用selenium1.0的API

Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium RC API那么方便.Selenium2.0提供了使用Selenium RC API的方法: 1 // You may use any WebDriver implementation. Firefox is used hereas an example 2 WebDriver driver =

Selenium2.0+TestNG+Ant+Jenkins自动化测试浅尝

当前常用自动化测试工具 Web自动化测试工具:QTP .selenium等 性能自动化测试工具:loadrunner.jmeter等 接口自动化测试工具:SoapUI.postman等 手机自动化测试工具:robotium.appium等 自动化脚本录制 Selenium 1.0包括:selenium IDE.selenium Grid.selenium RC三部分. Selenium IDE 是嵌入到Firefox浏览器中的一个插件,实现简单的浏览器操作的录制与回放功能IDE录制的脚本可以可以