0

I am trying to run tests that will create, edit, and delete entries from a database using a web application. The problem I am having is that the tests are dependent of one another. E.g. I have to first create the entry to edit or delete it. What is the appropriate method for doing this? Is there a way to execute the tests in a specific order? Here is an example of my code as it is now:

[TestFixture]
public class DevicesTest : ASysTest {
    private const string DeviceName = "Automated Test Device";
    private const string EditedDeviceName = "Modified Automated Test Device";

    public override void PostInitialize() {
        this.Login("devices");
    }

    [SetUp]
    public void TestInitialize() {
        this.Driver.Navigate().GoToUrl(GetAppUrl("/devices/"));
    }

    [Test]
    public void CreateNewDeviceTest() {
        var modal = DisplayModal("create");

        var selector = new SelectElement(modal.FindElement(By.Id("CompanyID")));
        selector.SelectByIndex(2);
        modal.FindElement(By.CssSelector("option[value='1']"));
        modal.FindElement(By.Id("Name")).SendKeys(DeviceName);
        modal.FindElement(By.Id("IP_Address")).SendKeys("111.1.1.1");
        modal.FindElement(By.Id("Operating_System")).SendKeys(DeviceName);
        modal.FindElement(By.Id("Administrator_Password")).SendKeys(DeviceName);
        modal.FindElement(By.Id("Notes")).SendKeys(DeviceName);

        modal.FindElement(By.CssSelector(".btn-primary[type='submit']")).Click();

        var heading = Driver.FindElementByTagName("h1");

        Assert.IsTrue(heading.Displayed);

        StringAssert.Contains("Devices", heading.Text);

        var page = Driver.FindElementByClassName("device");

        StringAssert.Contains(DeviceName, page.Text);
    }

    [Test]
    public void EditDeviceTest() {
        var tableData = Driver.FindElement(By.XPath("//td[contains(., '" + DeviceName + "')]")); // Select <td> containing DeviceName
        var tableRow = tableData.FindElement(By.XPath("..")); // Grab parent element <tr>
        tableRow.FindElement(By.CssSelector(".modal-open[data-modal='edit-modal']")).Click(); // Click edit button contained within <tr>

        Thread.Sleep(500);

        var modal = Driver.FindElementById("edit-modal");


        modal.FindElement(By.Id("Name")).Clear();
        modal.FindElement(By.Id("Operating_System")).Clear();
        modal.FindElement(By.Id("Administrator_Password")).Clear();
        modal.FindElement(By.Id("Notes")).Clear();
        modal.FindElement(By.Id("Name")).SendKeys(EditedDeviceName);
        modal.FindElement(By.Id("Operating_System")).SendKeys(EditedDeviceName);
        modal.FindElement(By.Id("Administrator_Password")).SendKeys(EditedDeviceName);
        modal.FindElement(By.Id("Notes")).SendKeys(EditedDeviceName);
        modal.FindElement(By.CssSelector(".btn-primary[type='submit']")).Click();

        var heading = Driver.FindElementByTagName("h1");

        Assert.IsTrue(heading.Displayed);

        StringAssert.Contains("Devices", heading.Text);

        var page = Driver.FindElementByClassName("device");

        StringAssert.Contains(EditedDeviceName, page.Text);
    }

    [Test]
    public void DeleteDeviceTest() {
        var tableData = Driver.FindElement(By.XPath("//td[contains(., '" + EditedDeviceName + "')]")); // Select <td> containing DeviceName
        var tableRow = tableData.FindElement(By.XPath("..")); // Grab parent element <tr>
        tableRow.FindElement(By.CssSelector(".modal-open[data-modal='delete-modal']")).Click(); // Click delete button contained within <tr>

        Thread.Sleep(500);

        var modal = Driver.FindElementById("delete-modal");

        modal.FindElement(By.CssSelector(".btn-danger[type='submit']")).Click();

        var heading = Driver.FindElementByTagName("h1");

        Assert.IsTrue(heading.Displayed);

        StringAssert.Contains("Devices", heading.Text);

        var page = Driver.FindElementByClassName("device");

        StringAssert.DoesNotContain(EditedDeviceName, page.Text);
    }
}
mdk09
  • 287
  • 1
  • 4
  • 17
  • 2
    Your tests should be independant on one another. You would normally stuff setup things in the Setup/TestFixtureSetup methods for this purpose. This is because you will not be able to guarantee the order that either a) someone else runs it in or more importantly, b) the order in what your CI server will be running it. – Arran Sep 16 '14 at 20:10
  • Test needs to be loosely coupled. As @Arran suggested, set it up at the test seetings level or create private methods and call them in order in your tests. – Sham Sep 17 '14 at 04:25

3 Answers3

1

As a solution you can use alphabetical order:

[Test]
    public void A_Create()
    ...


[Test]
    public void B_Edit()
    ...


[Test]
    public void C_Delete()
    ...
German Petrov
  • 1,475
  • 1
  • 17
  • 21
0

About

problem I am having is that the tests are dependent of one another

I think that Modularity-driven_testing approach is what you're looking for. Generally it requires the small, independent test scripts. After you create them - you can use them in a hierarchical fashion to construct your bigger tests, creating a particular test case you need/order also.

When I had to use Selenium for E2E testing in MVC project, I implemented my own DB_Accessor class that was responsible for the CRUD. Use case: verify that data changes from front-end are represented in DB accordingly.

ekostadinov
  • 6,880
  • 3
  • 29
  • 47
-1

In NUnit 2.5, individual test cases are sorted alphabetically and executed in that order. With NUnit 2.5.1, the individual cases are not sorted, but are executed in the order in which NUnit discovers them

Refer: Nunit test Order

Community
  • 1
  • 1
Rishi Khanna
  • 409
  • 1
  • 5
  • 16