在IntelliJ(OSX)中运行Android测试时出现java.lang.NullPointerException

时间:2016-06-22 18:22:38

标签: java android appium

我已经设置了一个Android手机模拟器,其中包含我试图使用Appium和IntelliJ进行测试的应用程序komoot。我正在使用macbook air。拥有:启动我的Android模拟器,启动Appium服务器,并在IntelliJ中编写我的测试(在java中),我运行我的测试,它产生附加图片中看到的错误。我还复制/粘贴了我的简单测试脚本和运行我的测试" komootTest"产生的错误日志消息。

我对被抛出的错误感到困惑。根据第44行,我实例化一个变量,它是komoot(登录按钮)中具有正确id的元素。为什么在这种情况下它会抛出一个null异常?我可以根据需要提供更多细节,谢谢!

error log image

komootTest.java code image

以下是我的测试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();
    }
}

以下是运行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

1 个答案:

答案 0 :(得分:0)

问题解决了!我通过在IntelliJ中开始一个新项目来重写项目,而不是选择Maven和Java。然后我在pom.xml文件中导入了依赖项,如下所示。我没有完全指定我的功能,我没有将我的依赖项包含在pom.xml文件中。我确保在我的项目结构/项目设置/库中包含java-client-4.0.0.jar和selenium独立服务器.jar文件。

以下是我的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>

以下是我能够成功运行的简单代码:

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();
    }
}

以下是生成的执行日志:

[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