Selenium Framework使用静态

时间:2015-05-24 11:05:21

标签: java selenium

我正在编写一个Selenium框架,对于使用和编写测试的Java知识相对较少的测试人员而言应该非常容易。为了使框架尽可能地保持用户友好性,我注意到我正在编写大量静态函数,以便测试人员不必每次都实例化页面对象和方法。这当然违背了OOP的原则。在我深入研究之前,我想看看我的方法是否有可行的替代方案。我在登录页面类下面包括实例化webdriver Singleton,有一个用户名/密码字段和一个登录按钮。此页面有2个使用TestNG运行的测试。要遵循的所有代码:

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");
}

}

1 个答案:

答案 0 :(得分:1)

这不是您的问题的答案,但这将帮助您创建完美的硒框架。

我建议您创建一个具有常见API的AbstractTestPage.java,例如goTo()API。您的所有页面(如LoginPage.java)都应扩展AbstractTestPage.java以实现代码可重用性。

例如:

static final AtomicInteger atomicInteger = new AtomicInteger(initialValue);
...
if (atomicInteger.get() <= 0) {
    return "imposibble. value=0";
}
int result = atomicInteger.decrementAndGet();
if (result < 0) {
    atomicInteger.incrementAndGet(); // Revert the effect of decrementing
    return "imposibble. value=0";
} else {
    return "new value=" + result;
}