简体   繁体   中英

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. 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. 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. 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.

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. When running direct from Eclipse only the top five lines are output.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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