简体   繁体   中英

Selenium WebDriver C# NUnit Tests Failing in Parallel

Please see the update at the bottom!

I am setting up my framework and currently have 4 Tests. Individually they all run like a charm. However when I try to run all 4 in parallel (I have set up the Parallelizable attribute up correctly and am calling tests from different classes not within the same method) I am consistently getting several errors that seem to jump around each test. These are the messages that I am getting each run: 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

Again These objects are found when the tests are run individually. I am not sure what code I need to show in order to help. Please advise.

UPDATE** @Chris my suspicions are the same. I think my tests are confusing the same driver when looking for objects. If that is the case can someone please advise how to better handle this, my browser class is what is calling the driver.

 public  static class Browser
{
    private static IWebDriver driver;
    private  static string baseURL = "someURL";

    public static ISearchContext Driver { get { return driver; } }

    internal static bool WaitUntilElementIsDisplayed(By element, int timeout)
    {
        for (int i = 0; i < timeout; i++)
        {
            if (ElementIsDisplayed(element))
            {
                return true;
            }
            Thread.Sleep(1000);
        }
        return false;
    }

    internal static IWebElement FindElement(By by)
    {

        return driver.FindElement(by);
    }

    public static bool ElementIsDisplayed(By element)
    {
        var present = false;
        driver.Manage().Timeouts().ImplicitlyWait(System.TimeSpan.FromSeconds(0));
        try
        {
            present = driver.FindElement(element).Displayed;
        }
        catch (NoSuchElementException)
        { }
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        return present;

    }



    public static void Initialize()
    {
        var options = new InternetExplorerOptions();
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        options.EnsureCleanSession = true;
        options.IgnoreZoomLevel = true;

        driver =
            new InternetExplorerDriver(

                @"C:Myfilepath",
                options, TimeSpan.FromMinutes(10));

        Goto("");
    }

    public static void CleanUp()
    {
        driver.Close();
        driver.Quit();
    }

    public static void Goto(string URL, bool userBaseURL = true)
    {
        if (userBaseURL)
            driver.Navigate().GoToUrl(string.Format("{0}/{1}", baseURL, URL));
        else
            driver.Navigate().GoToUrl(URL);
    }


}

Newest Update: per the recommendation I have removed the static references but could someone help me with the syntax on creating an instance of the driver within my current code

public class Pages
{
    private  T GetPage<T>() where T : new()
    {
        var page = new T();
        PageFactory.InitElements(Browser.Driver, page);
        return page;
    }
    public  LoginPage Login
    {
        get { return GetPage<LoginPage>(); }
    }

    public  RegisterPage Register
    { get { return GetPage<RegisterPage>(); } }

    public  SearchPage Search
    { get { return GetPage<SearchPage>(); } }
}

I am not sure how to create an instance of Browser.Driver Please help!

Remove all references to "static" in your class and create an instance of the class in each test to fix your issue.

...Now change your Page class to accept the driver in the constructor

public class Pages
{
    private readonly ISearchContext _context;

    public Pages(ISearchContext context)
    {
        _context = context; 
    }

    private T GetPage<T>() where T : new()
    {
        var page = new T();
        PageFactory.InitElements(_context, page);
        return page;
    }
    public LoginPage Login
    {
        get { return GetPage<LoginPage>(); }
    }

    public RegisterPage Register
    { get { return GetPage<RegisterPage>(); } }

    public SearchPage Search
    { get { return GetPage<SearchPage>(); } }
}

... then in your test method

var browser = new Browser();
var page = new Page(browser.Driver);

Sorry. Been away and noticed your updates.

I have a separate class helper that I use to return my web driver. I'm using chrome driver and (headless) unit driver, which on my machines polices requires several params to get it running, so a class in its own right makes senses to me. Eg WebDriverHelper.java. This has several static methods that returns a new instance of the driver of interest.

Eg

WebDriver myDriver = WebDriverHelper.ChromeDriver();

My ChromeDriver method returns a new driver.

Eg

return new ChromeDriver;

If you need more detail, let me know and I'll copy some of my classes when I get in work tomorrow.

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