VS2010+Selenium测试脚本设计V1.0_第1页
VS2010+Selenium测试脚本设计V1.0_第2页
VS2010+Selenium测试脚本设计V1.0_第3页
VS2010+Selenium测试脚本设计V1.0_第4页
VS2010+Selenium测试脚本设计V1.0_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、名目TOC o 1-3 h z u HYPERLINK l _Toc377741306 脚本结构设计 PAGEREF _Toc377741306 h 2 PAGEREF _Toc377741306 h PAGEREF _Toc377741306 h HYPERLINK l _Toc377741307 VS2010 MSTest单元测试框架 PAGEREF _Toc377741307 h 2 PAGEREF _Toc377741307 h PAGEREF _Toc377741307 h HYPERLINK l _Toc377741308 Setup & TearDown PAGEREF _Toc

2、377741308 h 2 PAGEREF _Toc377741308 h PAGEREF _Toc377741308 h HYPERLINK l _Toc377741309 Case分类组织 PAGEREF _Toc377741309 h 4 PAGEREF _Toc377741309 h PAGEREF _Toc377741309 h HYPERLINK l _Toc377741310 UnitTest文件TestMethod测试用例 PAGEREF _Toc377741310 h 5 PAGEREF _Toc377741310 h PAGEREF _Toc377741310 h HYPE

3、RLINK l _Toc377741311 例子 PAGEREF _Toc377741311 h 5 PAGEREF _Toc377741311 h PAGEREF _Toc377741311 h HYPERLINK l _Toc377741312 封装检查点 PAGEREF _Toc377741312 h 9 PAGEREF _Toc377741312 h PAGEREF _Toc377741312 h HYPERLINK l _Toc377741313 封装调用 PAGEREF _Toc377741313 h 10 PAGEREF _Toc377741313 h PAGEREF _Toc3

4、77741313 h HYPERLINK l _Toc377741314 例-safeClick PAGEREF _Toc377741314 h 13 PAGEREF _Toc377741314 h PAGEREF _Toc377741314 h HYPERLINK l _Toc377741315 对象管理与对象库设计 PAGEREF _Toc377741315 h 14 PAGEREF _Toc377741315 h PAGEREF _Toc377741315 h HYPERLINK l _Toc377741316 Open2Test的Framework for Selenium 2 PAG

5、EREF _Toc377741316 h 14 PAGEREF _Toc377741316 h PAGEREF _Toc377741316 h HYPERLINK l _Toc377741317 VS2010的App.config PAGEREF _Toc377741317 h 14 PAGEREF _Toc377741317 h PAGEREF _Toc377741317 h HYPERLINK l _Toc377741318 面对页面对象设计模式 PAGEREF _Toc377741318 h 15 PAGEREF _Toc377741318 h PAGEREF _Toc377741318

6、 h HYPERLINK l _Toc377741319 数据驱动 PAGEREF _Toc377741319 h 17 PAGEREF _Toc377741319 h PAGEREF _Toc377741319 h HYPERLINK l _Toc377741320 VS2010 数据驱动 Unit Test PAGEREF _Toc377741320 h 17 PAGEREF _Toc377741320 h PAGEREF _Toc377741320 h HYPERLINK l _Toc377741321 Excel PAGEREF _Toc377741321 h 17 PAGEREF _

7、Toc377741321 h PAGEREF _Toc377741321 h HYPERLINK l _Toc377741322 ADO.NET PAGEREF _Toc377741322 h 18 PAGEREF _Toc377741322 h PAGEREF _Toc377741322 h HYPERLINK l _Toc377741323 COM PAGEREF _Toc377741323 h 19 PAGEREF _Toc377741323 h PAGEREF _Toc377741323 h HYPERLINK l _Toc377741324 截图 PAGEREF _Toc377741

8、324 h 19 PAGEREF _Toc377741324 h PAGEREF _Toc377741324 h 脚本结构设计VS2010 MSTest单元测试框架Setup & TearDownClassInitializeTestInitialize()属性表示测试初始化跑的地方TestCleanup()属性表示是每次执行完测试类后清除的东西也就是测试类最终跑的地方TestMethod1. ClassInitialize and ClassCleanup: Since ClassInitialize and ClassCleanUp are static, they are only ex

9、ecuted once even though several instances of a test class can be created by MSTest. ClassInitialize executes in the instance of the test class corresponding to the first test method in the test class. Similarly, MSTest executes ClassCleanUp in the instance of the test class corresponding to the last

10、 test method in the test class.2. Execution Interleaving: Since each instance of the test class is instantiated separately on a different thread, there are no guarantees regarding the order of execution of unit tests in a single class, or across classes. The execution of tests may be interleaved acr

11、oss classes, and potentially even assemblies, depending on how you chose to execute your tests. The key thing here is all tests could be executed in any order, it is totally undefined.3. TextContext Instances: TestContexts are different for each test method, with no sharing between test methods. For

12、 example, if we have a Test Class: TestClass public class VSTSClass1 private TestContext testContextInstance; public TestContext TestContext get return testContextInstance; set testContextInstance = value; ClassInitialize public static void ClassSetup(TestContext a) Console.WriteLine(Class Setup); T

13、estInitialize public void TestInit() Console.WriteLine(Test Init); TestMethod public void Test1() Console.WriteLine(Test1); TestMethod public void Test2() Console.WriteLine(Test2); TestMethod public void Test3() Console.WriteLine(Test3); TestCleanup public void TestCleanUp() Console.WriteLine(TestCl

14、eanUp); ClassCleanup public static void ClassCleanUp() Console.WriteLine(ClassCleanUp); (This consists of 3 Test Methods, ClassInitialize, ClassCleanup, TestInitialize, TestCleanUp and anexplicit declaration of TestContext)The execution order would be as follows:Test1 Thread 1: new TestContext - Cla

15、ssInitialize - TestInitialize - TestMethod1 - TestCleanUpTest2 Thread 2: new TestContext - TestInitialize - TestMethod2 - TestCleanUpTest3 Thread 3: new TestContext - TestInitialize - TestMethod2 - TestCleanUp - ClassCleanUpThe output after running all the tests in the class would be:Class SetupTest

16、 InitTest1TestCleanUpTest InitTest2TestCleanUpTest InitTest3TestCleanUpClassCleanUpCase分类组织TestCategory分类,TestList中筛选组织UnitTest文件TestMethod测试用例留意UnitTest文件和TestMethod的命名规范例子using System;using System.Text;using System.Collections.Generic;using System.Linq;using Microsoft.VisualStudio.TestTools.UnitTe

17、sting;using OpenQA.Selenium;using OpenQA.Selenium.Firefox;/*namespace TestProject1 TestClass public class UnitTest1 TestMethod public void TestMethod1() IWebDriver driver = new FirefoxDriver(); driver.Url = http:/localhost:8080/AltoroJ/; driver.FindElement(By.Id(LoginLink).Click(); driver.Quit(); */

18、*namespace TestProject1 TestClass public class UnitTest1 IWebDriver driver; TestInitialize public void Setup() driver = new FirefoxDriver(); TestMethod public void TestMethod1() driver.Url = http:/localhost:8080/AltoroJ/; driver.FindElement(By.Id(LoginLink).Click(); TestMethod public void TestMethod

19、2() driver.Url = http:/localhost:8080/AltoroJ/; driver.FindElement(By.Id(query).SendKeys(hello!); TestCleanup public void TearDown() driver.Quit(); */*namespace TestProject1 TestClass public class UnitTest1 public static IWebDriver driver; private TestContext testContextInstance; public TestContext

20、TestContext get return testContextInstance; set testContextInstance = value; ClassInitialize public static void ClassSetup(TestContext a) driver = new FirefoxDriver(); TestMethod public void TestMethod1() driver.Url = http:/localhost:8080/AltoroJ/; driver.FindElement(By.Id(LoginLink).Click(); TestMe

21、thod public void TestMethod2() driver.Url = http:/localhost:8080/AltoroJ/; driver.FindElement(By.Id(query).SendKeys(hello!); ClassCleanup public static void ClassTearDown() driver.Quit(); */namespace TestProject1 TestClass public class UnitTest1 public static IWebDriver driver; private TestContext t

22、estContextInstance; public TestContext TestContext get return testContextInstance; set testContextInstance = value; ClassInitialize public static void ClassSetup(TestContext a) driver = new FirefoxDriver(); Ignore TestCategory(SmokingTest) TestMethod public void TestMethod1() driver.Url = http:/loca

23、lhost:8080/AltoroJ/; driver.FindElement(By.Id(LoginLink).Click(); TestMethod public void TestMethod2() driver.Url = http:/localhost:8080/AltoroJ/; driver.FindElement(By.Id(query).SendKeys(hello!); ClassCleanup public static void ClassTearDown() driver.Quit(); 封装检查点Page TitleElementPage TextJS Return

24、 Value把检查点封装到可重用的类例:namespace TestProject1 class Class1 public static IWebDriver driver; public Boolean CheckPoint_PageTitle(IWebDriver driver, string title) if (driver.Title.Equals(title) Console.WriteLine(Check Title Pass!); return true; else Console.WriteLine(Check Title Fail!); /Assert.Fail(Chec

25、k Title Fail!); return false; 封装调用参考:G:资料seleniumMyTestSeleniumTestProjectsrccomcoreWebDriverWrapper.javausing System;using System.Collections.Generic;using System.Linq;using System.Text;using OpenQA.Selenium;using Microsoft.VisualStudio.TestTools.UnitTesting;namespace TestProject1 class Class1 publ

26、ic static IWebDriver driver; public Boolean CheckPoint_PageTitle(IWebDriver driver, string title) if (driver.Title.Equals(title) Console.WriteLine(Check Title Pass!); return true; else Console.WriteLine(Check Title Fail!); /Assert.Fail(Check Title Fail!); return false; / public void safeClick(IWebDr

27、iver driver, String elementLocator) IWebElement webElement = null ; try webElement = driver.FindElement(By.Id(elementLocator); catch (Exception ex) webElement = null; if (webElement != null) webElement.Click(); Console.WriteLine(Click on element : + elementLocator); else Console.WriteLine(Element: +

28、 elementLocator + , is not available on + driver.Url); Assert.Fail(Element: + elementLocator + , is not available on + driver.Url); public Boolean isWebElementExist(IWebDriver driver, By selector) try driver.FindElement(selector); return true; catch (NoSuchElementException e) return false; public St

29、ring getWebText(IWebDriver driver, By by) try return driver.FindElement(by).Text; catch (NoSuchElementException e) return Textnot existed!; public void clickElementContainingText(IWebDriver driver, By by, String text) IList elementList = driver.FindElements(by); foreach (IWebElement e in elementList

30、) if (e.Text.Contains(text) e.Click(); break; / 表格对象处理 public String getCellText(IWebDriver driver , By by, String tableCellAddress) / 得到table元素对象 IWebElement table = driver.FindElement(by); / 对所要查找的单元格位置字符串进行分解,得到其对应行、列。 int index = tableCellAddress.Trim().IndexOf(.); int row = Int32.Parse(tableCellAddress.Substring(0, index); int cell = Int32.Parse(tableCellAddress.Substring(index + 1);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论