问题
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:
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
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.).
回答2:
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.
回答3:
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");
回答4:
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
}
来源:https://stackoverflow.com/questions/31940746/how-to-run-multiple-test-methods-in-same-browser-instance-without-closing-it-c