2015-05-24 61 views
0

我正在編寫一個Selenium框架,對於測試人員而言,使用和編寫測試相對較少的Java知識應該非常容易。爲了保持框架儘可能地方便用戶,我注意到我正在編寫大量靜態代碼,以便測試人員不必每次都實例化頁面對象和方法。這當然違背了OOP的原則。在深入研究這個問題之前,我想看看我的方法是否有可行的替代方案。我在包含實例化webdriver Singleton的登錄頁面類下面包含一個用戶名/密碼字段和一個登錄按鈕。這個頁面有兩個使用TestNG運行的測試。所有代碼如下:使用靜態的Selenium框架

LoginPage.java

public class LoginPage { 

    public static WebDriver driver; 
    private static String username_selector = "username"; 
    private static String password_selector = "password"; 
    private static String login_button_selector = "#loginbutton > input"; 
    private static String fail_message_selector = "error"; 


    public static void goTo(String environment_url){ 
     driver = Driver.getDriver(); 
     driver.get(environment_url); 
    } 

    public static void loginAs(String username, String password){ 
     WebElement user = driver.findElement(By.name(username_selector)); 
     user.clear(); 
     user.sendKeys(username); 
     WebElement pass = driver.findElement(By.name(password_selector)); 
     pass.clear(); 
     pass.sendKeys(password); 
     WebElement loginBtn = driver.findElement(By.cssSelector(login_button_selector)); 
     loginBtn.click(); 
    } 

    public static boolean loginErrorDisplayed(){ 
     WebElement failMessageContainer = driver.findElement(By.className(fail_message_selector)); 
      if(failMessageContainer.isDisplayed()){ 
       return true; 
      } 
      else return false; 
    } 

    public static String getLoginErrorMessage(){ 
     WebElement failMessageContainer = driver.findElement(By.className(fail_message_selector)); 
     String failMessage = failMessageContainer.getText(); 
     return failMessage; 
    } 

} 

Driver.java

public class Driver { 
    public static WebDriver driver = null; 

    public static WebDriver getDriver(){ 
     if(driver == null){ 
      driver = new FirefoxDriver(); 
     } 

     return driver; 
    } 

    public static void killDriver(){ 
     driver.quit(); 
    } 
} 

LoginTest.java

public class LoginTest { 

@Test(priority = 0) 
public void loginfail() { 
    LoginPage.goTo("http://127.0.0.1:8080/login"); 
    LoginPage.loginAs("wrong username", "wrongpassword"); 
    boolean didLoginFail = LoginPage.loginErrorDisplayed(); 
    Assert.assertTrue(didLoginFail == true, "Bad login was successful"); 
    if (didLoginFail){ 
     LoginPage.getLoginErrorMessage(); 
    } 
} 

@Test(priority = 1) 
public void loginsuccess() { 
    LoginPage.loginAs("correct_username", "correctpass"); 
    boolean didLoginFail = LoginPage.loginErrorDisplayed(); 
    Assert.assertTrue(didLoginFail == false, "Valid Login was unsuccessful"); 
} 

} 
+3

我投票關閉這一問題作爲題外話,因爲它應該是codereview.stackexchange.com –

回答

1

這不是你的問題的答案,但這將幫助你創建完美的硒框架。

我想建議你創建一個具有通用API的AbstractTestPage.java,比如goTo()API。您的所有頁面(如LoginPage.java)都應擴展AbstractTestPage.java以實現代碼重用性。

例如:

public abstract class AbstractTestPage { 

    public static WebDriver driver; 

    public static void goTo(String environment_url){ 
     driver = Driver.getDriver(); 
     driver.get(environment_url); 
    } 

} 

public class LoginPage extends AbstractTestPage { 

    . 
    . 
    . 
    . 
    . 
    . 
}