Amol
Amol

Reputation: 351

Need chrome drive instance in all test cases selenium c#

I am launching the amazon url using chrome drive instance and I want to share the same session in which I have launched the amazon in all test methods. Here is the code for the same. Can somebody tell me how to share the session from one method to another?

public class UnitTest1
{
    IWebDriver chromeDriver = null;
    [Fact]
    public void Launch_Amazon_WithSearching()
    {
        amazonUrl = "https://www.amazon.in/";
        chromeDriver = new ChromeDriver(@"C:\Projects\Install\ChromDriver");
        chromeDriver.Navigate().GoToUrl(amazonUrl);
        chromeDriver.Manage().Window.Maximize();

        //here we are looking for search textbox and then entering a new value 
        IWebElement searchElement = chromeDriver.FindElement(By.Id("twotabsearchtextbox"));
        searchElement.SendKeys("bluetooth earphones");
        searchElement.SendKeys(Keys.Enter);
    }

    [Fact]
    public void Amazon_OpenSearchedItem_And_AddToCart()
    {
        //here we are finding the elements from the searched results.
        IWebElement searchedElement = chromeDriver.FindElement(By.XPath("//*[@id='search']//span[contains(text(),'Raging Red')]"));
        searchedElement.Click();

        //Here we are getting the new window name and then setting the chrome driver window to that new window
        string newWindowName = chromeDriver.WindowHandles.Where(x => x != chromeDriver.CurrentWindowHandle).FirstOrDefault();
        chromeDriver.SwitchTo().Window(newWindowName);

        //this is for clicking add to cart button
        IWebElement addToCartElement = chromeDriver.FindElement(By.Id("add-to-cart-button"));
        addToCartElement.Click();
        Assert.NotNull(addToCartElement);
    }
}

Upvotes: 0

Views: 333

Answers (1)

mholle
mholle

Reputation: 597

The usage of FactAttribute reveals that you're using xUnit.

xUnit documentation:

xUnit.net creates a new instance of the test class for every test that is run, so any code which is placed into the constructor of the test class will be run for every single test.

That means fields won't help you to share the Chromedriver instance between test methods.

What you need is a fixture class - xUnit will make sure to create an instance of this class shared across tests methods. If your fixture class implements IDisposable, xUnit will call the dispose method after the methods of the class have run.

public class ChromeDriverFixture
{
    public ChromeDriverFixture()
    {
        Driver = new ChromeDriver(@"C:\Path\To\ChromeDriver");
        Driver.Manage().Window.Maximize();
    }

    public IWebDriver Driver { get; }
}

public class UnitTest1 : IClassFixture<ChromeDriverFixture>
{
    private ChromeDriverFixture _fixture;

    public UnitTest1(ChromeDriverFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public void Launch_Amazon_WithSearching()
    {
        const string amazonUrl = "https://www.amazon.in/";
        _fixture.Driver.Navigate().GoToUrl(amazonUrl);

        // ...
    }
}

Upvotes: 1

Related Questions