使用Appium + TestNG + Eclipse在Android上运行自动测试

时间:2015-04-30 14:24:53

标签: android eclipse selenium testng appium

我是自动测试和Appium的初学者,我正在尝试测试(对于android)。

我下载了:

  1. Appium - 运行服务器

  2. Android模拟器(Android虚拟设备管理器)

  3. TestNG的

  4. 测试的目的是在联系人列表中添加联系人。

    有谁知道我错过了什么?

    测试:

    package com.appium;
    
    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.android.AndroidDriver;
    
    import java.io.File;
    import java.net.URL;
    import java.util.List;
    
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeSuite;
    
    public class One {
    
        private AppiumDriver driver;
    
        @BeforeSuite
        public void setUp() throws Exception {
            //driver.launchApp();
    
            // set up appium
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("deviceName", "Nexus5");
            capabilities.setCapability("platformV01ersion", "4.1.2");
            capabilities.setCapability("appPackage",
                "com.example.android.contactmanager");
            capabilities.setCapability("appActivity", ".ContactManager");
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                    capabilities);
        }
    
        @Test
        public void addContact() {
            WebElement el = driver.findElement(By.name("Add Contact"));
            el.click();
            List<WebElement> textFieldsList = driver
                    .findElementsByClassName("android.widget.EditText");
            textFieldsList.get(0).sendKeys("Some Name");
            textFieldsList.get(2).sendKeys("Some@example.com");
            driver.swipe(100, 500, 100, 100, 2);
            driver.findElementByName("Save").click();
        }
    
        @AfterSuite
        public void tearDown() throws Exception {
            driver.quit();
        }
    
    }
    

    EXCEPTION

    [TestNG] Running:
      C:\Users\nadav.miran\AppData\Local\Temp\testng-eclipse-451394169\testng-customsuite.xml
    
    FAILED CONFIGURATION: @BeforeSuite setUp
    java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
        at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:88)
        at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:62)
        at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:57)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153)
        at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:109)
        at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:39)
        at com.appium.One.setUp(One.java:37)
        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:84)
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:277)
        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)
    Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
        ... 25 more
    
    
    ===============================================
        Default test
        Tests run: 0, Failures: 0, Skips: 0
        Configuration Failures: 1, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 0, Failures: 0, Skips: 0
    Configuration Failures: 1, Skips: 1
    ===============================================
    
    [TestNG] Time taken by org.testng.reporters.XMLReporter@517ceb9b: 13 ms
    [TestNG] Time taken by org.testng.reporters.EmailableReporter2@37610393: 7 ms
    [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@54fba1ac: 0 ms
    [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@9e12aac: 34 ms
    [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
    [TestNG] Time taken by org.testng.reporters.jq.Main@61ed627d: 29 ms
    

    谢谢!

2 个答案:

答案 0 :(得分:1)

请将以下依赖项添加到您的项目中

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.2</version>
</dependency>

也请 从here下载Selenium-server-standalone.jar并添加到类路径。 有关详细信息,请参阅此link

答案 1 :(得分:1)

我收到了这个错误。

当我按下&#39;跑步时发生了Eclipse中的按钮,但运行TestNG测试套件的正确方法如下:

Right Click on testng.xml
Run as > TestNG Suite

我知道这是一个老问题,但我希望这有帮助