gpt4 book ai didi

java - 西装运行时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 17:55:47 25 4
gpt4 key购买 nike

我是自动化测试新手。我尝试编写 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("android", "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();
}

}

这是我的一个测试类(class)

<小时/>
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("android") && 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("android") && 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("android")){
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("android") && 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>

如果您需要更多信息,请告诉我。

最佳答案

您没有清楚地解释 NullPointerException 在哪里(即哪一行)被抛出。

我认为有两种可能性:

  1. 由于某种原因,justSignIn 未被调用。我不明白为什么......但也许我错过了一些东西。

  2. 正在调用您的 justSignIn 方法,但在将值分配给 driver 之前抛出并处理了异常。您的处理程序应该应该打印堆栈跟踪,但我想您可能没有在适当的日志文件中查看它。

捕获所有异常然后继续使用 null 驱动程序是一个非常糟糕的主意。您最好让所有异常传播,以便 TestNG 可以脱离测试套件。

关于java - 西装运行时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60720467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com