简体   繁体   中英

How can I call few objects of superclass inside different methods in one subclass?

I try to write automated tests on Java + Selenium WebDriver, this particular test works properly now, but in my opinion, there is a problem with quality of code - the most weird lines are calling objects of the superclass for every test here.

I tried to refactor it by removing all object calls inside the test methods and adding one object call inside class, but got a NullPointerException.

@BeforeTest
public void initialize() throws IOException{
    driver = initializeDriver();
}

@Test
public void sendRecoverEmailValid() {

    ForgotPasswordPage fp = new ForgotPasswordPage(driver);
    fp.AssertForgotPasswordPage();

    fp.setRecoverEmail(validRecoveryEmail);
    fp.pressSendButton();

    RecoverPasswordValidationPage rpvp = new RecoverPasswordValidationPage(driver);
    rpvp.AssertRecoverPasswordValidationPage();

}

@Test
public void sendRecoverEmailInvalid() {

    ForgotPasswordPage fp = new ForgotPasswordPage(driver);
    fp.AssertForgotPasswordPage();

    fp.setRecoverEmail(invalidRecoveryEmail);
    fp.pressSendButton();
    fp.AssertRecoverEmailErrorPresece();

}

@AfterTest
public void shutdown() {
    shutdownDriver();
}

So, how can I solve it? And maybe you have any other recommendations regarding clear code for java/selenium, please write. I'll be very thankful.

Thanks a lot!

First, I would suggest to remove assertion from Page Objects, it will turn in a hell in the future.

rpvp.AssertRecoverPasswordValidationPage(); // no :^C

Second,I don't think you need to skimp the variable name that much, it get's really hard to read when you start doing complex test scenarios. This code below:

ForgotPasswordPage fp = new ForgotPasswordPage(driver);

can turn into:

ForgotPasswordPage forgotPasswordPage = new ForgotPasswordPage(driver);

Third, if you are using page factory and your tests WILL follow a sequence (I know that tests should be independent but in UI testing it's almost a dream), you can instantiate your pages on the @BeforeTest part (Keep in mind that your @FindBy things of your page objects will be "located" only after you first use it, so don't mind of create it way before you use on the test class check this answer about @FindBy ). This code below:

@BeforeTest
public void initialize() throws IOException{
    driver = initializeDriver();
}

will turn into:

// in case you are using JUnit, this attributes should be static, don't remember about TestNG. (Nullpointer situation)
private ForgotPasswordPage forgotPasswdPage;
private RecoverPasswordValidationPage recoverPasswdValidationPage;

@BeforeTest
public void initialize() throws IOException{
    driver = initializeDriver();
    forgotPasswdPage = new ForgotPasswordPage(driver);
    recoverPasswdValidationPage = new RecoverPasswordValidationPage(driver);
}

I would suggest you to don't move this code below to a parent class (keep it as it's now), becuase if you plan to run things in parallel at class level in the future, you may see weird things happen, as your test class share the same parent, I faced this issue with more than 50 classes when we started moving to parallel, your test class should control when to start and end a WebDriver.

@BeforeTest
public void initialize() throws IOException{
    driver = initializeDriver();
}

@AfterTest
public void shutdown() {
    shutdownDriver();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM