We are doing a Proof of Concept with Selenium and MS Test at our company. We are supporting only three browsers for the POC; Chrome, Firefox, and Edge.
I have created an array of IWebDriver and added the browsers to it in this way:
private IWebDriver[] drivers = { new ChromeDriver(), new FirefoxDriver(), new EdgeDriver() };
[TestMethod]
[TestCategory("SmokeTest")]
public void LoadHomePageWhenInitiated()
{
foreach (var driver in drivers)
{
using (driver)
{
// arrange (do prep work here, if needed)
var searchPage = new SearchPage(driver);
// act (perform the actual test)
driver.Manage().Window.Maximize();
searchPage.NavigateTo();
// assert (determine pass/fail outcome)
// doesn't seem to be closing the browser like it should
driver.Close();
}
}
}
What I'd hoped for is that each browser would initialize in succession (at the foreach or the using), each run the tests, and the close. At that point the next browser would initialize and run the same tests.
What's happening is that all three browsers are opening at the time the array is created.
I'm thinking there must be a way to do this with [ClassInitialize()] or [TestInitialize()] but at the moment cannot determine how to accomplish what I'm looking for.
I appreciate any insights on a better way to accomplish this with Selenium and MS Test.
CodePudding user response:
This seems to have solved the problem. The browsers now open one at a time.
private Func<IWebDriver>[] driverBuilders = { () => new ChromeDriver(), () => new FirefoxDriver(), () => new EdgeDriver() };
[TestMethod]
[TestCategory("SmokeTest")]
public void LoadHomePageWhenInitiated()
{
foreach (var driverBuilder in driverBuilders)
{
using (var driver = driverBuilder.Invoke())
{
// arrange (do prep work here, if needed)
var searchPage = new SearchPage(driver);
// act (perform the actual test)
driver.Manage().Window.Maximize();
searchPage.NavigateTo();
// assert (determine pass/fail outcome)
// We don't need an assert here because the NavigateTo verifies we are on the
// correct page.
// doesn't seem to be closing the browser like it should
driver.Close();
}
}
}
CodePudding user response:
I will advice against doing this. If you want to run all your tests in succession, schedule your tests and run them that way. One good way of managing this is by using a CI/CD platform like Jenkins.
In Jenkins, you can easily create all kinds of jobs, scheduled them at whatever intervals/frequency you need, deliver reports to whoever, all without the need to do what you are trying to do.
Here is a 50-minute video you can watch to setup Jenkins configure it to run your tests and integrate with GitHub (if you need to do that as well).
If the video doesn't help, reach out to me and I should be able to get you going. You don't have to become an expert on Jenkins to use it effectively to create all kinds of scheduled tasks that can be executed automatically or manually.