在Java中使用PageFactory实现Webdriver PageObject

在Java中使用PageFactory实现Webdriver PageObject,webdriver,pageobjects,Webdriver,Pageobjects,以下是我到目前为止的情况: 一个基于Webdriver的Java类,它可以登录到应用程序并进入主页: import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; i

以下是我到目前为止的情况:

一个基于Webdriver的Java类,它可以登录到应用程序并进入主页:

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class MLoginFFTest {

    private WebDriver driver;
    private String baseUrl;
    private String fileName = "screenshot.png";

    @BeforeMethod
    public void setUp() throws Exception {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("network.http.phishy-userpass-length", 255);
        profile.setAssumeUntrustedCertificateIssuer(false);

        driver = new FirefoxDriver(profile);

        baseUrl = "https://a.b.c.d/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testAccountLogin() throws Exception {
        driver.get(baseUrl + "web/certLogon.jsp");
        driver.findElement(By.name("logonName")).clear();

        AssertJUnit.assertEquals(driver.findElement(By.name("logonName"))
                .getTagName(), "input");

        AssertJUnit.assertEquals(driver.getTitle(), "DA Logon");

        driver.findElement(By.name("logonName")).sendKeys("username");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("password");
        driver.findElement(By.name("submit")).click();
        driver.findElement(By.linkText("Account")).click();


        AssertJUnit.assertEquals(driver.getTitle(), "View Account");


    }

    @AfterMethod
    public void tearDown() throws Exception {

        File screenshot = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);

        try {
            FileUtils.copyFile(screenshot, new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver.quit();

    }

}
现在我们看到有两页: 1.登录页面,其中我必须输入用户名和密码,以及主页,一旦身份验证成功,我将被带到这里

现在我想使用Pagefactory将其实现为PageObjects:

package com.example.pageobjects;

import static com.example.setup.SeleniumDriver.getDriver;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;



public abstract class MPage<T> {

    private static final String BASE_URL = "https://a.b.c.d/";
    private static final int LOAD_TIMEOUT = 30;
    private static final int REFRESH_RATE = 2;

    public T openPage(Class<T> clazz) {
        T page = PageFactory.initElements(getDriver(), clazz);
        getDriver().get(BASE_URL + getPageUrl());
        ExpectedCondition pageLoadCondition = ((MPage) page).getPageLoadCondition();
        waitForPageToLoad(pageLoadCondition);
        return page;
    }

    private void waitForPageToLoad(ExpectedCondition pageLoadCondition) {
        Wait wait = new FluentWait(getDriver())
                .withTimeout(LOAD_TIMEOUT, TimeUnit.SECONDS)
                .pollingEvery(REFRESH_RATE, TimeUnit.SECONDS);

        wait.until(pageLoadCondition);
    }

    /**
     * Provides condition when page can be considered as fully loaded.
     *
     * @return
     */
    protected abstract ExpectedCondition getPageLoadCondition();

    /**
     * Provides page relative URL/
     *
     * @return
     */
    public abstract String getPageUrl();
}
package com.example.pageobjects;
导入静态com.example.setup.SeleniumDriver.getDriver;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.support.PageFactory;
导入org.openqa.selenium.support.ui.ExpectedCondition;
导入org.openqa.selenium.support.ui.FluentWait;
导入org.openqa.selenium.support.ui.Wait;
公共抽象类MPage{
私有静态最终字符串BASE_URL=”https://a.b.c.d/";
私有静态最终整数加载\u超时=30;
私有静态最终整数刷新率=2;
公共T openPage(类clazz){
T page=PageFactory.initElements(getDriver(),clazz);
getDriver().get(BASE_URL+getPageUrl());
ExpectedCondition pageLoadCondition=((MPage)页)。getPageLoadCondition();
waitForPageToLoad(pageLoadCondition);
返回页面;
}
私有无效waitForPageToLoad(ExpectedCondition pageLoadCondition){
Wait Wait=new FluentWait(getDriver())
.withTimeout(加载超时,时间单位为秒)
.pollingEvery(刷新率,时间单位为秒);
等待.直到(pageLoadCondition);
}
/**
*提供页面被视为完全加载时的条件。
*
*@返回
*/
受保护的抽象ExpectedCondition getPageLoadCondition();
/**
*提供页面相对URL/
*
*@返回
*/
公共抽象字符串getPageUrl();
}

对于登录页面,我不确定我将如何实现它,以及调用这些页面的测试。

我希望这些链接将有帮助:

我建议让一个类负责before/after方法,并且应该在整个场景之前和之后调用它们。现在你会在登录到你的页面后关闭webdriver,我想这不是你想要的行为。您可以为登录页面和主页中发生所有单击的页面(现在您的mloginftest类正在登录)提取一个抽象级别。 现在,另一个类只需运行页面中的方法,如下所示:

@Test
public void shouldOpenMainPage(){
   LoginPage loginPage = new LoginPage();
   MainPage mainPage = loginPage.loginCorrectly();
   mainPage.verifyOnMainPage();
   mainPage.doSthElse();
   verifySth(....);
}
所以现在你的文件结构可能是

++pages/LoginPage.class
++pages/MainPage.class
++steps/SomeTest.class
希望这有帮助