目录
介绍
下载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版):
- Firefox
- Chrome
- MS Edge
对于本练习,我们使用以下测试用例:
- 打开谷歌主页
- 在搜索栏 textbox中输入搜索词
- 点击“谷歌搜索”按钮
- 验证是否显示测试结果页面
我们使用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窗口中,进行以下选择:
- 选择“浏览”。
- 在搜索框中输入“selenium”。
- 从搜索结果中选择“ Selenium.WebDriver ”。
- 在“项目”旁边打勾。
- 点击“安装”。
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 Driver
using(var driver = new FirefoxDriver())
{
// 1. Maximize the browser
driver.Manage().Window.Maximize();
// 2. Go to the "Google" homepage
driver.Navigate().GoToUrl("http://www.google.com");
// 3. Find the search textbox (by ID) on the homepage
var searchBox = driver.FindElementById("lst-ib");
// 4. Enter the text (to search for) in the textbox
searchBox.SendKeys("Automation using selenium 3.0 in C#");
// 5. Find the search button (by Name) on the homepage
var searchButton = driver.FindElementByName("btnK");
// 6. Click "Submit" to start the search
searchButton.Submit();
// 7. Find the "Id" of the "Div" containing results stats,
// located just above the results table.
var searchResults = driver.FindElementById("resultStats");
}
}
}
}
在测试资源管理器中右键单击测试用例,然后选择“运行选定的测试”:
(例如,您也可以在方法本身内部右键单击,例如第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 Driver
using(var driver = new ChromeDriver())
{
// 1. Maximize the browser
driver.Manage().Window.Maximize();
// 2. Go to the "Google" homepage
driver.Navigate().GoToUrl("http: //www.google.com");
// 3. Find the search textbox (by ID) on the homepage
var searchBox = driver.FindElementById("lst - ib");
// 4. Enter the text (to search for) in the textbox
searchBox.SendKeys("Automation using selenium 3.0 in C#");
// 5. Find the search button (by Name) on the homepage
var searchButton = driver.FindElementByName("btnK");
// 6. Click "Submit" to start the search
searchButton.Submit();
// 7. Find the "Id" of the "Div" containing results stats
var searchResults = driver.FindElementById("resultStats");
}
}
}
}
为Chrome创建测试用例的过程与Firefox非常相似。基本上,我们使用NuGet管理器来安装Chrome.Webdriver,然后我们添加一个新的测试用例。以下是安装Chrome.Webdriver。
- 右键单击项目(或解决方案)
- 选择“管理解决方案的 NuGet 包... ”
- 从 NuGet 窗口中选择“浏览”
- 在搜索框中输入“Selenium”
- 从搜索结果中选择“Selenium.Chrome.Webdriver”
- 单击“安装”
要执行测试用例,请在测试资源管理器中右键单击测试用例,然后选择“运行选定的测试”。例如,您还可以在方法本身内部右键单击,例如第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 Driver
using(var driver = new EdgeDriver(edgeDriverLocation))
{
// 1. Maximize the browser
driver.Manage().Window.Maximize();
// 2. Go to the "Google" homepage
driver.Navigate().GoToUrl("http://www.google.com");
// 3. Find the search textbox (by ID) on the homepage
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
var searchBox = driver.FindElementById("lst-ib");
// 4. Enter the text (to search for) in the textbox
searchBox.SendKeys("Automation using selenium 3.0 in C#");
// 5. Find the search button (by Name) on the homepage
var searchButton = driver.FindElementByName("btnK");
// 6. Click "Submit" to start the search
searchButton.Submit();
// 7. Find the "Id" of the "Div" containing results stats,
// just before the results table.
var searchResults = driver.FindElementById("resultStats");
}
}
}
}
为 Edge 创建测试用例的过程与Firefox和Chrome的过程非常相似。
基本上,我们首先使用NuGet管理器来安装Edge.Webdriver。以下是步骤:
- 右键单击项目。
- 选择“管理NuGet包... ”。
- 从NuGet窗口中选择“浏览”。
- 在搜索框中输入“Selenium”。
- 从搜索结果中选择“Selenium.Webdriver.IEDriver”。
- 单击“安装”。
在为Edge浏览器创建测试用例时,我遇到了2个问题。
- 安装EdgeDriver
- 在定位文本框之前添加超时(10秒)(测试用例中的第25行)
您可能需要手动下载驱动程序并将其放置在测试用例中需要引用的位置。以下是使边缘驱动程序正常工作的步骤。
- 从桌面打开边缘浏览器。
- 单击浏览器右上角的省略号“ ... ”。
- 点击“设置”。
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