简体   繁体   中英

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

Below is the Test class code:

 [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.

But, if I run Test2() individually, its a Pass!

Tests pass if i use just [onetimesetup] and [onetimeteardown] instead of [setup] and [teardown]. In Debug Mode, it shows that exception occurs when Hovers method is under execution.

Debug Mode - Exception

Exception details - 1

Exception details - 2

Am I missing something? Kindly help me out.

Thanks in advance.

You don't show the definition of d , the driver instance. 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 .

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); act.MoveToElement(IW).Build().Perform(); }

and deleted driver instance from class m IWebDriver d=d.Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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