【问题标题】:Run Selenium Grid C# Project in Visual Studio在 Visual Studio 中运行 Selenium Grid C# 项目
【发布时间】:2020-02-24 05:39:13
【问题描述】:

我在 Visual Studio(控制台应用程序 n 类库)中实现了 Nunit selenium C# 测试。我在 Visual Studio 中的项目是控制台应用程序。我使用

启动了 selenium 网格
java -Dwebdriver.gecko.driver="..\jar\geckodriver.exe" -Dwebdriver.chrome.driver="..\jar\chromedriver.exe" -Dwebdriver.ie.driver="..\jar\IEDriverServer.exe" -jar ..\jar\selenium-server-standalone-3.14.0.jar -role hub -port 4444 

代码:

using Automation_Framework.Manager;
using NUnit.Framework;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Text;

namespace Automation_Framework.TestManager
{
    [TestFixture]
    class ChromeTestManager
    {
        private WebDriverManager webDriverManager;
        private IWebDriver driver;

        public ChromeTestManager()
        {
            webDriverManager = new WebDriverManager();
        }

        [SetUp]
        public void setup()
        {
            webDriverManager.createDriver("chrome");
            driver = webDriverManager.getDriver();
        }

        [Test]
        public void test()
        {
            driver.Url = "http://www.google.com.my";
            driver.Navigate();
        }

        [TearDown]
        public void shutdown()
        {
            driver.Close();
        }


    }
}

我曾尝试使用测试资源管理器执行,但它没有打开任何浏览器。我正在关注这个tutorial

问题:

  1. 如何在浏览器打开的情况下运行项目并查看所有操作?
  2. 如何使用 Nunit-console-runner 运行。

请帮助我。谢谢。

【问题讨论】:

  • 请帮帮我,因为这很紧急。
  • 你真的需要 WebDriverManager 吗?尝试只使用 IWebDriver 驱动程序,就像您遵循的教程一样。
  • @nicholas,只有TestProject 没有按预期工作吗,MainProject 也不是“OpeningAnyBrowser”吗?

标签: c# selenium


【解决方案1】:

我没有在 .Net 中使用网格,但这里是我的答案:

  • 您的命令只是注册一个集线器,它需要继续运行(打开浏览器并测试它是否正常工作)
  • 您需要在该集线器(不同端口)下注册您的节点(打开浏览器并测试它是否正常工作)

  • 在您的代码中,您应该使用“RemoteWebDriver”连接到集线器。 这些方面的东西(它在java中,但我希望它能给你一个起点)

    public class Gmail 
    {
       public WebDriver driver=null;    
       @Parameters("browser") //testng.xml
       @Test()
       public void GmailTest(String browser) 
       {
          System.out.println("Gmail " + browser);
          // RemoteWebdriver
          DesiredCapabilities cap = null;
    
          if(browser.equals("firefox")){
            cap = DesiredCapabilities.firefox();
            cap.setBrowserName("firefox"); 
            cap.setPlatform(Platform.ANY);
          }else if (browser.equals("iexplore")){
            cap = DesiredCapabilities.internetExplorer(); 
            cap.setBrowserName("iexplore");
            cap.setPlatform(Platform.WINDOWS);
          }
          driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);        
          driver.get("http://gmail.com");
          driver.findElement(By.id("Email")).sendKeys("abcd");  
          driver.quit();    
      }
    

我希望这会有所帮助。祝你好运

【讨论】:

    【解决方案2】:

    我假设:
    1.您已经在本地尝试过您的代码,并且当您在没有网格的机器上运行它时,您的测试正在打开浏览器。
    2. 您的节点已设置并注册到集线器。

    您需要:
    1.使用RemoteWebDriver:

    var uri = 'uri_to_your_grid_hub';
    var capabilities =  new ChromeOptions().ToCapabilities();
    var commandTimeout = TimeSpan.FromMinutes(5);
    var driver = new RemoteWebDriver(new Uri(uri),capabilities,commandTimeout)
    
    1. 将属性添加到类:[Parallelizable(ParallelScope.Self)] 以便与其他测试类并行运行您的测试。
    2. 为了验证集线器是否正在运行,请打开浏览器并导航到集线器计算机上的http://localhost:4444

    来源:

    How can I run NUnit(Selenium Grid) tests in parallel?
    Selenium Grid in C#
    Useful C# WebDriver examples
    Selenium Grid set up

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 2019-11-23
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多