简体   繁体   English

将Nunit的[setup]和[teardown]属性与Selenium一起使用时,无法同时运行[Test]

[英]Unable to Run [Test]'s together when using [setup] and [teardown] attributes of Nunit with Selenium

Below is the Test class code: 下面是Test类代码:

 [TestFixture]
public class Playground
{
public static IWebDriver d { set; get; }

[SetUp]
    public void Initialize_Browser()
    {
        d = new ChromeDriver();
        d.Manage().Window.Maximize();
        d.Url = "https://the-internet.herokuapp.com/";
    }

    [TearDown]
    public void Quit()
    {
        d.Quit();
    }



[Test]
    public void Test1()
    {
        POM p = new POM();
        m.Iwait(15);
        m.Print("You are now at " + p.main_header_txt + " page");
    }
[Test]
    public void Test2()
    {
        POM p = new POM();
        p.Hover_Images();
    }

Below is one more class which has custom static methods that contain frequently used code: (For Hovering over an element) 下面是一个具有自定义静态方法的类,该类包含经常使用的代码:(用于将鼠标悬停在元素上)

public class m
{
 static IWebDriver d= Playground.d;
 public static void Hover(IWebElement IW)
    {
        Actions act = new Actions(d);
        act.MoveToElement(IW).Build().Perform();
    }
}

Below is another class whose method is called in test class. 下面是另一个类,其方法在测试类中被调用。

public class POM
{    
static IWebDriver d= Playground.d;
IWebElement hovers_link => d.FindElement(By.LinkText("Hovers"));
public void Hover_Images()
    {
        hovers_link.Click();
        IList<IWebElement> user_image = d.FindElements(By.XPath("//div[@class='figure']"));
        foreach (IWebElement ui in user_image)
        {
            m.Hover(ui);
            Thread.Sleep(1000);
        }
    }

} }

After building the solution and running it, Test 1() passes and Test 2() fails. 构建解决方案并运行它之后,测试1()通过,而测试2()失败。

But, if I run Test2() individually, its a Pass! 但是,如果我单独运行Test2(),则可以通过!

Tests pass if i use just [onetimesetup] and [onetimeteardown] instead of [setup] and [teardown]. 如果我仅使用[onetimesetup]和[onetimeteardown]而不是[setup]和[teardown],则测试通过。 In Debug Mode, it shows that exception occurs when Hovers method is under execution. 在调试模式下,它表明在执行Hovers方法时发生异常。

Debug Mode - Exception 调试模式-异常

Exception details - 1 例外详情-1

Exception details - 2 例外详情-2

Am I missing something? 我想念什么吗? Kindly help me out. 请帮我。

Thanks in advance. 提前致谢。

You don't show the definition of d , the driver instance. 您没有显示驱动程序实例d的定义。 From the usage, I assume it is defined as a member variable of the test class. 根据用法,我假设它被定义为测试类的成员变量。 That means both tests use the same instance of d . 这意味着两个测试都使用d的相同实例。

My guess is that you have enabled parallel running of your tests, and that they are stepping on one another's common driver. 我的猜测是,您已启用测试的并行运行,并且它们正在逐步使用彼此的通用驱动程序。 When you use one time setup instead of setup, only one driver is created. 当您使用一次设置而不是设置时,只会创建一个驱动程序。 This is OK if it is what you intend for the tests. 如果这正是您要进行的测试,则可以。 Depending on what operations you perform with the driver, parallel tests may not impact one another. 根据您使用驱动程序执行的操作,并行测试可能不会相互影响。

Another workaround, of course, would be to flag the tests as non-parallelizable. 当然,另一个解决方法是将测试标记为不可并行化。

I have found out the solution, 我找到了解决方案,

I had modified the arguments of Hover() method to public static void Hover(IWebDriver d,IWebElement IW) { Actions act = new Actions(d); 我已经将Hover()方法的参数修改为公共静态void Hover(IWebDriver d,IWebElement IW){Actions act = new Actions(d); act.MoveToElement(IW).Build().Perform(); act.MoveToElement(IW).Build()执行(); } }

and deleted driver instance from class m IWebDriver d=d.Playground 并从类m IWebDriver d = d中删除了驱动程序实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM