在Appium和模拟器启动之前,我得到一个NullPointerException
。因此,尝试通过在代码中包含sysout
行进行调试完全没有帮助。
如果有人有任何建议,请发送,因为我快疯了!
我的依赖:
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta3</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
我的挂钩课程:
package MA.test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import java.net.MalformedURLException;
import java.net.URL;
public class Hooks {
public AppiumDriver driver;
public AppiumDriverLocalService service;
@Parameters({"platformVersion", "emulatorNumber", "deviceName", "port"})
@BeforeTest(alwaysRun = true)
public void startAppiumServer(String platformVersion, String emulatorNumber, String deviceName, String port) throws InterruptedException, MalformedURLException {
System.out.println("\n ABC");
service = new AppiumServiceBuilder()
.usingPort(Integer.valueOf(port))
.build();
service.start();
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, platformVersion);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, emulatorNumber);
caps.setCapability(AndroidMobileCapabilityType.AVD, deviceName);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.APPLICATION_NAME, "Name");
caps.setCapability(MobileCapabilityType.APPIUM_VERSION, "1.14.0");
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "activity");
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "package");
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:" + port + "/wd/hub"), caps);
System.out.println("\n Appium server: " + service.getUrl());
Thread.sleep(2000);
}
@AfterTest
public void teardown() {
service.stop();
driver.quit();
driver.closeApp();
System.out.println("\n Test quit");
}
}
我的TestRunner类:
package MA.steps;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(
plugin = {"pretty", "html:target/cucumber-reports"}
, monochrome = true
, features = "src/test/java/feature"
, tags = "@Login"
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
我的testng.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Android Parallel Execution" parallel="tests" thread-count="2" verbose="7">
<test name="Device1">
<parameter name="platformVersion" value="9.0"/>
<parameter name="emulatorNumber" value="emulator-5554"/>
<parameter name="deviceName" value="Android9_Nexus"/>
<parameter name="port" value="4723"/>
<classes>
<class name="MA.steps.TestRunner"/>
</classes>
</test>
</suite>
我收到以下错误:
java.lang.NullPointerException
在screens.myAccountOverviewScreen.logoutAccount(myAccountOverviewScreen.java:104)
指向下面的代码行:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
这是我的测试中第一次调用
driver
,但令我感到困惑的是,它甚至没有启动appium或启动仿真器就抛出了nullpointerexception
。 最佳答案
问题出在您的测试代码上。
您创建了一个套件xml文件,以仅包含您的TestRunner
类。但是,整个appium实例化逻辑都停留在Hooks
类中,该类通过TestNG配置批注@BeforeTest
和@AfterTest
来实现。但是该类既不包含在您的套件中,也不由您的测试类(TestRunner
)对其进行扩展。因此,不会调用您的配置,导致AppiumDriver
对象的值为空。
若要解决此问题,您可以执行以下任一操作:
编辑您的套件xml文件,并在其中也包含Hooks
类。 (要么)
构建一个实现IInvokedMethodListener
的TestNG侦听器,并将驱动程序实例移至其beforeInvocation
关于java - Appium,Java和Testng-查找NullPointerException的原因,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57728050/