AppiumDriver 给出空指针异常 [英] AppiumDriver gives null pointer exception

查看:30
本文介绍了AppiumDriver 给出空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 appium + testng 应用中,我放的是:

In my appium + testng app, what I have put is:

package com.tribehr.ios.ios_test;
import java.net.URL;
import io.appium.java_client.AppiumDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import org.testng.Assert;

public class NewTest {
    private AppiumDriver driver;

    @Before
    public void setUp() throws Exception {
        // set up appium
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformVersion", "7.1");
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("deviceName", "iPhone Simulator");
        capabilities.setCapability("app", "/path to .app");
        driver = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public void Test1() throws Exception {
driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]/UIATextField[1]"));
  } 
}

给出了以下堆栈跟踪:

[TestNG] Running:
  /private/var/folders/hh/r5f5h1lx23g6drvycy03g29w0000gs/T/testng-eclipse--1877089062/testng-customsuite.xml

FAILED: Test1
java.lang.NullPointerException
    at com.tribehr.ios.ios_test.NewTest.Test1(NewTest.java:39)
    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:483)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7b1d7fff: 23 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@7b3300e5: 28 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 2 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@2ff5659e: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@43556938: 4 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@57fa26b7: 2 ms

似乎 Appium 服务器没有使用该功能成功实例化,但这几乎是在线示例的复制和粘贴 - 我无法弄清楚出了什么问题..

It seems like the Appium server is not instantiated successfully with the capability, but this is almost a copy-and-paste of examples online - and I cannot figure out what goes wrong..

推荐答案

用 if 语句包围driver",以了解它是否 == null.

if(driver!=null){
    System.out.println("driver does not == null");
    driver.findElement(By.xpath("YourXpath"));
} else {
    System.out.println("driver == null")
}

这是我的能力 使用 appium 1.2*

public WebDriver appiumCapabilities() {
    File appDir = new File(System.getProperty("user.dir"), "/app/");
    File app = new File(appDir, "test.app");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
    capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("deviceName", "iPod");
    capabilities.setCapability("platformVersion", "7.1");
    capabilities.setCapability("device", "iphone");
    capabilities.setCapability("udid", "cd827d3778cfdee2fc7210f8f44184821a083c06");
    capabilities.setCapability("app", app);

    try {
        driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return driver;
}

这篇关于AppiumDriver 给出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆