[英]How to call subclass methods when subclass objects are stored in superclass array?
[英]How can I call few objects of superclass inside different methods in one subclass?
我嘗試在Java + Selenium WebDriver上編寫自動化測試,該特定測試現在可以正常工作,但是在我看來,代碼質量存在問題-最奇怪的行是這里的每個測試都調用超類的對象。
我試圖通過刪除測試方法內的所有對象調用並在類內添加一個對象調用來重構它,但是得到了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();
}
那么,我該如何解決呢? 也許您還有其他關於Java / Selenium清晰代碼的建議,請寫。 我會非常感激。
非常感謝!
首先,我建議從頁面對象中刪除斷言,它將在將來陷入困境。
rpvp.AssertRecoverPasswordValidationPage(); // no :^C
其次,我認為您無需過多地忽略變量名,當您開始進行復雜的測試場景時,很難理解它。 下面的代碼:
ForgotPasswordPage fp = new ForgotPasswordPage(driver);
可以變成:
ForgotPasswordPage forgotPasswordPage = new ForgotPasswordPage(driver);
第三,如果您正在使用頁面工廠,並且測試將遵循一個順序(我知道測試應該是獨立的,但是在UI測試中幾乎是夢),則可以在@BeforeTest部分實例化您的頁面(請注意,您的@頁面對象中的FindBy事物僅在您首次使用后才會被“定位”,因此在測試類上使用它之前不要介意創建它的方式,請檢查有關@FindBy的答案 。 下面的代碼:
@BeforeTest
public void initialize() throws IOException{
driver = initializeDriver();
}
會變成:
// 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);
}
我建議您不要將下面的代碼移至父類(保持當前狀態),因為如果您計划將來在類級別並行運行某些事情,那么在測試時您可能會看到奇怪的事情發生類共享同一父級,當我們開始並行運行時,我遇到了50多個類的問題,您的測試類應控制何時啟動和結束WebDriver。
@BeforeTest
public void initialize() throws IOException{
driver = initializeDriver();
}
@AfterTest
public void shutdown() {
shutdownDriver();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.