java - Selenium WebDriver - Appium - Android 自动化期间出现 NoClassDefFound 错误?

标签 java android selenium-webdriver appium

我刚刚开始编写 Java 测试,以使用 Appium 和 JUnit 自动化 native Android 应用程序。即使所需的 jar 位于 Eclipse GUI 中的类路径上,也会出现 NoClassDefFound 错误。我已将范围缩小到这一行:

driver = new RemoteWebDriver(new URL("appium typical server url here"), capabilities);

以下 jar 位于 Eclipse 的库下的 Java 构建路径中:java-client-1.5.0.jarselenium-java-2.42.2.jarselenium-java-2.42.2-srcs.jar,以及 JUnit

这是我的完整代码:

package com.chinmay.automation;

import java.io.File;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

    public class ChessTest {

    private WebDriver driver=null;

    @Before

    public void setUp() throws Exception{

        File appDir= new File("/Users/csathe/Desktop/Java");
        File app = new File(appDir, "ChessFree.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability(CapabilityType.VERSION, "4.3");
        capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
        capabilities.setCapability("device", "android");
        capabilities.setCapability("app-package", "uk.co.aifactory.chessfree");
        capabilities.setCapability("app-activity", "ChessFreeActivity");
        capabilities.setCapability("app", app.getAbsolutePath());

        driver= new RemoteWebDriver(new URL("//appium typical server url here"), capabilities);


    }

    @Test

    public void testFirst(){

        // do something

    }
    @After

    public void tearDown(){

        // driver.quit();
       }

    }

如果我使用 JUnit 运行它并注释掉该行,它运行良好(但是什么也不做)。

带有以下行的堆栈跟踪:

java.lang.NoClassDefFoundError: com/google/common/base/Function
at com.chinmay.automation.ChessTest.setUp(ChessTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at   
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 25 more

在 JUnit 之外运行这一类会导致相同的错误。

最佳答案

此错误清楚地表明构建路径中缺少任何 jar 依赖项。

请将此处可用的所有 jar 添加到类路径中:

http://selenium-release.storage.googleapis.com/2.42/selenium-java-2.42.2.zip

或者

从此处下载 Selenium-server-standalone.jar 并添加到类路径:

http://selenium-release.storage.googleapis.com/2.42/selenium-server-standalone-2.42.2.jar

关于java - Selenium WebDriver - Appium - Android 自动化期间出现 NoClassDefFound 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25047437/

相关文章:

java - 如何从用户请求中获取用户代理?

android - ContentProvider 路径遍历漏洞

java - 将海报路径从一项 Activity 传递到另一项 Activity

java - 处理警告 : [rawtypes] found raw type: JList in Netbeans (changing the object types to meet the new Java 7 standards)

java - 如何更改 JComboBox 弹出部分的边框?

java - Selenium 3 InternetExplorer 驱动程序设置

python - 如何在 Selenium Webdriver 2 Python 中获取当前 URL?

javascript - 在 Windows 上使用 PhantomJS 时 WebdriverJS 测试挂起

java - 更改数据库中用户的密码

android - Jsoup - 从元素中提取 html