3

I am beginner with c# and Selenium. I am wondering if it is possible to run multiple [TestMethod]-s in same browser instance without closing it?

For e.g. after "Can_Change_Name_And_Title" is finished, I want to continue with "Can_Change_Profile_Picture".

    [TestMethod]
    public void Can_Change_Name_And_Title()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewName("John Doe").SetNewTitle("New Title Test").ChangeNameTitle();
    }

    [TestMethod]
    public void Can_Change_Profile_Picture()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewProfilePicture(Driver.BaseFilePath + "Profile.png").ChangeProfilePicture();
    }
1
  • Hmm.. I need to put "new FirefoxDriver();" in [ClassInitialize] somehow, if I understood well. Right now it is in my [TestInitialze] part... But when I do it, my tests will threw exception. Commented Aug 12, 2015 at 9:18

4 Answers 4

1

Looks like you need tests chain, but having tests dependent on other tests points to a design flaw. Nevertheless there are ways to achieve that. You can create ordered unit tests, which is basically a single test container that ensures the test sequence.

Here's a guide on MSDN, or you can Use Playlist

Right click on the test method -> Add to playlist -> New playlist

the execution order will be as you add them to the playlist but if you want to change it you have the file

Playlist

In case you need to keep test data/objects during the execution, you could utilize some global variable. I'm using such TestDataStore:

private Dictionary<string, yourObjData> _dataStore = new Dictionary<string, yourObjData>();

So you could add and retrieve what you need any time (including your session details, web driver etc.).

Sign up to request clarification or add additional context in comments.

2 Comments

No, this is not what I am looking for. I am familiar with playlist. I need to keep everything in one browser. From beginning of [TestClass] until the end.
If you need a place for the test data - see my updated answer. Also call the driver.Dispose() in your last [TestMethod].
1

Ok, so here is solution that I have found. Instead of:

 using Microsoft.VisualStudio.TestTools.UnitTesting;

I used:

 using NUnit.Framework;

So now I have next hierarchy:

[TestFixture]
 [TestFixtureSetup] // this is where I initialize my WebDriver " new FirefoxDriver(); "
  [Test] //this is my first test
    public void Can_Change_Name_And_Title()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewName("John Doe").SetNewTitle("New Title Test").ChangeNameTitle();
    }
  [Test] //this is my second test
    public void Can_Change_Profile_Picture()
    {
        SidebarNavigation.MyProfile.GoTo();
        ProfilePages.SetNewProfilePicture(Driver.BaseFilePath + "Profile.png").ChangeProfilePicture();
    }

[TestFixtureTearDown] // this is where I close my driver

With this changes, my browser will open only once for TestFixture (or TestClass if you use "using Microsoft.VisualStudio.TestTools.UnitTesting;") and all [Test]-s from that fixture will run in that same browser instance. After all tests are done, browser will close.

Hope this will help someone else in future. Ask me if you need additional help.

Comments

0

You can set the selenium property restart.browser.each.scenario to false, maybe this does already the trick for you. In Java you can set this property with

System.setProperty("restart.browser.each.scenario", "false");

I think in C# it will be something like

System.Environment.SetEnvironmentVariable("restart.browser.each.scenario","false");

Comments

0

HI If you are using using NUnit.Framework;

The code Execution plan is like below. For First Test Case

[TestFixtureSetup]   ---->For each test case this will work so here we can          
                          initialize  the driver instance.              
[TestMethod]         ----->test method will goes here
[TearDown]           -----> clean up code
                         **For Second Test Case**
[TestFixtureSetup]               
[TestMethod]
[TearDown]

If you have to run both test case in one browser instance Dont close the driver inside TearDown. AND INITIALIZE THE DRIVER UNDER TextFixtureSetup

    [TestFixture()]
        public class TestClass
        {

            [TestFixtureSetUp]
            public void Init()
            {
                Driver.initialize(new InternetExplorerDriver());
            }
            [TearDown]
           public void Close()
            {
//dont do any driver.close()
            }
         [TestMethod]
        public void TestCase001()
        {
        //your code goes here
        }
 [TestMethod]
        public void TestCase002()
        {
        //your code goes here
        }

1 Comment

You mentioned in your solution to not close the browser inside TearDown then shall I close it after my second test case? and it is guaranteed that the test-cases will run in same sequence as written in the file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.