使用基页对象扩展loadablecomponent

时间:2015-02-09 12:00:29

标签: java generics selenium selenium-webdriver

我正在使用selenium页面对象模式并尝试将可加载组件合并到此中。我希望有一个父页面对象类,所有页面对象都可以扩展,这将有我的常用方法。

我正在努力让这个工作没有得到java内存错误。当我调试时,我得到以下错误

org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.

在测试结束时,没有堆栈跟踪只是内存不足错误。

所以我的父页面对象类如下(在搜索之后这是它没有抛出编译时错误的唯一方法)

public abstract class SeleniumPageObject<T extends SeleniumPageObject<T>> extends LoadableComponent<T> {

    protected WebDriver driver;

    protected void clearAndType(WebElement field, String text) {
        field.clear();
        field.sendKeys(text);
    }

    protected String getPageTitle() {
        return driver.getTitle();
    }

}

然后我有一个页面对象(简化如下)

package selenium.selenium_test;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class HomePage extends SeleniumPageObject {

    private final static String baseUrl = "https://someUrl";
    //WebDriver driver;

    @FindBy(css = "a[href*='tell-me-more']")
    WebElement link_TellMeMore;

    @FindBy(css = "a[href*='registration']")
    WebElement link_Register;


    public HomePage(WebDriver driver) {
        super();
        this.driver = driver;
        PageFactory.initElements(driver, HomePage.class);
    }

    @Override
    protected void isLoaded() throws Error {
        String url = driver.getCurrentUrl();
        Assert.assertTrue("Home page has not been loaded correctly", baseUrl.equals(url));
    }   

    @Override
    protected void load() {
        driver.get(baseUrl);        
    }

    public RegisterPageOne clickRegisterLink() {
        link_Register.click();
        return new RegisterPageOne(driver);
    }
}

我的测试类很简单,我的方法中有这行代码

driver = new FirefoxDriver();
HomePage homePage = new HomePage(driver);

当我点击pagefactory时,它会挂起然后抛出错误。我只能认为我错误地使用了泛型。

任何一个帮助我做错了什么?提前致谢

更新:

我还将类签名更改为下面但仍然出现相同的错误

public class HomePage extends SeleniumPageObject<HomePage>

我还对代码进行了一些调试,它是PageFactory方法中的以下行,它抛出了运行时异常。当它尝试创建我的主页类的新实例时。代理参数的页面类作为类selenium.selenium_test.HomePage传入,它确实找到了此类的构造函数。

private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy)
Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);
return constructor.newInstance(driver);

下面是我从Maven运行测试时得到的堆栈跟踪。直接从Eclipse运行时,只输出前五行。

java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Arrays.java:2694)
    at java.lang.String.<init>(String.java:203)
    at java.lang.StringBuffer.toString(StringBuffer.java:561)
    at java.io.StringWriter.toString(StringWriter.java:210)
    at org.junit.runner.notification.Failure.getTrace(Failure.java:76)
    at org.apache.maven.surefire.common.junit4.JUnit4StackTraceWriter.writeTrimmedTraceToString(JUnit4StackTraceWriter.java:69)
    at org.apache.maven.surefire.booter.ForkingRunListener.encode(ForkingRunListener.java:317)
    at org.apache.maven.surefire.booter.ForkingRunListener.toString(ForkingRunListener.java:250)
    at org.apache.maven.surefire.booter.ForkingRunListener.testError(ForkingRunListener.java:127)
    at org.apache.maven.surefire.common.junit4.JUnit4RunListener.testFailure(JUnit4RunListener.java:110)
    at org.junit.runner.notification.SynchronizedRunListener.testFailure(SynchronizedRunListener.java:63)
    at org.junit.runner.notification.RunNotifier$4.notifyListener(RunNotifier.java:142)
    at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72)
    at org.junit.runner.notification.RunNotifier.fireTestFailures(RunNotifier.java:138)
    at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:132)
    at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:23)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:329)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)

1 个答案:

答案 0 :(得分:1)

对于有相同问题的任何人,将PageFactory Line更改为以下解决问题

PageFactory.initElements(driver, this);