Selenium doesn't close any browsers until ALL tests are complete - c#

I'm doing something like this (below) with multiple Test Classes but [ClassCleanup] is not called until ALL tests run.
Because of this each class starts up a browser and ALL of the browsers remain open until ALL of the tests are completed, and then they ALL close down.
Is this expected?
Could someone comment if their tests do the same thing?
Is there a way to force a browser to close when the test class is done?
[ClassCleanup]
public static void ClassCleanup()
{
driver.Quit();
I dont know if this is just how Selenium is supposed to work, or if I have some sort of hanging resource that doesn't allow the browser to close until the end?
I cannot use [TestCleanup] because then I cannot have multiple tests in the same test class (as it will close before the 2nd test will run)

I finally found an example that works :)
Simply replaced
[ClassCleanup]
with
[ClassCleanup(ClassCleanupBehavior.EndOfClass)]

you can use [TestInitialize] as setUp and [TestCleanup] as Teardown of each test case.
In your code, it runs after class(all test cases)
So full code:
Setup:
[TestInitialize()]
public void InitializeTest() {
//your driver initilazation code
}
and, Teardown:
[TestCleanup]
public void Cleanup() {
driver.Quit();
}

Related

AssemblyInitialize and AssemblyCleanup not triggering

In MSTEST, I want to send out a mail once all the test run has been completed.
Currently I have included my code to send mail in the [TestCleanup] method in MSTEST. The problem is, the mail is sending for each test cases. If I run more than one test case from TestExplorer in Visual Studio, the mail is going for every test cases.
I want to send out a single mail once all the test cases are executed. Also I tried to include my mailing part in the [AssemblyCleanup] method in MSTest. But both the [AssemblyInitialize] and [AssemblyCleanup] are not triggering while executing the test cases.
Is there any way to know, how many test cases are queued for running while executing in MSTest programmatically?
Or
Is there any way to trigger the [AssemblyInitialize] and [AssemblyCleanup] ?
Sample Code:
[TestClass]
public abstract class MasterClass
{
[AssemblyInitialize] -- Not triggering
public virtual void AssemblyInit(TestContext context)
{
Reports.PrintLog("------------------ Test Started ------------------");
}
[AssemblyCleanup] -- Not triggering
public virtual void AssemblyCleanup()
{
SendEmail(MailTo, attachment);
}
}
Thanks in advance.
This method should be static as they are on the whole assembly and not on a specific instance.
Check Microsoft documentation here.
MSTest cannot instantiate your abstract class. So it cannot test it.
And the method should be static

Is it Ok to ignore exceptions in TearDown or cleanup code

I have a test suite running in Automation. All of these tests are functional UI tests. It looks like below
[SetUp]
public void Setup()
{
CreatePolicy();
}
[Test]
public void Test1()
{
EditPolicyAndValidateResults();
}
[Test]
public void Test2()
{
EditPolicyAndValidateResults();
}
[TearDown]
public void TearDown()
{
DeletePolicy();
}
Now, the problem is DeletePolicy() is failing sometimes (randomly for one or two of the tests) and because of which the corresponding tests are failing.
To circumvent the failure if have added a try catch block to DeletePolicy() and it looks like the following:
[TearDown]
public void TearDown()
{
try
{
DeletePolicy();
}
catch(Exception ex)
{
// Do nothing
}
}
Since testing DeletePolicy() function is not the intent of my test cases, the approach looks fine to me. Is this correct or I am missing something here?
The general pattern of Unit Testing is: Arrange, Act then Assert: AAA.
Setup is just a designated method to Arrange the test in a better way (for example by re-setting any previously mocked/stubbed objects to save time and to avoid code duplication). It is by no means mandatory and is just a way to help test coder to code better.
TearDown again and in the same way is a helper and it is even farther to the generic concept of AAA than Setup. It is because in AAA nothing regarding destructing or cleaning up is mentioned.
So, feel free to ignore any failure in TearDown unless it is somehow important. Maybe there is something hidden from your eyes and you haven't taken into account yet. It may be important to make another unit test on failure point, but it totally depends on your case.

CodedUI : The Browser gets closed as finish testing without keeping for testing further steps

I'm working on a CodedUI project to check on a Web Solution.
There, when Im testing for launching the site (as in launching the web URL), I am facing an issue with closing the browser as it finish testing, where it wont allow to use the same browser instance for further testing, Unless if I keep open a instance of a browser (IE) prior manually before running the test.
Can anyone please assist me with this, where I was unable to find a suitable solution. I even went through articles in MSDN where I posted regarding this issue as a comment which I'm facing, since I tried various ways of Code Snippets so far.
(MSDN : http://blogs.msdn.com/b/visualstudioalm/archive/2012/11/08/using-same-applicationundertest-browserwindow-across-multiple-tests.aspx)
My written code is provided below.
---
-- common.cs
[TestMethod]
public void LoadLocalHost()
{
this.UIMap.LoadLocalHost();
}
---
-- UIMap.Designer.cs
public void LoadLocalHost()
{
this.UIMsnInternetExplorerWindow.LaunchUrl(new System.Uri("http://localhost:5500/"));
Console.WriteLine(UIMsnInternetExplorerWindow.CloseOnPlaybackCleanup);
UIMsnInternetExplorerWindow.CloseOnPlaybackCleanup = false;
}
Highly appreciate an assistance from some one who can.
If you launch the browser in a class Initialize method rather than a test initialize method, and don't close it in your test or in a test cleanup, it should remain open for the duration of your tests, provided they are all executed within the same test class.
[ClassInitialize]
public void classInitialize()
{
// Do some stuff, including launch the browser
// This is executed once per class.
}
[TestInitialize]
public void testInitialize()
{
// This is where you'd get the test back to a specific
// state, like bring your browser back to a home page or something.
// It would be executed at the beginning of each test.
}
....
[TestMethod]
public void myFirstTest()
{
// Do some more stuff specific to your test.
}
[TestMethod]
public void mySecondTest()
{
// Do things after the last test.
}
....
[ClassCleanup]
public void classCleanup()
{
// Finalize everything and close the browser.
}

Is it possible to execute a method before and after all tests in the assembly?

I would like to built an nunit project for selenium ui automation. I would like to sign in to the site before running all tests (all of them) and to close the browser after running all tests (all of them).
I can't use the SetUp since it related to fixtures and I want to do it before and after everything.
Do you know who to execute it?
I'm familiar with the SetUp and TearDown attribute.
Let me explain it again.
I need some logic to be executed before all tests from all fixtures starts (AKA - First test in the entire assembly) and also some logic to be executed after all tests from all fixtures ended (AKA - Last test in the entire assembly).
If all your test fixtures are within the same namespace then you can use the [SetUpFixture] attribute to mark a class as the global setup and teardown. You can then put all your login/logout functionality in there.
NUNIT 2.x
namespace MyNamespace.Tests
{
using System;
using NUnit.Framework;
[SetUpFixture]
public class TestsSetupClass
{
[SetUp]
public void GlobalSetup()
{
// Do login here.
}
[TearDown]
public void GlobalTeardown()
{
// Do logout here
}
}
}
See:
http://www.nunit.org/index.php?p=setupFixture&r=2.4
NUNIT 3.x
namespace MyNamespace.Tests
{
using System;
using NUnit.Framework;
[SetUpFixture]
public class TestsSetupClass
{
[OneTimeSetUp]
public void GlobalSetup()
{
// Do login here.
}
[OneTimeTearDown]
public void GlobalTeardown()
{
// Do logout here
}
}
}
See:
https://github.com/nunit/docs/wiki/SetUpFixture-Attribute
Sure. That's what the [TestSetUp] and [TearDown] attributes are for. Don't confuse them with [TestFixtureSetUp] and [TestFixtureTearDown], which are executed before the first test and after the last.
Before executing each test cases [SetUp] section will executed
after completed the execution of each test cases [TearDown] section will executed.
if we want to initialize variables we often write in [SetUp] section like a constructor
if we want to dispose any object we often write in [TearDown] section
[SetUp]
protected void SetUp()
{
//initialize objects
}
[TearDown]
public void TearDown()
{
//dispose objects
}
The closest thing in nunit is the SetupFixture attribute, which allows you to tag a class to do setup/teardown for all test fixtures in a namespace;
The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

The TestContext.TestName property NEVER changes

GOAL:
I want to use the TestContext.TestName property to extract the name of the test being ran so that my [TestCleanup] function can log the outcome to our bespoke results repository, automatically when every test completes.
PROBLEM:
Even in my basic 'sanity check' test project that contains 5 tests that are similar to the structure below:
[TestMethod]
public void TestMethodX()
{
Console.WriteLine(String.Format("In Test '{0}'",_ctx.TestName));
Assert.IsTrue(true);
}
With a Class 'initializer' like below which sets _ctx for me:
[ClassInitialize]
public static void ClassInit(TestContext Context)
{
_ctx = Context;
Console.WriteLine("In ClassInit()");
}
[[NOTE: the Console.WriteLines are purely there for me to hover the mouse over and inspect value/properties, etc.]]
The _ctx.TestName NEVER changes from the name of the first test in the run of tests, i.e. If I was to run all five tests ('TestMethod1', 'TestMethod2', 'TestMethod3', etc.) they all log 'TestMethod1' as their testname in my results repository.
Running the tests individually it works fine, but that is of no use to me as I need to be able to run 10's/100's/1000's of tests against my application and have the testContext handle the testname or me.
I have tried this several times now and searched the internet loads and haven't anyone else with this problem, so i'm either: unique with this problem, have poor 'Google-Fu' skills, or am doing something REAL stupid. Hopefully this makes sense and someone has the answer.
Thanks in advance,
Andy
This is happening because the [ClassInitialize] is executed only once for the whole test run and you initialize the _ctx in there. Use the [TestInitialize] instead, which is executed before each test method and override the TestContext Class :
[TestClass]
public class TestClass
{
public TestContext TestContext { get; set; }
[TestInitialize]
public void Initialize()
{
// Runs once before each test method and logs the method's name
Console.WriteLine(TestContext.TestName);
}
[TestMethod]
public void TestMethod1()
{
// Logs the method name inside the method
Console.WriteLine(String.Format("In Test '{0}'", TestContext.TestName));
}
// ... Your rest test methods here
}
MSTest.exe outputs can be configured to output a .trx (xml)file with the complete results of you test, names , passed or failed and output from any of those test, there is also a tool to convert the TRX file to HTML http://trxtohtml.codeplex.com/
Hope this helps

Categories