使用Visual Studio 2017/C#开始使用Selenium 3.7

目录

介绍

下载Visual Studio 2017社区版

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

为Firefox浏览器创建测试用例

添加Selenium/Firefox NuGet包

执行Firefox测试用例

为Chrome浏览器创建测试用例

添加Selenium/Chrome Nuget包

执行Chrome测试用例

为Edge浏览器创建测试用例

添加Selenium/Edge NuGet包

设置边缘驱动程序

执行边缘测试用例

执行所有测试用例

结论


介绍

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

  1. Firefox
  2. Chrome
  3. MS Edge

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

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

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

那么,让我们开始吧!

下载Visual Studio 2017社区版

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

链接在这里:

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

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

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 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");
   }
  }
 }
}

执行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 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");
    }
   }
  }
 }

添加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 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");
    }
   }
  }
 }

添加Selenium/Edge NuGet

Edge 创建测试用例的过程与FirefoxChrome的过程非常相似。

基本上,我们首先使用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执行速度比FirefoxChrome快。

结论

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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值