PS:这次用公司的项目来练手,希望公司不会起诉我,因为我绝对是抱着学习的态度,没有任何恶意。仅供交流学习。

该项目是基于SharePoint平台所开发的门户网站,为了切身感受一下Selenium 2.0我决定自己动手写一个自动化测试用例,而不是通过录制的方式,以加深我对一些web操作的理解。

我设计的测试用例是:检查Staff Spotlight中所包含的item对应三级页面显示的信息是否正确。具体逻辑是,在英语浏览器下检查后台list中英语Column的value和三级page中对应Column的value是否一致,在日语浏览器下检查后台list中日语Column的value和三级page中对应Column的value是否一致,如果是英语浏览器下后台list中没有勾选“English Ready”项,则在三级page中显示日语Column的value。

三级page如下:

点击页面右上角的“Edit Item”按钮可以跳转到后台list中相应的item编辑页面;点击上方的“日本语”按钮可以进行英语和日语的语言切换,点击“日本语”将切换到日本语,再点击“English”将切换到英语。

我们本次要取的页面元素就是Department,Location,Title以及Join Date的页面value。然后点击“Edit Item”按钮跳转到后台item编辑页面,按“F12”,找到对应要获取的后台元素值:

获取到对应的Column的value所在页面的元素,然后获取页面上显示的值,将其与三级page上的value按我们之前说过的逻辑进行比对,如果一样,测试通过;如果不一样,测试失败。

代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenQA.Selenium;
  7. using OpenQA.Selenium.IE;
  8. using OpenQA.Selenium.Support;
  9. using OpenQA.Selenium.Support.UI;
  10. using Selenium;
  11. using mySelenium;
  12. using System.Runtime.InteropServices;
  13. using System.Threading;
  14. using System.Diagnostics;
  15. using System.Net;
  16. using System.IO;
  17. using System.Globalization;
  18.  
  19. namespace mySelenium
  20. {
  21. class LoginSPSite
  22. {
  23. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  24. public static extern IntPtr GetForegroundWindow();
  25. static void Main(string[] args)
  26. {
  27. IWebDriver driver = new InternetExplorerDriver();
  28. IWebDriver iw = login(driver,"https://insite.ccqa11apps.com/Pages/default.aspx");
  29. INavigation navi = iw.Navigate();
  30. string language = checkLanguage(iw);
  31. if (language == "English")
  32. {
  33. //Check the English page.(检查英语页面信息。)
  34. Console.ForegroundColor = ConsoleColor.Yellow;
  35. Console.WriteLine("--English page check--");
  36. checkPageInfo(iw,"En");
  37. //Turn to the Japanese page.(转换到日语页面。)
  38. navi.GoToUrl("https://insite.ccqa11apps.com/Pages/default.aspx");
  39. waitUntilPageLoaded(iw, "ctl00_ctl21_ToggleLanguage");
  40. iw.FindElement(By.Id("ctl00_ctl21_ToggleLanguage")).Click();
  41. //Check the Japanese page.(检查日语页面信息。)
  42. Console.ForegroundColor = ConsoleColor.Yellow;
  43. Console.WriteLine("--Japanese page check--");
  44. checkPageInfo(iw,"Ja");
  45. }
  46. //Language is "Japanese".
  47. else
  48. {
  49. //Check the Japanese page.(检查日语页面信息。)
  50. Console.ForegroundColor = ConsoleColor.Yellow;
  51. Console.WriteLine("--Japanese page check--");
  52. checkPageInfo(iw,"Ja");
  53. //Turn to the English page.(转换到英语页面。)
  54. navi.GoToUrl("https://insite.ccqa11apps.com/Pages/default.aspx");
  55. waitUntilPageLoaded(iw, "ctl00_ctl21_ToggleLanguage");
  56. iw.FindElement(By.Id("ctl00_ctl21_ToggleLanguage")).Click();
  57. //Check the English page.(检查英语页面信息。)
  58. Console.ForegroundColor = ConsoleColor.Yellow;
  59. Console.WriteLine("--English page check--");
  60. checkPageInfo(iw, "En");
  61. }
  62. }//Get the 3rd page info.(从三级page中获取指定的Column的value,需要传入IWebDriver对象以及当前页面的语言种类。)
  63. private static void checkPageInfo(IWebDriver iw,string LanType)
  64. {
  65. //Check 3rd page with the format: "https://insite.ccqa11apps.com/_layouts/15/InSite/pages/StaffDetails.aspx?id=" under the English environment.
  66. INavigation navi = iw.Navigate();
  67. for (int i = ; i <= ; i++)
  68. {
  69. Console.ForegroundColor = ConsoleColor.White;
  70. Console.WriteLine("This is the " + i + " staff item check info:");
  71. navi.GoToUrl("https://insite.ccqa11apps.com/_layouts/15/InSite/pages/StaffDetails.aspx?id=" + i.ToString());
  72. var eles = iw.FindElements(By.ClassName("apps-staffdetail-content-splitline"));
  73. //Get the info from the 3rd page.(获取三级page上Column的value信息。)
  74. StaffSpotlight staffSpotlight = new StaffSpotlight();
  75. staffSpotlight.Department = eles[].FindElement(By.ClassName("apps-staffdetail-content-splitline-bottom")).Text;
  76. staffSpotlight.Location = eles[].FindElement(By.ClassName("apps-staffdetail-content-splitline-bottom")).Text;
  77. staffSpotlight.Title = eles[].FindElement(By.ClassName("apps-staffdetail-content-splitline-bottom")).Text;
  78. staffSpotlight.JoinDate = eles[].FindElement(By.ClassName("apps-staffdetail-content-splitline-bottom")).Text;
  79. //Find the "Edit Item" button.(找到“Edit Item”按钮并点击。)
  80. iw.FindElement(By.ClassName("apps-staffdetail-edit")).Click();
  81. //Wait until the element on page loaded.(等待页面元素加载完成。)
  82. waitUntilPageLoaded(iw, "Inpex_Department_$containereditableRegion");
  83. //Get the info from the backend list.(获取后台list中Column的value信息。)
  84. StaffSpotlight staffInfoFromBackend = new StaffSpotlight();
  85. staffInfoFromBackend.Department = iw.FindElement(By.Id("Inpex_Department_$containereditableRegion")).FindElement(By.ClassName("valid-text")).Text;
  86. staffInfoFromBackend.Location = iw.FindElement(By.Id("Inpex_Office_$containereditableRegion")).FindElement(By.ClassName("valid-text")).Text;
  87. //Job title is the title under English IE.(Title是日语浏览器中显示的Title,EnTitle是英语浏览器中显示的Title。)
  88. staffInfoFromBackend.Title = iw.FindElement(By.Id("Inpex_Job_Title_30756774-5931-4844-bac1-a2f463d04ca0_$TextField")).GetAttribute("value").ToString();
  89. staffInfoFromBackend.EnTitle = iw.FindElement(By.Id("Inpex_Job_Title_E_ef6d11a7-f73e-4885-bef1-527a1c03c924_$TextField")).GetAttribute("value").ToString();
  90. staffInfoFromBackend.JoinDate = iw.FindElement(By.Id("Inpex_Join_Date_b133dcfe-4e0c-4f0f-8b82-92a549516e6d_$DateTimeFieldDate")).GetAttribute("value").ToString();
  91. //Compare the actual 3rd info with the expected backend info.(将三级page中的Column value和预期value比较。)
  92. Compare("Department", staffSpotlight.Department, staffInfoFromBackend.Department);
  93. Compare("Location", staffSpotlight.Location, staffInfoFromBackend.Location);
              //英语页面的Title信息比对检查逻辑。
  94. if (LanType == "En")
  95. {
  96. try
  97. {
  98. //If can get the element underneath, it means "English Ready" has been checked.(如果可以在页面上获取到该元素,说明“English Ready”这一项是勾选的。)
  99. string ifChecked = iw.FindElement(By.Id("Inpex_E_Text_a355a229-a1bb-48f5-81dd-e428a0d4fbd2_$BooleanField")).GetAttribute("checked").ToString();
  100. Console.WriteLine("The Englisht ready has been checked: " + ifChecked);
  101. //Compare the title with the backend "En" one.(比对EnTitle和三级page中的Column value是否一致。)
  102. Compare("Title", staffSpotlight.Title, staffInfoFromBackend.EnTitle);
  103. }
  104. //If can not get the element by the ID above, it means "English Ready" has not been checked.(如果获取不到“English Ready”元素下的“checked” Attribute,说明没勾选。)
  105. catch(Exception ex)
  106. {
  107. WriteLog(ex);
  108. //Compare the title with the backend "Jp" one.(比对Title和三级page中的Column value是否一致。)
  109. Console.WriteLine("The Englisht ready has been checked: false");
  110. Compare("Title", staffSpotlight.Title, staffInfoFromBackend.Title);
  111. }
  112. }
              //日语页面的Title信息比对检查逻辑。
  113. else
  114. {
  115. staffInfoFromBackend.Title = iw.FindElement(By.Id("Inpex_Job_Title_30756774-5931-4844-bac1-a2f463d04ca0_$TextField")).GetAttribute("value").ToString();
  116. Compare("JapTitle", staffSpotlight.Title, staffInfoFromBackend.Title);
  117. }
  118. //Change the time format into the same.(统一转换三级page上的join date时间格式以及后台list item的join date时间格式,并进行比较。)
  119. string joinDateOnPage;
  120. string joinDateFromBackend;
  121. if (staffSpotlight.JoinDate != "")
  122. {
  123. DateTime dt = Convert.ToDateTime(staffSpotlight.JoinDate);
  124. joinDateOnPage = dt.ToString("yyyy-MM-dd");
  125. }
  126. else
  127. {
  128. joinDateOnPage = "";
  129. }
  130. if (staffInfoFromBackend.JoinDate != "")
  131. {
  132. DateTime dt2 = Convert.ToDateTime(staffInfoFromBackend.JoinDate);
  133. joinDateFromBackend = dt2.ToString("yyyy-MM-dd");
  134. }
  135. else
  136. {
  137. joinDateFromBackend = "";
  138. }
  139. Compare("JoinDate", joinDateOnPage, joinDateFromBackend);
  140. }
  141. }
  142.      //Compare value method.(传入:对应要检查的Column的标题,三级page中Column的value,后台list中item对应Column的value。)
  143. private static void Compare(string title, string p1, string p2)
  144. {
  145. if (p1 == p2)
  146. {
  147. Console.ForegroundColor = ConsoleColor.Green;
  148. Console.WriteLine(title + " info on the 3rd page has passed the check.");
  149. }
  150. else
  151. {
  152. Console.ForegroundColor = ConsoleColor.Red;
  153. Console.WriteLine(title + "info on the 3rd page has not passed the check.");
  154. Console.WriteLine("The info on the 3rd page: " + p1);
  155. Console.WriteLine("The info from the backend page: " + p2);
  156. }
  157. }
  158. //Check language method.(检查当前页面所处于的语言环境,判断是英语还是日语,并返回当前语言种类。)
  159. private static string checkLanguage(IWebDriver iw)
  160. {
  161. string currentLanguage;
  162. //Wait until the element on page loaded.
  163. waitUntilPageLoaded(iw, "ctl00_ctl21_ToggleLanguage");
  164. string language = iw.FindElement(By.Id("ctl00_ctl21_ToggleLanguage")).Text;
  165. if (language.Length == )
  166. {
  167. currentLanguage = "English";
  168. return currentLanguage;
  169. }
  170. else
  171. {
  172. currentLanguage = "Japanese";
  173. return currentLanguage;
  174. }
  175. }
  176. //Wait until loaded method.(等待网页加载完毕指定元素,如果没有加载完,获取不到,捕获异常,继续等待,直到指定元素加载完毕。)
  177. private static void waitUntilPageLoaded(IWebDriver iw, string element)
  178. {
  179. try
  180. {
  181. iw.FindElement(By.Id(element));
  182. }
  183. catch (Exception ex)
  184. {
  185. WriteLog(ex);
  186. Thread.Sleep();
  187. waitUntilPageLoaded(iw, element);
  188. }
  189. }
  190. //Login method.(登陆网站的方法,需要传入IWebDriver对象以及网站的url。)
  191. public static IWebDriver login(IWebDriver driver, string url)
  192. {
  193. INavigation navigation = driver.Navigate();
  194. navigation.GoToUrl(url);
  195. driver.FindElement(By.Id("overridelink")).Click();
  196. IntPtr myPtr = GetForegroundWindow();
  197. //IntPtr hWnd = FindWindow(null, "abc");
  198. if (myPtr != IntPtr.Zero)
  199. {
  200. //Send message to the window.
  201. System.Windows.Forms.SendKeys.SendWait("dswu");
  202. System.Windows.Forms.SendKeys.SendWait("{TAB}");
  203. System.Windows.Forms.SendKeys.SendWait("1qaz2wsxE");
  204. System.Windows.Forms.SendKeys.SendWait("{ENTER}");
  205. }
  206. return driver;
  207. }
  208. //Write log method.(将捕获到的异常打印到本地log文件中。)
  209. private static void WriteLog(Exception ex)
  210. {
  211. string logUrl = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\SeleniumAutoTest.txt";
  212. if (File.Exists(@logUrl))
  213. {
  214. using (FileStream fs = new FileStream(logUrl, FileMode.Append))
  215. {
  216. using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
  217. {
  218. try
  219. {
  220. sw.Write(ex);
  221. }
  222. catch (Exception ex1)
  223. {
  224. WriteLog(ex1);
  225. }
  226. finally
  227. {
  228. sw.Close();
  229. fs.Close();
  230. }
  231. }
  232. }
  233. }
  234. else
  235. {
  236. using (FileStream fs = new FileStream(logUrl, FileMode.CreateNew))
  237. {
  238. using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
  239. {
  240. try
  241. {
  242. sw.Write(ex);
  243. }
  244. catch (Exception ex1)
  245. {
  246. WriteLog(ex1);
  247. }
  248. finally
  249. {
  250. sw.Close();
  251. fs.Close();
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }

这里我创建一个类,用来存储三级page以及后台list中获取到的Staff item的信息:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace mySelenium
  7. {
  8. class StaffSpotlight
  9. {
  10. public string Department { get; set; }
  11. public string Location { get; set; }
  12. public string Title { get; set; }
  13. public string EnTitle { get; set; }
  14. public string JoinDate { get; set; }
  15. }
  16. }

按F5进行测试,结果如下:

测试通过。

覆盖多浏览器在Selenium也是很容易实现的,只需要把IE打开方法改用为指定浏览器所对应的打开方法:

IWebDriver driver = new InternetExplorerDriver();

把这句换了,之后的步骤对应做些修改即可。

自动化测试从某些方面讲确实是一劳永逸的,但是有很多时候自动化测试是完全没有必要的,尤其是项目周期很短的情况下,自动化的封装应该更偏向于具有普遍性的可复用过程,而不是业务逻辑。

有时候,产出决定付出。我们没必要为了一个两三月交付的项目写一堆自动化测试用例,否则得不偿失。自动化测试应该更偏向于长期上线、持续迭代的项目展开!否则最后失落感是一定有的。

请大家关注我的博客园,给我的文章点个赞!关注一个测试开发工程师的成长之路。

.NET项目web自动化测试实战——Selenium 2.0的更多相关文章

  1. web自动化测试实战之生成测试报告

    同志们,老铁们,继上篇文章 web自动化测试实战之批量执行测试用例 之后我们接着继续往下走,有人说我们运行了所有测试用例,控制台输入的结果,如果很多测试用例那也不能够清晰快速的知道多少用例通过率以及错 ...

  2. web自动化测试实战之批量执行测试用例

    01实战之分离测试固件 在UI 自动化测试中,不管编写哪个模块的测试用例,都需要首先在测试类中编写测试固件初始化WebDriver类及打开浏览器,执行登录,才能进行下一步业务逻辑的操作,测试用例执行完 ...

  3. 《零成本实现Web自动化测试--基于Selenium》第一章 自动化测试基础

    第一篇 Selenium 和WebDriver工具篇 第一章 自动化测试基础 1.1    初识自动化测试 自动化测试有两种常见方式 1.1.1 代码驱动测试,又叫测试驱动开发(TDD) 1.1.2 ...

  4. 开源Web自动化测试工具Selenium IDE

    Selenium IDE(也有简写SIDE的)是一款开源的Web自动化测试工具,它实现了测试用例的录制与回放. Selenium IDE目前版本为 3.6 系列,支持跨浏览器运行,所以IDE的UI从原 ...

  5. 《零成本实现Web自动化测试--基于Selenium》 第四章 Selenium 命令

    Selenium 命令,通常被称为Selenese,由一系列运行测试案例所需要的命令构成.按顺序排列这些命令就构成了测试脚本. 一. 验证颜面元素 1.Assertion或者Verification ...

  6. 《零成本实现Web自动化测试--基于Selenium》第二章 Selenium简介和基础

    第一部分 Selenium简介 1.Selenium 组建 1.1 Selenium-IDE Selenium-IDC是开发Selenium测试案例的集成开发环境.它像FireFox插件一样的工作,支 ...

  7. 《零成本实现Web自动化测试--基于Selenium》第三章 Selenium-IDE

    1.简介 Selenium-IDE(集成开发环境)是一种开发selenium测试案例的工具.是一种易用的Firefox插件.你可以通过文字菜单,在当前页面上选择一个UI元素,接着挑选与UI元素相关的s ...

  8. 《零成本实现Web自动化测试--基于Selenium》 第五章 Selenium-RC

    一. 简介 Selenium-RC可以适应更复杂的自动化测试需求,而不仅仅是简单的浏览器操作和线性执行.Selenium-RC能够充分利用编程语言来构建更复杂的自动化测试案例,例如读写文件.查询数据库 ...

  9. Web自动化测试 五 ----- selenium的等待和切换

    一.selenium的三种等待 当执行python的selenium代码时,如果需要定位一个元素或者点击一个元素,需要考虑到网速等多方面原因,导致页面加载速度慢,元素还未加载出来,这样就会导致找不到对 ...

随机推荐

  1. textwrap 模块

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #textwrap 模块 #http://www.cnblogs.com/hongten/p/python_t ...

  2. maven profiles、filters、resources学习笔记 及 常用 plugin demo

    这里只记了学习以下博客后,自己做的一个总结. 来源:http://blog.csdn.net/fengchao2016/article/details/72726101 profiles定义了一些不同 ...

  3. linux RAID10测试

    mdadm命令用于管理系统软件RAID硬盘阵列,格式为:"mdadm [模式] <RAID设备名称> [选项] [成员设备名称]". mdadm命令能够在Linux系统 ...

  4. Linear Algebra Courses

    Lecture 1:Demonstrate the columns of a matrix (imagine the vectors) in N-dimension space.How to mult ...

  5. Oracle创建库

    oracle创建表空间 SYS用户在CMD下以DBA身份登陆: 在CMD中打sqlplus /nolog 然后再 conn / as sysdba --如果路径不存在则要创建路径 --创建临时表空间 ...

  6. 【centos6.5 安装 node.js + npm】

    1.参考链接:http://www.jb51.net/article/116231.htm 2.下载的nodejs版本一定要是v0.12.10的(传送门:https://nodejs.org/en/b ...

  7. caffe与MATLAB连接踩的坑

    刚来公司用的服务器,分配给自己账号,安装了caffe,配好了Makefile.config中的MATLAB dir. 之后问题来了 解决方法: 在Makefile里面,大约第410行那一句话CXXFL ...

  8. 越狱iphone在cydia下插件后出现exit safe mode肿么办小教程

    http://bbs.app111.com/thread-318898-1-1.html 从简单的开始..最简单的点击状态栏会弹出来一个窗口,那窗口有三个选择请选择第二个,然后等待它重启,重启后还没消 ...

  9. GoogleMapsV3-----基础地图(自定义消息提示OverlayView) (转)

    <html> <head> <title> </title> <title></title>    <style type ...

  10. 转 ShowSlow+Yslow页面前端性能测试环境搭建

    ----//工具介绍 Yslow:YSlow是Yahoo发布的一款基于FireFox的插件. YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. ShowSlo ...