我使用Selenium和JUnit创建了一个用于培训的Java项目。第一次测试将用户连接到网站,第二次测试在用户连接时检查主页中的信息。我不明白为什么我的第二个测试开始并停止NullPointerException ......
我使用这个文件:
主要文件=>声明一个JunitCore并运行两个类测试(通过jCore.run(test1.class,test2.class)
主要:
public class Main {
public static void main(String[] args){
JUnitCore jCore = new JUnitCore();
//jCore.addListener(new ExecutionListener());
jCore.run(WebsiteLoginTest.class, WebsiteCheckEventTest.class);
}
}
两个测试类文件:
test1.java:
public class Test1 {
@Test
public void testlogin(){
WebsiteHomePage homePage = new WebsiteHomePage().open();
homePage.login(...);
}
}
test2.java:
public class WebsiteCheckEventTest {
@Test
public void websiteCheckEvent(){
System.out.println("a");
WebsiteHomePageConnected homePageConnected = new WebsiteHomePageConnected();
System.out.println("b");
homePageConnected.checkEvent();
System.out.println("c");
}
}
两个"页面"文件:(使用页面对象模式)
WebsiteHomePage(未记录):
public class WebsiteHomePage extends WebsitePage<WebsiteHomePage> {
@FindBy(id = "usernameLogin") WebElement usernameInputText;
@FindBy(id = "passwordLogin") WebElement passwordInputText;
@FindBy(id = "loginSubmit") WebElement loginInputSubmit;
@Override
public ExpectedCondition getPageLoadCondition() {
return ExpectedConditions.titleContains("Homepage");
}
@Override
public String getPageUrl() {
return "";
}
public void login(String univers, String login, String password){
usernameInputText.sendKeys(login);
passwordInputText.sendKeys(password);
loginInputSubmit.click();
}
public WebsiteHomePage open() {
return new WebsiteHomePage().openPage(WebsiteHomePage.class);
}
}
WebsiteHomePageConnected(当用户通过第一次测试连接时):
public class WebsiteHomePageConnected extends WebsitePage<WebsiteHomePageConnected> {
@FindBy(id = "playerName") WebElement playerNameLi;
@FindBy(id = "eventContent") WebElement eventContentDiv;
@FindBy(id = "eventListWrap") WebElement eventListDiv;
@Override
public ExpectedCondition getPageLoadCondition() {
return ExpectedConditions.visibilityOf(playerNameLi);
}
@Override
public String getPageUrl() {
return "";
}
public void checkEvent(String event){
System.out.println("111"); // is displayed in the console
eventContentDiv.click();
System.out.println("222"); // isn't displayed in the console
}
}
当我运行程序时,控制台显示: 一个 b C 111
...但是没有222,程序在&#34; eventContentDiv.click()&#34;行中的WebsiteHomePageConnected文件中停止。没有任何错误消息。
在主文件中,如果我取消注释//jCore.addListener(new ExecutionListener())控制台显示:
a
b
c
111
Execution of test case failed : java.lang.NullPointerException
at page.WebsiteHomePageConnected.checkEvent(WebsiteHomePageConnected.java:34)
at test.WebsiteCheckEventTest.websiteCheckEvent(WebsiteCheckEventTest.java:28)
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:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.runner.JUnitCore.run(JUnitCore.java:105)
at org.junit.runner.JUnitCore.run(JUnitCore.java:94)
at Main.main(Main.java:32)
所以,&#34; eventContentDiv&#34;似乎是问题所在,但是当我手动检查网站时,在连接之后,我找到了带有id&#34; eventContent&#34;的div。 (通过firebug ctrl + f)。手动,更确切地说:
那么...为什么我的程序找不到这个元素?
〜UPDATE〜 这是抽象的网站类:
public abstract class WebsitePage<T>{
private static final String BASE_URL = "http://www.test.com/";
public T openPage(Class<T> pageObject){
T page = PageFactory.initElements(getDriver(), pageObject);
getDriver().get(BASE_URL + getPageUrl());
ExpectedCondition pageLoadCondition = ((WebsitePage) page).getPageLoadCondition();
WaitForPageToLoad(pageLoadCondition);
return page;
}
private static final int LOAD_TIMEOUT = 30;
private static final int REFRESH_RATE = 2;
private void WaitForPageToLoad(ExpectedCondition pageLoadCondition) {
Wait wait = new FluentWait(getDriver())
.withTimeout(LOAD_TIMEOUT, TimeUnit.SECONDS)
.pollingEvery(REFRESH_RATE, TimeUnit.SECONDS);
}
public abstract ExpectedCondition getPageLoadCondition();
public abstract String getPageUrl();
}
〜更新2~ 我在公共类WebsiteHomePageConnected中的com中犯了一个错误。变化:
...
System.out.println("222"); // is displayed in the console
...
到
...
System.out.println("222"); // isn't displayed in the console
...