版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年防雾涂料项目投资申请报告代可行性研究报告
- 山东省临沂一中2024-2025学年高三11月测试语文试题含答案
- 第六章 社区营养管理和营养干预课件
- 五年级数学(小数除法)计算题专项练习及答案
- 2024年期房屋建筑施工协议范例大全
- 2024电力供应与消费合规协议样式
- 2024育儿嫂职责与服务协议书
- 2024无息借款协议格式
- 2024公司绩效奖金分配协议样本
- 单休就业协议范本2024年适用
- 2023-2024学年江苏省南京玄武区中考语文最后一模试卷含解析
- 糖皮质激素的合理应用课件
- 五年级四则混合运算
- 苏教版五年级上册第七单元解决问题的策略作业设计
- 《变压器有载分接开关振动声学现场测试方法》
- 管桁架施工方案
- 全国高考物理高考题说题比赛一等奖课件物理说题李焕景
- 华为MA5800配置及调试手册
- 二轮复习微专题湖泊专题
- 2024年德阳发展控股集团有限公司招聘笔试参考题库附带答案详解
- 餐前检查表(标准模版)
评论
0/150
提交评论