简体   繁体   English

如何在一个子类中的不同方法中调用超类的几个对象?

[英]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. 我尝试在Java + Selenium WebDriver上编写自动化测试,该特定测试现在可以正常工作,但是在我看来,代码质量存在问题-最奇怪的行是这里的每个测试都调用超类的对象。

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. 我试图通过删除测试方法内的所有对象调用并在类内添加一个对象调用来重构它,但是得到了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. 也许您还有其他关于Java / Selenium清晰代码的建议,请写。 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 ). 第三,如果您正在使用页面工厂,并且测试遵循一个顺序(我知道测试应该是独立的,但是在UI测试中几乎是梦),则可以在@BeforeTest部分实例化您的页面(请注意,您的@页面对象中的FindBy事物仅在您首次使用后才会被“定位”,因此在测试类上使用它之前不要介意创建它的方式,请检查有关@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. 我建议您不要将下面的代码移至父类(保持当前状态),因为如果您计划将来在类级别并行运行某些事情,那么在测试时您可能会看到奇怪的事情发生类共享同一父级,当我们开始并行运行时,我遇到了50多个类的问题,您的测试类应控制何时启动和结束WebDriver。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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