When I write a test in Visual Studio, I check that it works by saving, building and then running the test it in Nunit (right click on the test then run).
The test works yay... so I Move on...
Now I have written another test and it works as I have saved and tested it like above. But, they dont work when they are run together.
Here are my two tests that work when run as individuals but fail when run together:
using System; using NUnit.Framework; using OpenQA.Selenium.Support.UI; using OpenQA.Selenium; namespace Fixtures.Users.Page1 { [TestFixture] public class AdminNavigateToPage1 : SeleniumTestBase { [Test] public void AdminNavigateToPage1() { NavigateTo<LogonPage>().LogonAsCustomerAdministrator(); NavigateTo<Page1>(); var headerelement = Driver.FindElement(By.ClassName("header")); Assert.That(headerelement.Text, Is.EqualTo("Page Title")); Assert.That(Driver.Url, Is.EqualTo("http://localhost/Page Title")); } [Test] public void AdminNavigateToPage1ViaMenu() { NavigateTo<LogonPage>().LogonAsCustomerAdministrator(); Driver.FindElement(By.Id("menuitem1")).Click(); Driver.FindElement(By.Id("submenuitem4")).Click(); var headerelement = Driver.FindElement(By.ClassName("header")); Assert.That(headerelement.Text, Is.EqualTo("Page Title")); Assert.That(Driver.Url, Is.EqualTo("http://localhost/Page Title")); } } }
When the second test fails because they have been run together
Nunit presents this:
Sse.Bec.Web.Tests.Fixtures.ManageSitesAndUsers.ChangeOfPremises.AdminNavigateToChangeOfPremises.AdminNavigateToPageChangeOfPremisesViaMenu: OpenQA.Selenium.NoSuchElementException : The element could not be found
And this line is highlighted:
var headerelement = Driver.FindElement(By.ClassName("header"));
Does anyone know why my code fails when run together, but passes when run alone?
Any answer would be greatly appreciated!
Order of Execution Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.
Tests should never depend on each other. If your tests have to be run in a specific order, then you need to change your tests. Instead, you should make proper use of the Setup and TearDown features of your unit-testing framework to ensure each test is ready to run individually.
Such a situation normally occurs when the unit tests are using shared resources/data in some way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With