如何解决我的自动化代码中的错误?

How can I work out the errors in my automation code?

我一直在观看大量与 Appium、Selenium 和 Android 应用程序自动化有关的 youtube 视频和教程。但是我仍然不确定我的代码有什么问题,我是编程新手所以我不知道如何破译所有这些。

这是我的代码:

package testcase;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;

public class OpenOfferUp {


@Test
public void OpensOfferUp() throws MalformedURLException
{
    File OfferUp = new File("C:/Users/boung/Desktop/offerupapk");
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability("deviceName", "Virtual Device");
    cap.setCapability("platformName", "android");
    cap.setCapability("app", OfferUp);
    cap.setCapability("appPackage", "com.offerup");
    cap.setCapability("activityName", "com.offerup.android.activities.SearchActivity");

    AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), cap);

}


}    

这是控制台中显示的内容

`FAILED: OpensOfferUp
org.openqa.selenium.json.JsonException: Unable to determine type from: C. Last 110 characters read: {
  "desiredCapabilities": {
    "activityName": "com.offerup.android.activities.SearchActivity",
    "app": C
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-HCHDEFK', ip: '10.0.0.83', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: driver.version: AndroidDriver
    at org.openqa.selenium.json.JsonInput.peek(JsonInput.java:122)
    at org.openqa.selenium.json.JsonTypeCoercer.lambda$null(JsonTypeCoercer.java:140)
    at org.openqa.selenium.json.JsonTypeCoercer.coerce(JsonTypeCoercer.java:126)
    at org.openqa.selenium.json.MapCoercer.lambda$null[=11=](MapCoercer.java:68)
    at java.util.stream.ReferencePipeline.accept(Unknown Source)
    at java.util.Iterator.forEachRemaining(Unknown Source)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.collect(Unknown Source)
    at org.openqa.selenium.json.MapCoercer.lambda$apply(MapCoercer.java:72)
    at org.openqa.selenium.json.JsonTypeCoercer.lambda$null(JsonTypeCoercer.java:145)
    at org.openqa.selenium.json.JsonTypeCoercer.coerce(JsonTypeCoercer.java:126)
    at org.openqa.selenium.json.JsonInput.read(JsonInput.java:280)
    at io.appium.java_client.remote.NewAppiumSessionPayload.getOss(NewAppiumSessionPayload.java:336)
    at io.appium.java_client.remote.NewAppiumSessionPayload.<init>(NewAppiumSessionPayload.java:172)
    at io.appium.java_client.remote.NewAppiumSessionPayload.create(NewAppiumSessionPayload.java:134)
    at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:175)
    at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:217)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:38)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:84)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:94)
    at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:95)
    at testcase.OpenOfferUp.OpensOfferUp(OpenOfferUp.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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

问题在于能力 app。您应该为您的 .apk 文件提供绝对本地路径或远程 http URL。但是你插入一个文件对象。

cap.setCapability("app", "C:/Users/boung/Desktop/offerupapk");

请注意,如果您的 webdriver hub 服务器在远程机器上,您必须先将 .apk 复制到那里