[英]java.lang.NullPointerException when running Android test in IntelliJ (OSX)
I've set up an Android phone emulator that contains the app komoot which I seek to perform testing on using Appium and IntelliJ to write the test. 我已经建立了一个Android手机仿真器,其中包含应用程序komoot,我试图通过Appium和IntelliJ编写测试来进行测试。 I'm working out of a macbook air. 我在忙于Macbook。 After having: started up my Android emulator, launched the Appium server, and written my test (in java) in IntelliJ, I run my test and it produces the error seen in the attached pictures. 在具备以下条件之后:启动我的Android模拟器,启动Appium服务器,并使用IntelliJ编写我的测试(以Java语言编写),然后运行测试,并产生所附图片中所示的错误。 I have also copied/pasted my simple testing script and the error log messages that result from running my test "komootTest". 我还复制/粘贴了我的简单测试脚本以及由于运行测试“ komootTest”而导致的错误日志消息。
I am confused about the error being thrown. 我对抛出的错误感到困惑。 As per line 44 I am instantiating a variable that is an element in komoot (the login button) with the correct id. 按照第44行,我实例化一个变量,该变量是komoot中的元素(登录按钮),具有正确的ID。 Why would it throw a null exception in this case? 为什么在这种情况下会引发null异常? I can provide more details as needed, thank you! 我可以根据需要提供更多详细信息,谢谢!
komootTest.java code image komootTest.java代码映像
Here is the code for my test komootTest.java: 这是我的测试komootTest.java的代码:
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import java.net.URL;
public class komootTest
{
AppiumDriver driver;
@Before
public void setUp() throws Exception
{
//set desired capabilities and specify device name
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "BigBrother Emulator");
//capabilities.setCapability("appPackage", "de.komoot.android");
//capabilities.setCapability("appActivity", "com.google.android.gms.auth.api.signin.internal.SignInHubActivity");
//establish a connection with the server
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@After
public void end() throws Exception
{
//kill connection with server after test has been executed
driver.quit();
}
@Test
public void logInHereButton()
{
//reference UI element by ID and click it
WebElement logInHere = driver.findElement(By.id("de.komoot.android:id/textview_login"));
logInHere.click();
}
}
Here is the error message when I run komootTest.java: 这是我运行komootTest.java时的错误消息:
[TestNG] Running:
/Users/coracoleman/Library/Caches/IdeaIC2016.1/temp-testng-customsuite.xml
java.lang.NullPointerException
at komootTest.logInHereButton(komootTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
===============================================
Default Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
Process finished with exit code 0
Problem solved! 问题解决了! I rewrote the project by beginning a new project in IntelliJ, instead selecting Maven versus Java. 我通过在IntelliJ中开始一个新项目重写了该项目,而不是选择Maven vs Java。 I then imported the dependencies in the pom.xml file as I have included below. 然后,将依存关系导入pom.xml文件,如下所示。 I had not fully specified my capabilities and I had not included my dependencies in the pom.xml file. 我没有完全指定自己的功能,也没有在pom.xml文件中包含依赖项。 I made sure to include the java-client-4.0.0.jar and selenium standalone server .jar file in my Project Structure/Project Settings/Library. 我确保在项目结构/项目设置/库中包含java-client-4.0.0.jar和selenium独立服务器.jar文件。
Here are the contents of my pom.xml file: 这是我的pom.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>AppiumTest2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Here is the simple code that I was able to successfully run: 这是我能够成功运行的简单代码:
package scenarios;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class AndroidSetup
{
WebDriver driver;
@Test
public void setUp() throws Exception
{
//create object of DesiredCapabilities class
DesiredCapabilities capabilities = new DesiredCapabilities();
//set android deviceName desired capability
capabilities.setCapability("deviceName", "BigBrother Emulator");
//set browserName desired capability
capabilities.setCapability("browserName", "Android");
//set android platformVersion desired capability
capabilities.setCapability("platformVersion", "5.1");
//set android platformName desired capability
capabilities.setCapability("platformName", "Android");
//set android appPackage desired capability
capabilities.setCapability("appPackage", "de.komoot.android");
//set android appActivity desired capability
capabilities.setCapability("appActivity", "de.komoot.android.app.InspirationActivity");
//set appium server address and port number in URL string
//this will launch app in emulator
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//click on LogIn button
driver.findElement(By.id("de.komoot.android:id/textview_login")).click();
driver.quit();
}
}
Here is the resulting execution log: 这是生成的执行日志:
[TestNG] Running:
/Users/coracoleman/Library/Caches/IdeaIC2016.1/temp-testng-customsuite.xml
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.