简体   繁体   English

在 Visual Studio 中运行 Selenium Grid C# 项目

[英]Run Selenium Grid C# Project in Visual Studio

I'm implemented Nunit selenium C# testing in visual studio (Console Application n Class Library).我在 Visual Studio(控制台应用程序 n 类库)中实现了 Nunit selenium C# 测试。 My project in visual studio is console application.我在 Visual Studio 中的项目是控制台应用程序。 I started the selenium grid using我开始使用硒网格

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 

Code:代码:

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


    }
}

I had tried execute using Test Explorer but it does not open any browser.我曾尝试使用测试资源管理器执行,但它没有打开任何浏览器。 I"m following this tutorial .我正在关注本教程

Questions:问题:

  1. How to run the project with browser open and see all actions?如何在浏览器打开的情况下运行项目并查看所有操作?
  2. How to run using Nunit-console-runner.如何使用 Nunit-console-runner 运行。

Please help me.请帮我。 Thanks.谢谢。

I haven't used grid in .Net but here my answer:我没有在 .Net 中使用过网格,但我的回答是:

  • your command is just register a hub, which needs to keep running (open a browser and test it is working)您的命令只是注册一个需要保持运行的集线器(打开浏览器并测试它是否正常工作)
  • you need to register your nodes under that hub (different ports) (open a browser and test it is working)您需要在该集线器(不同端口)下注册您的节点(打开浏览器并测试它是否正常工作)

  • in your code, you should use "RemoteWebDriver" to connect to the hub.在您的代码中,您应该使用“RemoteWebDriver”连接到集线器。 something along these lines (it is in java but I hope it gives you a starting point)沿着这些路线的东西(它是在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(); }

I hope this helps.good luck我希望这会有所帮助。祝你好运

I assume that:我假设:
1. You have tried your code locally and your test is opening the browser when you run it on your machine without the grid. 1. 你已经在本地尝试过你的代码,当你在没有网格的机器上运行它时,你的测试正在打开浏览器。
2. Your nodes are set up and registered with the hub. 2. 您的节点已设置并注册到集线器。

You need to:你需要:
1. Use RemoteWebDriver: 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. Add the attribute to a class: [Parallelizable(ParallelScope.Self)] in order to run your tests in parallel with other test classes.将属性添加到类: [Parallelizable(ParallelScope.Self)]以便与其他测试类并行运行您的测试。
  2. In order to verify whether the hub is running, open the browser and navigate to http://localhost:4444 on the hub machine.为了验证集线器是否正在运行,打开浏览器并导航到集线器机器上的http://localhost:4444

Sources:资料来源:

How can I run NUnit(Selenium Grid) tests in parallel? 如何并行运行 NUnit(Selenium Grid)测试?
Selenium Grid in C# C#中的硒网格
Useful C# WebDriver examples有用的 C# WebDriver 示例
Selenium Grid set up 硒网格设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM