在nunit testresult xml中打印浏览器的名称

时间:2019-05-03 07:02:09

标签: c# selenium nunit selenium-chromedriver selenium-grid

我正在使用硒网格在多个浏览器上并行运行硒测试。这是我用测试治具定义测试的方式

public class ChromeDriver : RemoteWebDriver
{
    public ChromeDriver() : base(new Uri("http://12.8.4.211:4444/wd/hub"), new ChromeOptions())
    {
    }
}

public class FirefoxDriver : RemoteWebDriver
{
    public FirefoxDriver() : base(new Uri("http://12.8.4.211:4444/wd/hub"), new FirefoxOptions())
    {
    }
}


[TestFixture]
[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(FirefoxDriver))]
[Parallelizable(ParallelScope.Fixtures)]
public class MyTests<TWebDriver> : SeleniumTestFixture<TWebDriver> where TWebDriver : IWebDriver, new(){
     //Here goes my tests
}

但是当我看到nunit生成的结果xml时,我看不到xml中的浏览器名称。如何获取XML中的浏览器名称。我也存储屏幕截图,并将这些结果保存在硬盘上,具体取决于下面的测试名称

var screenshot = ((ITakesScreenshot)WebDriver).GetScreenshot();
screenshot.SaveAsFile(Path.Combine(dir, $"{TestContext.CurrentContext.Test.MethodName}.png"), ScreenshotImageFormat.Png);

因此,在运行测试时,它将根据测试名称保存屏幕截图。但是问题是,当我使用多个浏览器运行测试时,屏幕快照将被覆盖,因为它的名称与浏览器无关。它应该基于浏览器名称存储,因此我需要以某种方式获取浏览器名称并追加到屏幕快照名称。

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

以NUnit的测试名称(而不是方法名称)保存文件。这将包括在TestFixture属性中传入的浏览器驱动程序的名称。

您可以在以下位置访问它:

TestContext.CurrentContext.Test.Name

答案 1 :(得分:0)

这里有一种方法,而不是使用testname作为屏幕快照名称,您可以通过将Browsername(使用静态变量)和testname串联来处理。

例如,测试名称:ABC和浏览器名称:GoogleChrome,用于在Google Chrome中执行测试,因此现在可以将屏幕快照名称另存为GoogleChrome_ABC,并在Mozilla Firefox中并行执行时将其保存。浏览器名称:Firefox和屏幕截图将被保存作为Firefox_ABC。

答案 2 :(得分:0)

您应该能够使用所需的功能来获取浏览器名称。

IWebDriver.DesiredCapabilities("BrowserName")

此处是更多信息的链接。 https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Remote_DesiredCapabilities.htm