【发布时间】:2021-11-21 07:56:15
【问题描述】:
我对 C# 很陌生,并且一直是 Java 人。在 Java selenium 中,这可以通过使用 @BeforeSuite 注释来实现。最初,我认为可以通过使用 [OneTimeSetUp] 注释来实现,但事实并非如此。当我将Parent类继承给子类时,Parent类中提到的[OneTimeSetUp]被多次调用。
我的要求是,一旦开始执行,我想创建一个具有唯一 ID 的文件夹并将报告放在该文件夹中。我在方法中拥有相同的所有代码,但它被多次调用,导致脚本失败。
我尝试了很多搜索,我看到人们推荐使用 [SetUpFixture] 来做同样的事情。我也尝试过使用它,但它也不适合我。我错过了什么吗?这是我的代码
我的家长班:-
namespace Core.Framework.Controls
{
[SetUpFixture]
public class Controls
{
private string Browsername { get; set; }
[OneTimeSetUp]
public void BeforeSuite()
{
//Create Unique Folder
}
Child1 班级:-
namespace Core
{
[TestFixture]
[Parallelizable]
public class UnitTest1 : Controls
{
[Test]
[TestCaseSource(typeof(Controls), "BrowsersToRunWith")]
public void Bing(String browser)
{
SetUp(browser);
Class1 class1 = new Class1(_controls);
class1.LaunchURL("http://bing.com");
class1.EnterTextInBing("Parallel Execution using C#");
Thread.Sleep(3000);
}
}
Child2 类:-
namespace Core
{
[TestFixture]
[Parallelizable]
public class UnitTest2 : Controls
{
[Test]
[TestCaseSource(typeof(Controls), "BrowsersToRunWith")]
public void Google(String browser)
{
SetUp(browser);
Class1 class1 = new Class1(_controls);
class1.LaunchURL("http://google.com");
}
}
}
根据@Charlie 的建议更新了项目结构
Base Class
• Setup Fixture
• No TestFixture
• One Time Setup
• One Time Tear Down
Parent Class : Base Class
• No TestFixture
• Set Up
o Driver initialization
• Tear Down
o Driver Quit
Child Class1:Parent Class
• Test Fixture
• Test Method
o Getting the driver instance from Parent class and testing the application
Child Class2:Parent Class
• Test Fixture
• Test Method
o Getting the driver instance from Parent class and testing the application –
基类图像
【问题讨论】:
-
感谢您抽出宝贵时间寻找答案,但我确实使用@Charlie 提供的方法尝试了该解决方案,但这也不起作用。这是我所做的基类-我将带有 OneTimeSetUp 和 OneTimeTear 的 SetUpFixture 保留在此处控制器继承基类-我保留了我的 SetUp 并在此处拆除测试类-继承控制器-此处只有 TestFixture,没有 SetUp 或 OneTimeSetup 仍然,它不是为我工作。