目录

介绍

下载Visual Studio 2017社区版

创建一个新的解决方案(和一个测试项目)

为Firefox浏览器创建测试用例

添加Selenium/Firefox NuGet包

执行Firefox测试用例

为Chrome浏览器创建测试用例

添加Selenium/Chrome Nuget包

执行Chrome测试用例

为Edge浏览器创建测试用例

添加Selenium/Edge NuGet包

设置边缘驱动程序

执行边缘测试用例

执行所有测试用例

结论


  • 从Github下载最新代码

介绍

在本文中,我们使用以下浏览器试驾(无双关语)最新版本的Selenium(截至2017年11月30日的3.7.0版):

  1. Firefox
  2. Chrome
  3. MS Edge

对于本练习,我们使用以下测试用例:

  1. 打开谷歌主页
  2. 在搜索栏 textbox中输入搜索词
  3. 点击“谷歌搜索”按钮
  4. 验证是否显示测试结果页面

我们使用Visual Studio 2017和C#的社区版自动化测试用例。我们为上面提到的每个浏览器编写了一个单独的测试用例。我们特意划分了创建测试用例的指令,分别进行试验。

那么,让我们开始吧!

下载Visual Studio 2017社区版

下载并安装Visual Studio 2017社区版(如果您已经安装了Visual Studio 2017,请跳过——任何版本都可以使用)。

链接在这里:

  • Download Visual Studio Tools - Install Free for Windows, Mac, Linux

创建一个新的解决方案(和一个测试项目)

以下是创建测试项目的步骤。

1、打开Visual Studio并创建一个新项目。单击“文件”,选择“新建”,然后选择“项目... ”

2、从“New Project”模板中,选择窗格左侧的“Visual C#”,然后选择“Test”。在右侧,选择“单元测试项目”。

3、为您的项目选择一个名称和位置,然后单击“确定”。

为Firefox浏览器创建测试用例

现在我们创建了一个“测试项目”,让我们专注于为Firefox创建一个测试用例。

1、单元测试文件使用默认名称“UnitTest1.cs”创建。

2、让我们将名称更改为“GoogleSearch.cs ”。重命名文件的最简单方法是在解决方案资源管理器中右键单击“UnitTest1.cs”,然后选择重命名。

3、在弹出的对话框中单击“Yes”。这将重命名文件和类。最佳做法是为类和包含它的文件提供相同的名称。

4、接下来,将“Method1”重命名为“Should_Search_Using_Firefox”:

5、重命名后,该方法应如下所示:

添加Selenium/Firefox NuGet包

在我们继续创建测试用例之前,让我们将所需的包添加到项目中:

  • Selenium.webdriver
  • Selenium.Support
  • Selenium.Firefox.Webdriver

1、在解决方案资源管理器中右键单击项目名称,然后选择“管理 NuGet ... ”:

2、在NuGet窗口中,进行以下选择:

  1. 选择“浏览”。
  2. 在搜索框中输入“selenium”。
  3. 从搜索结果中选择“ Selenium.WebDriver ”。
  4. 在“项目”旁边打勾。
  5. 点击“安装”。

3、按照相同的过程安装“Selenium.Support ”:

4、再次,按照相同的过程安装“Selenium.Firefox.Webdriver”:

5、在测试文件顶部添加“using”语句,并创建Firefox驱动的实例,如下图:

测试用例的完整代码如下所示:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Firefox;namespace Selenium3_7_VisualStudio2017
{[TestClass]public class GoogleSearchEngineUsingFirefox{[TestMethod]public void Shoud_Search_Using_Firefox(){// Initialize the Firefox Driverusing(var driver = new FirefoxDriver()){// 1. Maximize the browserdriver.Manage().Window.Maximize();// 2. Go to the "Google" homepagedriver.Navigate().GoToUrl("http://www.google.com");// 3. Find the search textbox (by ID) on the homepagevar searchBox = driver.FindElementById("lst-ib");// 4. Enter the text (to search for) in the textboxsearchBox.SendKeys("Automation using selenium 3.0 in C#");// 5. Find the search button (by Name) on the homepagevar searchButton = driver.FindElementByName("btnK");// 6. Click "Submit" to start the searchsearchButton.Submit();// 7. Find the "Id" of the "Div" containing results stats,// located just above the results table.var searchResults = driver.FindElementById("resultStats");}}}
}

执行Firefox测试用例

在测试资源管理器中右键单击测试用例,然后选择“运行选定的测试”:

(例如,您也可以在方法本身内部右键单击,例如第13行之后的任何位置):

为Chrome浏览器创建测试用例

添加一个新的测试用例,并为其命名:'Shoud_Search_Using_Chrome'。

这是完整的代码:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Chrome;namespace Selenium3_7_VisualStudio2017
{[TestClass]public class GoogleSearchEngineUsingChrome{[TestMethod]public void Shoud_Search_Using_Chrome(){// Initialize the Chrome Driverusing(var driver = new ChromeDriver()){// 1. Maximize the browserdriver.Manage().Window.Maximize();// 2. Go to the "Google" homepagedriver.Navigate().GoToUrl("http: //www.google.com");// 3. Find the search textbox (by ID) on the homepagevar searchBox = driver.FindElementById("lst - ib");// 4. Enter the text (to search for) in the textboxsearchBox.SendKeys("Automation using selenium 3.0 in C#");// 5. Find the search button (by Name) on the homepagevar searchButton = driver.FindElementByName("btnK");// 6. Click "Submit" to start the searchsearchButton.Submit();// 7. Find the "Id" of the "Div" containing results statsvar searchResults = driver.FindElementById("resultStats");}}}}

添加Selenium/Chrome Nuget包

为Chrome创建测试用例的过程与Firefox非常相似。基本上,我们使用NuGet管理器来安装Chrome.Webdriver,然后我们添加一个新的测试用例。以下是安装Chrome.Webdriver。

  1. 右键单击项目(或解决方案)
  2. 选择“管理解决方案的 NuGet ... ”
  3. 从 NuGet 窗口中选择“浏览
  4. 在搜索框中输入“Selenium
  5. 从搜索结果中选择“Selenium.Chrome.Webdriver
  6. 单击“安装

执行Chrome测试用例

要执行测试用例,请在测试资源管理器中右键单击测试用例,然后选择“运行选定的测试”。例如,您还可以在方法本身内部右键单击,例如第13行之后的任何位置。

为Edge浏览器创建测试用例

添加一个新的测试用例,并为其命名,例如“Shoud_Search_Using_EdgeBrowser”。

下面是测试用例的完整代码:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Edge;namespace Selenium3_7_VisualStudio2017
{[TestMethod]public class GoogleSearchEngineUsingEdgeBrowser{// Edge driver full path: @"C:\SeleniumEdgeDriver\MicrosoftWebDriver.exe"string edgeDriverLocation = @ "C:\SeleniumEdgeDriver";[TestMethod]public void Shoud_Search_Using_EdgeBrowser(){// Initialize the IE Driverusing(var driver = new EdgeDriver(edgeDriverLocation)){// 1. Maximize the browserdriver.Manage().Window.Maximize();// 2. Go to the "Google" homepagedriver.Navigate().GoToUrl("http://www.google.com");// 3. Find the search textbox (by ID) on the homepagedriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);var searchBox = driver.FindElementById("lst-ib");// 4. Enter the text (to search for) in the textboxsearchBox.SendKeys("Automation using selenium 3.0 in C#");// 5. Find the search button (by Name) on the homepagevar searchButton = driver.FindElementByName("btnK");// 6. Click "Submit" to start the searchsearchButton.Submit();// 7. Find the "Id" of the "Div" containing results stats,// just before the results table.var searchResults = driver.FindElementById("resultStats");}}}}

添加Selenium/Edge NuGet包

为 Edge 创建测试用例的过程与Firefox和Chrome的过程非常相似。

基本上,我们首先使用NuGet管理器来安装Edge.Webdriver。以下是步骤:

  1. 右键单击项目。
  2. 选择“管理NuGet... ”。
  3. 从NuGet窗口中选择“浏览”。
  4. 在搜索框中输入“Selenium”。
  5. 从搜索结果中选择“Selenium.Webdriver.IEDriver”。
  6. 单击“安装”。

设置边缘驱动程序

在为Edge浏览器创建测试用例时,我遇到了2个问题。

  • 安装EdgeDriver
  • 在定位文本框之前添加超时(10秒)(测试用例中的第25行)

您可能需要手动下载驱动程序并将其放置在测试用例中需要引用的位置。以下是使边缘驱动程序正常工作的步骤。

  1. 从桌面打开边缘浏览器。
  2. 单击浏览器右上角的省略号“ ... ”。
  3. 点击“设置”。

4、滚动到“设置”窗口底部:

5、记下版本号。在这种情况下“14393”。

6、导航到以下页面:Microsoft Edge Driver - Microsoft Edge Developer

7、请注意下载部分下的版本号:

8、点击“Release 14393”(即为安装的浏览器版本选择正确的版本)。

9、将“MicrosoftWebDriver.exe”保存到一个位置。

执行边缘测试用例

在“测试资源管理器”中,右键单击测试用例并选择“运行选定的测试”。例如,您还可以在方法本身内部单击,例如第 13 行之后的任何位置。

执行所有测试用例

如果您实现了所有测试用例,请在测试资源管理器中右键单击项目名称并选择“运行选定的测试”:

结果应如下所示。作为旁注,请注意Edge执行速度比Firefox和Chrome快。

结论

在这篇介绍性文章中,我们介绍了为三种浏览器编写测试用例的详细步骤:Firefox、Chrome和Edge。我们使用Selenium 3.7.0和Visual Studio 2017社区版/C#编写测试用例。我们特意为每个测试用例提供了单独的说明,以帮助初学者完成整个过程,一次一个测试用例。

https://www.codeproject.com/Articles/1217887/Getting-Started-with-Selenium-3-7-using-Visual-Stu

使用Visual Studio 2017/C#开始使用Selenium 3.7相关推荐

  1. vs2015编译linux源码,使用Visual Studio 2017(VS2017)编译OpenCC 1.0.4 (Open Chinese Convert)源代码...

    摘要:本文介绍了Win7 64位环境下OpenCC(Open Chinese Convert) 1.0.4源码编译全过程,除VS2017外,VS2013以上版本也应该基本同样适用,其他版本的Windo ...

  2. Visual Studio 2017 - Windows应用程序打包成exe文件(1)- 工具简单总结

    最近有对一个Windows应用程序少许维护和修改.修改之后要发布新的exe安装文件,打包exe文件时,遇到了很头疼的问题,还好最后解决了,记录一下. Visual Studio版本:Visual St ...

  3. Visual Studio 2017新版发布,极大提高开发效率丨附下载

    2019独角兽企业重金招聘Python工程师标准>>> 最新的Visual Studio 2017免费下载[包含Professional.Enterprise.Community版本 ...

  4. Visual Studio 2017正式版发布全纪录

    2019独角兽企业重金招聘Python工程师标准>>> 又是一年发布季,微软借着Visual Studio品牌20周年之际,于美国太平洋时间2017年3月7日9点召开发布会议,宣布正 ...

  5. Visual Studio 2017 第三方依赖设置,附加依赖项和附加库目录

    Visual Studio 2017 第三方依赖.需要做的事情有:1.指定头文件所在目录.2.指定需要使用到的lib.3.指定lib文件夹,也就是代码的实现部分所在的目录. 附加依赖项 是指输入项,如 ...

  6. Windows 10 Visual Studio 2017 安装配置 Apache Thrift (C++)

    最近需要使用Thrift,所以在网上看了很多资料,不过很多教程都不够详细完整,导致我花了不少时间安装配置.在这里我把我配置的过程写下来和大家分享. 1 介绍 Apache Thrift 是一个跨语言的 ...

  7. Visual Studio 2017 15.7预览版发布

    \ 看新闻很累?看技术新闻更累?试试下载InfoQ手机客户端,每天上下班路上听新闻,有趣还有料! \ \\ Visual Studio 2017已经发布一年多了,微软一直持续定期推出更新.第7个预览版 ...

  8. Create C++ Windows Forms Application in Visual Studio 2017

    Background: From Windows Forms Application in Visual C++ ???!!!!: "MS removed the WinForms C++/ ...

  9. Visual Studio 2017 15.8概览

    Microsoft正式发布VS2017的第八次更新,即15.8.15.8提供了今年夏天预览的大量新特性,包括Code Cleanup.IDE支持多重查补(Multiple Caret).Visual ...

最新文章

  1. 时间序列举例--------协方差+相关系数+随机游走+平稳性
  2. frame框架的显示隐藏操作 (转)
  3. swing LayoutManager 和多态
  4. 虚拟现实大会ChinaVR2015报告之-From Visual Content to Virtual Reality Data-driven Intelligence Production
  5. 全面降低windows系统的安全隐患 (四)
  6. Noip 2013 练习
  7. python汉诺塔问题_Python汉诺塔问题
  8. 最最基础的Android倒计时应用
  9. Microsoft Office Visio 2007
  10. MSDN下载Windows7系统与安装
  11. 【测试】软件测试的生命周期
  12. python中减号怎么打_我的python将减号与破折号混淆
  13. 推荐一个Github上很酷的开源项目——The Octo-Bouncer
  14. python深浅拷贝应用_简述Python的深浅拷贝以及应用场景?
  15. 关于openssl 的几个key的生成方式
  16. 泰拉瑞亚 阿里云服务器搭建记录
  17. python爬取小说写入txt_基于python爬取小说章节源代码内容存为文本实现小说下载...
  18. cad文档服务器部署,云服务器安装cad
  19. 用人篇-曾国藩家书整理
  20. android的app,用java程序开发

热门文章

  1. 去除面部黑色素小妙招_去除暗黄皮肤的小妙招 7招让你白皙动人
  2. 计算机d盘不显示容量,电脑D盘可用空间小,可是看不到文件
  3. linux打开文件命令_详解Linux中3个文件查找相关命令
  4. 灵感库 | 让设计师欲罢不能的设计网站
  5. 配图做设计、找图找灵感交给素材路SUCAI63
  6. 促销惊喜活动优惠海报设计,可临摹PSD分层格式
  7. C++ 判断某一个数是否为质数
  8. MyBatis的总结(下)
  9. linux内存管理:kmap、vmap、ioremap
  10. Valgrind动态分析工具