运行时发生java空指针异常
我是自动化测试的新手。我尝试编写UI自动化测试脚本并为它们创建套件。但当我运行我的套装时,它只运行第一类并抛出空指针异常。我调试我的代码,并看到驱动程序在传递到第二个类时为空。但我不能做任何事情来修复它。请帮帮我
这是我的基础课
package Helpers;
import Pages.LoginPage;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.aspectj.lang.annotation.Before;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;
import java.io.File;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class BaseClass extends Devices {
protected AppiumDriver<MobileElement> driver;
@BeforeSuite
protected void justSignIn() {
try {
Devices devices = new Devices("安卓", "Pixel 4 XL");
devices.deviceManager();
File appDir = new File("/Users/Narek/Downloads/SoloLearn.ipa");
File newAPP = new File(appDir, "SoloLearn.ipa");
driver = new AppiumDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), devices.caps);
} catch (Exception exp) {
System.out.println("Cause is : " + exp.getCause());
System.out.println("Message is : " + exp.getMessage());
exp.printStackTrace();
}
LoginPage loginPage = new LoginPage(driver);
loginPage.logInToApp("demo21@sololearn.com", "password11");
}
@AfterSuite
protected void teardown(){
driver.quit();
}
}
这是我的一门考试课
package Tests;
import Helpers.BaseClass;
import Pages.LeaderboardPage;
import Pages.PlayPage;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class LeaderboardTest extends BaseClass {
LeaderboardPage leaderboardPage;
@BeforeMethod
public void setUp(){
leaderboardPage = new LeaderboardPage(driver);
}
@Test(priority = 1)
public void openLeaderboardFOLLOWING(){
ExtentTest test = extent.createTest("Positive test", "Open Leaderboard Following tab");
test.log(Status.INFO, "Test is starting");
leaderboardPage.openLeaderboard();
if(platform.equals("安卓") && leaderboardPage.getFollowingTabTitle()){
return;
}
else{
String followingTabTitle = leaderboardPage.getFollowingTabTitleiOS();
Assert.assertEquals(followingTabTitle, "FOLLOWING");
}
test.log(Status.PASS, "Open Leaderboard and make sure that opened 'FOLLOWING' tab");
String followingTitle = leaderboardPage.getFollowingTitle();
Assert.assertEquals(followingTitle, "YOUR NETWORK");
test.log(Status.PASS, "Open Leaderboard and make sure that Your Network is displayed");
test.log(Status.INFO, "Test is over");
}
@Test(priority = 2)
public void openLeaderboardLOCAL(){
ExtentTest test = extent.createTest("Positive test", "Open Leaderboard Local tab");
test.log(Status.INFO, "Test is starting");
leaderboardPage.goToLocalTab();
if(platform.equals("安卓") && leaderboardPage.getlocalTabTitle()){
return;
}
else{
String localTabTitle = leaderboardPage.getLocalTabTitleiOS();
Assert.assertEquals(localTabTitle, "LOCAL");
}
test.log(Status.PASS, "Tap on Local and make sure that opened 'LOCAL' tab");
String localTitle = leaderboardPage.getLocalTitle();
if(platform.equals("安卓")){
Assert.assertEquals(localTitle, "CHANGE");
}else{
Assert.assertEquals(localTitle, "ARMENIA");
}
test.log(Status.PASS, "make sure that 'CHANGE' button or ARMENIA is displayed");
test.log(Status.INFO, "Test is over");
}
@Test(priority = 3)
public void openLeaderboardGLOBAL(){
ExtentTest test = extent.createTest("Positive test", "Open Leaderboard Global tab");
test.log(Status.INFO, "Test is starting");
leaderboardPage.goToGlobalTab();
if(platform.equals("安卓") && leaderboardPage.getGlobalTabTitle()){
return;
}
else{
String globalTabTitle = leaderboardPage.getGlobalTabTitleiOS();
Assert.assertEquals(globalTabTitle, "GLOBAL");
}
test.log(Status.PASS, "Tap on Global and make sure that opened 'GLOBAL' tab");
String globalTitle = leaderboardPage.getGlobalTitle();
Assert.assertEquals(globalTitle, "LEADERS");
test.log(Status.PASS, "make sure that 'LEADERS' title is displayed");
leaderboardPage.backFromLeaderboard();
test.log(Status.INFO, "Test is over");
}
}
这是我的套房
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name = "Test" verbose="1">
<test name = "teeeeeeessssttt">
<classes>
<class name = "Tests.LeaderboardTest" />
<class name = "Tests.ProfileTest" />
</classes>
</test>
</suite>
如果你需要更多信息,请告诉我
# 1 楼答案
您没有清楚地解释
NullPointerException
被抛出的位置(即在哪一行)我认为有两种可能性:
由于某种原因
justSignIn
没有被调用。我看不出有什么理由。。。但也许我错过了什么正在调用
justSignIn
方法,但在将值分配给driver
之前会引发并处理异常。您的处理程序应该应该打印stacktrace,但我猜您可能没有查看相应的日志文件来查看它捕获所有异常,然后继续使用
null
驱动程序是一个非常糟糕的主意。最好让所有异常传播,这样TestNG就可以退出测试套件