gpt4 book ai didi

java - AppiumDriver 给出空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:45:14 26 4
gpt4 key购买 nike

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

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 服务器没有成功实例化该功能,但这几乎是在线示例的复制和粘贴 - 我无法弄清楚出了什么问题..

最佳答案

用 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;
}

关于java - AppiumDriver 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24769181/

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