Issue
I'm trying to create a JUnit test that requires a user to log in with an admin account.
To make it so that I don't have to repeat code I've done a Login
JUnit test and a GoToAdminPage
JUnit test.
- The first accesses the login page and logs in.
- The second attempts to reach a menu only accessible by admins, and is meant to pick up right where the first leaves off. What I want is for
GoToAdminPage
is to start on the very same page and with the very same environment thatLogin
ends.
The idea is that by creating a Login
automated test, I can reuse it for whatever test I want to do in the future that requires admin rights.
What I'm trying to do is, in the setUp()
of GoToAdminPage
, call the first test.
However, what this does is that it launches a test browser for GoToAdminPage
, and then immediately launches another test browser for Login
which closes since Login.tearDown()
contains a driver.quit()
instruction.
What I want is for GoToAdminPage
expects to pick up right on the same page Login
ends, it has no get
instruction so it never leaves the starting Firefox page.
I've already tried a couple of things. Each of my tests inherits the BasicTest
class, which defines a number of things (WebDriver, JS Executor, generic utility functions, etc.). In that class, I created a perform()
function that calls setUp()
and test()
.
That way, in GoToAdminPage
, all I would have to do is call Login.perform()
in GoToAdminPage.setUp()
, and that would technically achieve what I want to do. However, it doesn't, and has the same effect as I mentioned above.
Looking online, I saw that I could also make it so that GoToAdminPage extends Login
, but that also has the same result.
Finally I attempted to place the entire Login.test()
process in a separate function in Login
class that I could call inside GoToAdminPage
, yet this has the same result.
In summation, what seems to be happening is that, when I call another JUnit test inside GoToLoginPage
, it performs the test, but does it as a separate test.
What I want is to be able to essentially make it all one big test, where, once I've called Login
, I can interact with the post-login page with my user logged in.
Essentially I just want GoToAdminPage
to be an extension of Login
's process.
Is this possible?
Here is my current code:
Login.java
public class Login extends BasicTest {
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
super.setUp();
driver.get(constants.connectionUrl);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void test() throws Exception {
this.login(driver);
}
public void login(WebDriver d) throws InterruptedException {
...login process using webdriver....
}
}
GoToAdminPage.java
public class GoToAdminPage extends BasicTest {
Login Login = new Login();
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
super.setUp();
Login.login(driver);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void test() throws Exception {
...post login test process...
}
}
Yet still, this has the aforementioned effect.
Solution
In the end I found the solution:
GoToAdminPage
extendsLogin
GoToAdminPage.setUp()
callsparent.setUp()
GoToAdminPage.test()
callsparent.test()
Using this I was able to make my second test use its parent as a setup phase.
Answered By - Grumbunks