简体   繁体   English

使用基本页面对象扩展loadablecomponent

[英]extending loadablecomponent with base page object

I am using the selenium page object pattern and trying to incorporate the loadable component into this. 我正在使用硒页面对象模式,并尝试将可加载组件合并到其中。 I want to have a Parent Page Object class that all the page objects extend from which will have my common methods. 我想拥有一个父页面对象类,所有页面对象都将从该类扩展而来,并具有我的常用方法。

I am struggling to get this to work without getting a java out of memory error. 我正在努力使它工作而没有出现Java内存不足错误。 When I debug I get the following error 调试时出现以下错误

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

At the end of the test there is no stack trace just a out of memory error. 在测试结束时,没有堆栈跟踪,只是一个内存不足错误。

So my parent page object class is as follows ( after searching this was the only way it didnt throw a compile time error) 所以我的父页面对象类如下(搜索后,这是它没有引发编译时错误的唯一方法)

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

}

I then have a page object (simplified below) 然后,我有一个页面对象(以下简化)

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

My test class is very simple it has this line of code in my method 我的测试类非常简单,它在我的方法中包含以下代码

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

When I hit the pagefactory it hangs and then throws the error. 当我点击pagefactory时,它将挂起,然后引发错误。 I can only think that i have used the generics incorrectly. 我只能认为我没有正确使用泛型。

Any one help with what I have done wrong? 有人帮我做错了吗? Thanks in advance 提前致谢

UPDATE: 更新:

I also changed the class signature to below but still got the same error 我也将类签名更改为以下,但仍然遇到相同的错误

public class HomePage extends SeleniumPageObject<HomePage>

I also did a little more debugging into the code and it is the following line in PageFactory method that throws the runtime exception. 我还对代码进行了一些调试,并且PageFactory方法中的以下行引发了运行时异常。 When it tries to create a new instance of my homepage class. 当它尝试创建我的主页类的新实例时。 The page class to proxy parameter is passed in as class selenium.selenium_test.HomePage and it does find the constructor for this class. 代理参数的页面类作为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);

below is the stack trace I get when running the test from Maven. 下面是从Maven运行测试时获得的堆栈跟踪。 When running direct from Eclipse only the top five lines are output. 从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)

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

PageFactory.initElements(driver, this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM