Appium appium+maven+tesyng 启动时,出现了 java.lang.NoClassDefFoundError 问题

roc · 2018年03月27日 · 最后由 lofbat 回复于 2018年03月28日 · 2268 次阅读

mave+testng 运行时出现这个报错:

AILED CONFIGURATION: @BeforeClass setup
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableSet
    at org.openqa.selenium.remote.ErrorCodes.<clinit>(ErrorCodes.java:215)
    at io.appium.java_client.AppiumDriver.<clinit>(AppiumDriver.java:95)
    at com.testCase.testcase1.setup(testcase1.java:74)
    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:86)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:178)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableSet
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 27 more

SKIPPED CONFIGURATION: @AfterClass tearDown
SKIPPED: plus

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

以下是用例内容

package com.testCase;

import org.testng.annotations.BeforeClass;

/**
 * com.testcase.demo
 * appiumTest
 * case2.java
 * 
 * Copyright 2018 QYcloud Product test group
 *
 * Created on 2018年3月16日 下午3:28:05
 *
 * Created by LuoPeng
 */

import com.qycloud.AppTest.ActivityBase;
import com.qycloud.AppTest.AppBase;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class testcase1 extends ActivityBase {

    // 配合android键盘输入 driver 切换为 AndroidDriver
    private static AppiumDriver driver;
    //private static AndroidDriver driver;

    // test case name
    String testTitle = "case2";

    // all waitFor MeThod time set (30s/60s)
    int waitTime = 30;

    // 登录方法账号信息配置
    String Account = "admin_pretest";
    String Password = "11111111";

    // 切换账号名
    String ForAccountName = "李根";

    @BeforeClass
    public void setup() throws Exception {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(CapabilityType.BROWSER_NAME, "");
        cap.setCapability("platformName", "Android");
        cap.setCapability("deviceName", "MKJNW17C20005057"); 
        cap.setCapability("platformVersion", "8.0");
        // 将上面获取到的包名和Activity名设置为值
        cap.setCapability("appPackage", "com.android.ayplatform");
        cap.setCapability("appActivity", "com.android.ayplatform.activity.WelcomeActivity");
        // A new session could not be created
        // cap.setCapability("appWaitActivity","com.android.ayplatform.activity.WelcomeActivity");
        // 每次启动时覆盖session,否则第二次后运行会报错不能新建session
        cap.setCapability("sessionOverride", true);
        // 解决每次启动APP需要确认权限问题--即不重复安装
        cap.setCapability("noReset", true);
        cap.setCapability("noSign", true);
        /*        
         中文输入,隐藏键盘启动
        capabilities.setCapability("unicodeKeyboard", "True");
        capabilities.setCapability("resetKeyboard", "True");
        */
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }

    /*
     * release 版本 登录-》切换账号-》新建流程-》提交
     */
    @Test
    public void plus() throws InterruptedException {

        login();
        changeAccount();

        waitForXpath("待办工作", MainPage_Tag_Already_Xpath);
        ClickXpath(MainPage_NewWork_Id);

        ClickXpath("//android.widget.TextView[@text='想喝花雕么']");

        // 等待流程标题控件
        waitForId("com.android.ayplatform.release:id/title");

        // 字段输入
        driver.findElementById("com.android.ayplatform.release:id/view_stringui_valueEdt").clear();
        driver.findElementById("com.android.ayplatform.release:id/view_stringui_valueEdt").sendKeys("auto test");

        // 数字输入
        driver.findElementByXPath("//android.widget.LinearLayout[3]").clear();
        driver.findElementByXPath("//android.widget.LinearLayout[3]").sendKeys("123123");

        // 点击添加人员
        ClickId("com.android.ayplatform.release:id/org_add_user_imageView");

        // 人员组织搜索页面-搜索
        ClickId("com.android.ayplatform.release:id/workbench_head_edit");

        // 键盘事件--成员目标 test-1
        // 如果调试失败,建议使用坐标点击替代
        /*
        // t
        driver.pressKeyCode(48);
        // e
        driver.pressKeyCode(33);
        // s
        driver.pressKeyCode(47);
        // t
        driver.pressKeyCode(48);
        // 1
        driver.pressKeyCode(8);
        // 搜索
        driver.pressKeyCode(66);
        */
        // 点击人名--只有一个结果
        ClickId("com.android.ayplatform.release:id/item_orgstructure_colleague_name");

        // 点击提交
        ClickId("com.android.ayplatform.release:id/org_search_display_submit");

        // 提交
        ClickId(WorkFlow_Send_Id);
    }

    @AfterClass
    public void tearDown() throws Exception {

        driver.navigate().back();
        driver.navigate().back();
        AppBase.textPrint("test end!");
        driver.quit();

    }

    public void CheckXpath(String Xpath, String Text) {
        try {
            AppBase.textPrint("Check The Text For Xpath! ->" +Xpath);
            assertEquals(driver.findElementByXPath(Xpath).getText(), Text);
            AppBase.textPrint("Check Success!");
        } catch (Error e) {
            AppBase.textPrint("Check Fail!");
            e.printStackTrace();
            AppBase.snapshot();
        }

    }

    public void CheckId(String Id, String Text) {
        try {
            AppBase.textPrint("check The Text For Id ->" + Id);
            assertEquals(driver.findElementById(Id).getText(), Text);
            AppBase.textPrint("Check Success!");
        } catch (Error e) {
            AppBase.textPrint("Check Fail!");
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

    public void ClickXpath(String Xpath) {
        try {
            AppBase.textPrint("Click The Element For Xpath ->" + Xpath);
            driver.findElementByXPath(Xpath).click();
            AppBase.textPrint("Click Act Success!");
        } catch (Error e) {
            AppBase.textPrint("Click Fail");
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

    public void ClickId(String Id) {
        try {
            AppBase.textPrint("Click The Element For Id -> " +Id);
            driver.findElementById(Id).click();
            AppBase.textPrint("Click Act Success");
        } catch (Error e) {
            AppBase.textPrint("Click Fail");
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

    public void waitForId(String Text, String Id) throws InterruptedException{
        for (int second = 0;; second++) {
            if (second >= waitTime)// 60秒市场区间
                fail("timeout->" + second);
            try {
                if (Text.equals(driver.findElementById(Id)))
                    break;
            } catch (Error e) {
                AppBase.textPrint("finding Element Fail");
                e.printStackTrace();
                AppBase.snapshot();
            }
            Thread.sleep(1000);// 每隔1秒执行查找

        }
    }


    public void waitForId(String Id) throws InterruptedException {
        for (int second = 0;; second++) {
            if (second >= waitTime)
                fail("timeout->" + second);
            try {
                if (Id.equals(driver.findElement(By.id(Id))))
                    ;
            } catch (Error e) {
                AppBase.textPrint("finding Element Fail");
                e.printStackTrace();
                AppBase.snapshot();
            }
            Thread.sleep(1000);
        }
    }

    public void waitForXpath(String Text, String Xpath) throws InterruptedException {
        for (int second = 0;; second++) {
            if (second >= waitTime)
                fail("timeout->" + second);
            try {
                if (Text.equals(driver.findElementByXPath(Xpath)))
                    break;
            } catch (Error e) {
                AppBase.textPrint("finding Element Fail");
                e.printStackTrace();
                AppBase.snapshot();
            }
            Thread.sleep(1000);
        }
    }


    public void waitForXpath(String Xpath) {
        for (int second = 0;; second++) {
            if (second >= waitTime)
                fail("timeout->" + second);
            try {
                if (Xpath.equals(driver.findElementByXPath(Xpath)))
                    break;
            } catch (Error e) {
                AppBase.textPrint("finding Element Fail");
                e.printStackTrace();
                AppBase.snapshot();
            }
        }
    }

    public void iputForId(String Id, String Keys) {
        try{
            driver.findElementById(Id).sendKeys(Keys);
            AppBase.textPrint("input Keys:  "+Keys);
        } catch (Error e) {
            AppBase.textPrint("input act Fail!");
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

    public void inptForXpath(String Xpath, String Keys) {
        try{
            driver.findElementByXPath(Xpath).sendKeys(Keys);
            AppBase.textPrint("input Keys:  "+Keys);
        } catch (Error e) {
            AppBase.textPrint("input act Fail!");
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

    public void changeAccount() {
        AppBase.textPrint("Account Change Act start!");

        try {
            ClickXpath(MainPage_preData_Xpath);
            waitForXpath("账号与安全", MyInfo_Tag_AccountSecurity_Xpath);

            ClickXpath(MyInfo_Tag_AccountSecurity_Xpath);
            waitForId(PreData_AS_AccountChange_Id);

            ClickId(PreData_AS_AccountChange_Id);
            // 切换账号
            waitForXpath(ForAccountName, "//android.widget.TextView[@text='" + ForAccountName + "']");
            ClickXpath("//android.widget.TextView[@text='" + ForAccountName + "']");
            // 回到个人信息页,关闭个人信息弹窗
            waitForXpath("账号与安全", MyInfo_Tag_AccountSecurity_Xpath);
            driver.navigate().back();
            AppBase.textPrint("Account Change Act Success!");
        } catch (InterruptedException e) {
            AppBase.textPrint("Account Change Act Fail!");
            // TODO Auto-generated catch block
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

    // 账号登录
    public void login() {
        AppBase.textPrint("login Act start!");
        // 用户名输入
        try {
            Thread.sleep(5000);

            driver.findElementById(LoginPage_Id).clear();
            Thread.sleep(2000);
            driver.findElementById(LoginPage_Id).sendKeys(Account);
            Thread.sleep(2000);
            // 密码输入
            driver.findElementById(LoginPage_Password).clear();
            Thread.sleep(2000);
            driver.findElementById(LoginPage_Password).sendKeys(Password);
            Thread.sleep(2000);
            // HTC U11 关闭输入法键盘
            driver.navigate().back();
            Thread.sleep(2000);
            // 登录
            driver.findElementById(LoginPage_LoginKey).click();
            Thread.sleep(2000);
            AppBase.textPrint("login Act Succesee!");
        } catch (InterruptedException e) {
            AppBase.textPrint("login Act Fail!");
            // TODO Auto-generated catch block
            e.printStackTrace();
            AppBase.snapshot();
        }

    }

    public void swipXY(int X1, int Y1, int X2, int Y2) {
        try {
            Thread.sleep(1000);
            driver.swipe(X1, Y1, X2, Y2, 500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            AppBase.textPrint("screeen shot Fail!");
            e.printStackTrace();
            AppBase.snapshot();
        }
    }

}

是不是因为依赖出现问题呢??

以下是 pom.xml 的内容

<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.qycloud</groupId>
    <artifactId>AppTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>AppTest</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
        </dependency>
        <dependency>
            <groupId>org.uncommons</groupId>
            <artifactId>reportng</artifactId>
            <version>1.1.4</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-server-standalone</artifactId>
                <version>3.31.0</version>
            </dependency>
        </dependencies>



    </dependencyManagement>
</project>

百思不得其解.同样的用例在正常的工程内可以正常运行.

同时也不知道是哪个依赖会跟 google 搭上关系.?.

求解😂

共收到 5 条回复 时间 点赞

com.testCase.testcase1.setup(testcase1.java:74)
74 是哪一行代码呢?

roc #2 · 2018年03月28日 Author
sini 回复

是端口那行

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

Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableSet
看下这个包版本是否正确

roc #4 · 2018年03月28日 Author
lofbat 回复

我一共就依赖的这几个包...就是搞不清哪个会跟 google 搭上关系...

guava 和 json 是后添加的.不过依然报错.也是说某个 google 组件有 java.lang.NoClassDefFoundError 问题

roc 回复

应该是 guava 版本和需要的依赖版本不一致,找一个含有 ImmutableSet 类的版本导入

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册