【问题标题】:Display tested URL when running Selenium test using vstest.console.exe tool使用 vstest.console.exe 工具运行 Selenium 测试时显示测试 URL
【发布时间】:2016-07-27 13:44:08
【问题描述】:

上下文

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

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

详细说明

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

【问题讨论】:

    标签: c# selenium jenkins automated-tests


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 2019-08-18
      • 2021-04-21
      • 2018-10-31
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多