65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Visual Studio 2017/C# 入门 Selenium 3.7

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5投票s)

2017年11月30日

CPOL

6分钟阅读

viewsIcon

68972

downloadIcon

125

本文提供了使用 Selenium 3.7.0 和 Visual Studio 2017/C# 编写测试用例的详细步骤。

目录

  1. 引言
  2. 下载 Visual Studio 2017 社区版
  3. 创建新解决方案(和测试项目)
  4. 创建 Firefox 浏览器测试用例
  5. 添加 Selenium/Firefox NuGet 包
  6. 执行 Firefox 测试用例
  7. 创建 Chrome 浏览器测试用例
  8. 添加 Selenium/Chrome Nuget 包
  9. 执行 Chrome 测试用例
  10. 创建 Edge 浏览器测试用例
  11. 添加 Selenium/Edge NuGet 包
  12. 设置 Edge 驱动
  13. 执行 Edge 测试用例
  14. 执行所有测试用例
  15. 结论
  16. 历史

引言

在本文中,我们将使用以下浏览器对最新版本的 Selenium(截至 2017 年 11 月 30 日的版本 3.7.0)进行测试(无意中双关 J):

  1. Firefox
  2. Chrome
  3. MS Edge

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

  1. 打开 Google 主页
  2. 文本框 中输入搜索词
  3. 点击“Google Search”按钮
  4. 验证测试结果页面是否已显示

我们使用 Visual Studio 2017 社区版和 C# 来自动化测试用例。我们为上述每种浏览器编写一个单独的测试用例。我们特意将创建测试用例的说明分开,以便单独进行实验。

那么,让我们开始吧!

下载 Visual Studio 2017 社区版

下载并安装 Visual Studio 2017 社区版(如果您已安装 Visual Studio 2017,请跳过此步骤 - 任何版本都可以)。

链接如下:

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

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

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

  2. 在“新建项目”模板中,在窗格左侧选择“Visual C#”,然后选择“测试”。在右侧,选择“单元测试项目”。

  3. 选择项目的名称和位置,然后单击“确定”。

创建 Firefox 浏览器测试用例

既然我们已经创建了一个“测试项目”,就让我们开始为 Firefox 创建测试用例。

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

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

  3. 对于弹出的对话框,单击“”。这将同时重命名文件和类。将类名和包含它的文件名设为相同是一个最佳实践。

  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 浏览器测试用例

添加一个新测试用例,并给它命名:“Should_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 浏览器测试用例

添加一个新测试用例,并给它命名,例如:“Should_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 创建测试用例的过程与 Firefox 和 Chrome 非常相似。

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

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

设置 Edge 驱动

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

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

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

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

  4. 滚动到“设置”窗口的底部。

  5. 记下发布号。在这种情况下是“14393”。
  6. 访问以下页面:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
  7. 在下载部分,记下发布号。

  8. 点击“Release 14393”(即,为已安装的浏览器版本选择正确的版本)。
  9. 将“MicrosoftWebDriver.exe”保存到一个位置。

执行 Edge 测试用例

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

执行所有测试用例

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

结果应如下所示。顺便说一句,请注意 Edge 的执行速度比 Firefox 和 Chrome 快。

结论

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

历史

  • 2017 年 11 月 30 日:初始版本
© . All rights reserved.