I do not have a separate TestBase/Service class for each test. I have just Page Object classes which contain the page factory and unit test
classes which contain the test methods. Is this approach correct?
Without seeing code, hard to picture exactly what you mean here, but, I feel you should separate your page factory classes from your unit test classes. I put them in separate projects and have the test project reference the page object model project. But, if one project, then I would do something like this: create subfolders in your VS project, one for Pages and one for Tests. The relationship is the Tests reference the pages, but pages do NOT reference tests.
My basic test cases run absolutely fine. The problem comes when a test case depends on the other test case. Say, there's a functionality
which gives me a certain output and that output is the requirement for
my other separate test case. How do I proceed with this scenario?
There's some red flags here: A purist's view, test cases should never depend on each other. A goal should be each test can stand/run on it's own.
I'm thinking you mean to create reusability. Test Methods are not intended to be "reusable" methods, so likely some simple refactoring and willingness to create stand alone tests will help in this case.
Imagine you have a test case that looks something like this (NOTE the void return type on our test):
[TestMethod]
public void SaveCustomer()
{
POM.HomePage.Login(user, password);
POM.HomePage.GoToNewCustomer();
POM.NewOrderPage.EnterCustomerName(custName);
POM.NewOrderPage.EnterCustomerAddr(custAddr);
POM.NewOrderPage.SaveCustomer();
var custId = POM.SaveConfirmationPage.GetCustomerId(); // customer id generated by system
Assert.IsTrue(custId.Length == 5);
}
Then we want to create a second test that searches for customer by Id. It is this second test case that may cause us to think, "hmm, I want to get the Customer Id from the first test, and use that in my second test."
[TestMethod]
public void SearchCustomerById()
{
// Get the customer ID created by first test: How do I do this?
// perform customer search
POM.HomePage.Login(user, password);
POM.HomePage.GoToSearchCustomer();
POM.SearchCustomerPage.EnterId(custIdFromSaveCustomerTest);
/*
... rest of test here
*/
}
The test method is a void method, so doesn't return anything (nor should it). One could imagine writing some value to file, that the second test reads. If at all possible, imo, you should avoid this temptation. In our basic example above we could do something like the following to satisfy code reuse:
[TestMethod]
public void SaveCustomer()
{
var custId = SaveCustomerAndReturnId("Test Save", "Test Address");
Assert.IsTrue(custId.Length == 5);
}
[TestMethod]
public void SearchCustomerById()
{
var custId = SaveCustomerAndReturnId("Test Search", "Test Address");
// perform customer search
POM.HomePage.Login(user, password);
POM.HomePage.GoToSearchCustomer();
POM.SearchCustomerPage.EnterId(custId);
/*
... rest of test here
*/
}
private int SaveCustomerAndReturnId(string custName, string custAddr)
{
POM.HomePage.Login(user, password);
POM.HomePage.GoToNewCustomer();
POM.NewOrderPage.EnterCustomerName(custName);
POM.NewOrderPage.EnterCustomerAddr(custAddr);
POM.NewOrderPage.SaveCustomer();
var custId = POM.SaveConfirmationPage.GetCustomerId(); // customer id generated by system
return custId;
}
In this case, if we run both tests, we create two customers which we might think is not as good as reusing first test's customer, but the advantage is we have stand alone tests, with code reuse.