c# - 使用 vstest.console.exe 工具运行 Selenium 测试时显示测试的 URL

标签 c# selenium jenkins automated-tests

上下文

我配置了执行 Selenium C# 测试的 Jenkins 作业。 在构建作业时,我必须提供两个值:

  • 分支构建我的解决方案
  • 应测试的 URL

详细说明

Jenkins 执行以下步骤:

  • 结帐选定的分支机构
  • 将存储在 AppSettings 中的 URL 替换为用户提供的 URL(“TestedURL”)
  • 构建解决方案
  • 执行 vstest.console.exe工具(Visual Studio 2012自带,存在于以下路径:“Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow”)如下:

    vstest.console.exe "c:\myProject\bin\Debug\myProject.Test.dll"

问题

我想确保测试正确的 URL:我想在 Jenkins/vstest.console.exe 输出的控制台输出中显示 URL。

我根据 StackOverflow 上的不同答案修改了我的代码。

URL 在直接来自 Visual Studio 的测试输出中可见。不幸的是,我仍然没有在 vstest.console.exe/Jenkins 输出中看到任何测试 URL。

如何在输出中显示URL(如何修改printMessage方法)?

我的代码如下:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Configuration;
using System.Diagnostics;

namespace My.Test
{
    [TestClass]
    public class MyTests
    {
        IWebDriver driver;
        string baseURL;

        [TestInitialize]
        public void Initialize()
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // URL is replaced by the value provided in Jenkins
            baseURL = config.AppSettings.Settings["TestedURL"].Value;
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl(baseURL);
            printMessage(string.Format("Tested URL: '{0}'", baseURL));
        }

        private TestContext testContextInstance;
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private void printMessage(string message)
        {
            // Method should display custom message in both Visual Studio output and Jenkins (vstest.console.exe) output
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            Trace.Listeners.Add(new ConsoleTraceListener());
            Trace.WriteLine(message);
            Debug.WriteLine(message);
            TestContext.WriteLine(message);
            Console.WriteLine(message);
        }
    }
}

最佳答案

由于我找不到直接的解决方案,所以我找到了解决方法。

vstest.console.exe 有 /logger:trx 选项,它生成扩展名为 .trx 的文件。提到的文件是一个 xml,其中包含测试结果和标准输出。我注意到当我在测试方法中使用我的 printmessage 时,消息就在那里(在 trx 文件的 Std 输出中)。

让我提供生成的 .trx 文件的一部分:

<Output>
    <StdOut>Tested URL was 'http://someURL'

Debug Trace:
Tested URL: 'http://someURL'
Trying to log into as 'Incorrect account'

TestContext Messages:
Tested URL: 'http://someURL'
Trying to log into as 'Incorrect account'</StdOut>
      </Output>
    </UnitTestResult>

有一些工具可以将 .trx 转换为 .html。 因此 Jenkins 可以在控制台输出中显示测试结果链接。


但是

我有问题,因为我找不到可以正常工作并在生成的 html 文件中显示标准输出的工具。 从那时起,我编写了以下 Powershell 代码:

# set $inputTRXFile, $outputHTMLFile before running following lines
$results = ([xml] (Get-Content $inputTRXFile)).TestRun.Results.UnitTestResult
$results | select testname,outcome,duration, @{Name="Output";Expression={$_.Output.InnerText}} | ConvertTo-HTML -head $a -title "Test results" | Out-File $outputHTMLFile

关于c# - 使用 vstest.console.exe 工具运行 Selenium 测试时显示测试的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614724/

相关文章:

java - Maven Jenkins 构建失败

C#:读取由附加换行符分隔的文本文件

c# - AutoResetEvent 重置方法

c# - 将应用程序从 .net 4.0 降级到 3.5

selenium - 如何利用保存的Chrome密码来实现自动登录脚本,这样我就不必在脚本中输入纯文本密码?

jenkins - 访问 Jenkins v2.73.1 crumbIssuer REST API 时出现 404

c# - 自动将所有控件属性从 resx 文件移动到 Designer.cs 代码文件

python - 使用 click() webdriver selenium 函数 Python 时超时

java - WebDriver driver=new FirefoxDriver() 这是编译时绑定(bind)还是运行时绑定(bind)?

git - 同一台服务器上的 Jenkins 和 Git