将 ITestOutputHelper 与 Selenium、xUnit 和 C# 结合使用
Using ITestOutputHelper with Selenium, xUnit and C#
我最近将一个项目从 NUnit 切换到 xUnit,以便可以使用 ITestOutputHelper 输出到日志。
该项目是一个相当标准的布局
功能文件->步骤类->页面类->帮助类。包括在助手 classes 中,我们也有 hooks.class。我正在使用 xUnit 跑步者。
所以在我的钩子中 class 我创造了这个
private readonly ScenarioContext _scenarioContext;
private ITestOutputHelper _testOutputHelper;
public Hooks(ScenarioContext scenarioContext, ITestOutputHelper testOutputHelper)
{
_scenarioContext = scenarioContext;
this._testOutputHelper = testOutputHelper;
}
public void WriteOutput(string theMessage)
{
_testOutputHelper.WriteLine(theMessage);
}
现在我的问题是如何从其他 classes 访问 WriteOutput 函数?
还是我放错了class?
由于您的挂钩 class 已经接受 ITestOutputHelper 对象,您的其他步骤定义只需要做同样的事情。从那时起,它只是很好的老式依赖注入。
如果您在每个步骤定义 class 中初始化页面模型和实用程序 classes,因为看起来 ITestOutputHelper 已经在 SpecFlow 的 dependency injection framework 中注册,您可以只传递对从构造函数到构造函数的助手。
例如,将构造函数 arg 和字段添加到步骤定义:
[Binding]
public class LoginSteps
{
private ITestOutputHelper testOutputHelper;
private LoginPage loginPage;
private SomeUtility utility;
public LoginSteps(IWebDriver driver, ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
// Pass the test output helper to a page model
loginPage = new LoginPage(driver, testOutputHelper);
// Pass the test output helper to a utility class
utility = new SomeUtility(testOutputHelper);
}
[Given(@"the user is logged in as ""(.*)"")")]
public void GivenTheUserIsLoggedInAs(string username)
{
testOutputHelper.WriteLine("...");
loginPage.LogIn(username);
}
}
然后页面模型和实用程序 class 需要构造函数参数和字段:
public class LoginPage
{
private IWebDriver driver;
private ITestOutputHelper testOutputHelper;
public LoginPage(IWebDriver driver, ITestOutputHelper testOutputHelper)
{
this.driver = driver;
this.testOutputHelper = testOutputHelper;
}
// ...
}
public class SomeUtility
{
private ITestOutputHelper testOutputHelper;
public SomeUtility(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
}
// ...
}
在另一位Dev的帮助下得到了答案,见下文
步骤 Class TestAppSteps
using Xunit.Abstractions;
[Binding]
public sealed class TestAppSteps : TestAppPage
{
public TestAppSteps(ITestOutputHelper output) : base(output)
{
}
code
}
页面ClassTestAppPage
使用 Xunit.Abstractions;
public class TestAppPage : PageAssertions
{
public TestAppPage(ITestOutputHelper output) : base(output)
{
}
code
}
实用程序Class PageAssertions
使用 Xunit.Abstractions;
public class PageAssertions : SharedClass
{
public PageAssertions(ITestOutputHelper output) : base(output) { }
code inc'
WriteToReport("Pass: URL is correct");
}
实用程序Class共享Class
使用 Xunit.Abstractions;
public abstract class SharedClass : OutputFunctions
{
public SharedClass(ITestOutputHelper output)
: base(output)
{
}
shared code including
WriteToReport(GetTheCurrentMethod());
}
抽象Class输出函数
使用 Xunit.Abstractions;
public abstract class OutputFunctions
{
protected readonly ITestOutputHelper _output;
public OutputFunctions(ITestOutputHelper output)
{
_output = output;
}
public void WriteToReport(string theMessage)
{
_output.WriteLine(theMessage);
}
}
我最近将一个项目从 NUnit 切换到 xUnit,以便可以使用 ITestOutputHelper 输出到日志。
该项目是一个相当标准的布局
功能文件->步骤类->页面类->帮助类。包括在助手 classes 中,我们也有 hooks.class。我正在使用 xUnit 跑步者。
所以在我的钩子中 class 我创造了这个
private readonly ScenarioContext _scenarioContext;
private ITestOutputHelper _testOutputHelper;
public Hooks(ScenarioContext scenarioContext, ITestOutputHelper testOutputHelper)
{
_scenarioContext = scenarioContext;
this._testOutputHelper = testOutputHelper;
}
public void WriteOutput(string theMessage)
{
_testOutputHelper.WriteLine(theMessage);
}
现在我的问题是如何从其他 classes 访问 WriteOutput 函数? 还是我放错了class?
由于您的挂钩 class 已经接受 ITestOutputHelper 对象,您的其他步骤定义只需要做同样的事情。从那时起,它只是很好的老式依赖注入。
如果您在每个步骤定义 class 中初始化页面模型和实用程序 classes,因为看起来 ITestOutputHelper 已经在 SpecFlow 的 dependency injection framework 中注册,您可以只传递对从构造函数到构造函数的助手。
例如,将构造函数 arg 和字段添加到步骤定义:
[Binding]
public class LoginSteps
{
private ITestOutputHelper testOutputHelper;
private LoginPage loginPage;
private SomeUtility utility;
public LoginSteps(IWebDriver driver, ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
// Pass the test output helper to a page model
loginPage = new LoginPage(driver, testOutputHelper);
// Pass the test output helper to a utility class
utility = new SomeUtility(testOutputHelper);
}
[Given(@"the user is logged in as ""(.*)"")")]
public void GivenTheUserIsLoggedInAs(string username)
{
testOutputHelper.WriteLine("...");
loginPage.LogIn(username);
}
}
然后页面模型和实用程序 class 需要构造函数参数和字段:
public class LoginPage
{
private IWebDriver driver;
private ITestOutputHelper testOutputHelper;
public LoginPage(IWebDriver driver, ITestOutputHelper testOutputHelper)
{
this.driver = driver;
this.testOutputHelper = testOutputHelper;
}
// ...
}
public class SomeUtility
{
private ITestOutputHelper testOutputHelper;
public SomeUtility(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
}
// ...
}
在另一位Dev的帮助下得到了答案,见下文
步骤 Class TestAppSteps
using Xunit.Abstractions;
[Binding]
public sealed class TestAppSteps : TestAppPage
{
public TestAppSteps(ITestOutputHelper output) : base(output)
{
}
code
}
页面ClassTestAppPage
使用 Xunit.Abstractions;
public class TestAppPage : PageAssertions
{
public TestAppPage(ITestOutputHelper output) : base(output)
{
}
code
}
实用程序Class PageAssertions 使用 Xunit.Abstractions;
public class PageAssertions : SharedClass
{
public PageAssertions(ITestOutputHelper output) : base(output) { }
code inc'
WriteToReport("Pass: URL is correct");
}
实用程序Class共享Class 使用 Xunit.Abstractions;
public abstract class SharedClass : OutputFunctions
{
public SharedClass(ITestOutputHelper output)
: base(output)
{
}
shared code including
WriteToReport(GetTheCurrentMethod());
}
抽象Class输出函数
使用 Xunit.Abstractions;
public abstract class OutputFunctions
{
protected readonly ITestOutputHelper _output;
public OutputFunctions(ITestOutputHelper output)
{
_output = output;
}
public void WriteToReport(string theMessage)
{
_output.WriteLine(theMessage);
}
}