使用基页对象扩展 loadablecomponent
extending loadablecomponent with base page object
我正在使用 selenium 页面对象模式并尝试将可加载组件合并到其中。我想要一个父页面对象 class,所有页面对象都从该父页面对象扩展,将具有我的常用方法。
我正在努力使它在不出现 java 内存不足错误的情况下工作。调试时出现以下错误
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
测试结束时没有堆栈跟踪,只是内存不足错误。
所以我的父页面对象class如下(搜索后这是唯一没有抛出编译时错误的方法)
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);
}
}
我的测试class非常简单,我的方法中有这行代码
driver = new FirefoxDriver();
HomePage homePage = new HomePage(driver);
当我点击 pagefactory 时它挂起然后抛出错误。我只能认为我错误地使用了泛型。
任何人都可以帮助我做错了什么?提前致谢
更新:
我也将 class 签名更改为以下但仍然出现相同的错误
public class HomePage extends SeleniumPageObject<HomePage>
我还对代码进行了更多调试,PageFactory 方法中的以下行引发了运行时异常。当它尝试创建我的主页的新实例时 class。代理参数的页面 class 作为 class selenium.selenium_test.HomePage 传入,它确实找到了此 class 的构造函数。
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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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 行更改为以下即可解决问题
PageFactory.initElements(driver, this);
我正在使用 selenium 页面对象模式并尝试将可加载组件合并到其中。我想要一个父页面对象 class,所有页面对象都从该父页面对象扩展,将具有我的常用方法。
我正在努力使它在不出现 java 内存不足错误的情况下工作。调试时出现以下错误
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
测试结束时没有堆栈跟踪,只是内存不足错误。
所以我的父页面对象class如下(搜索后这是唯一没有抛出编译时错误的方法)
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);
}
}
我的测试class非常简单,我的方法中有这行代码
driver = new FirefoxDriver();
HomePage homePage = new HomePage(driver);
当我点击 pagefactory 时它挂起然后抛出错误。我只能认为我错误地使用了泛型。
任何人都可以帮助我做错了什么?提前致谢
更新:
我也将 class 签名更改为以下但仍然出现相同的错误
public class HomePage extends SeleniumPageObject<HomePage>
我还对代码进行了更多调试,PageFactory 方法中的以下行引发了运行时异常。当它尝试创建我的主页的新实例时 class。代理参数的页面 class 作为 class selenium.selenium_test.HomePage 传入,它确实找到了此 class 的构造函数。
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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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 行更改为以下即可解决问题
PageFactory.initElements(driver, this);