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);
}
}