Appium 求助:使用 TestNG 运行脚本出现下面的错误,有没有人遇到过?

杨秋强 · 2014年09月05日 · 最后由 杨秋强 回复于 2014年09月05日 · 2232 次阅读

代码:

package com.incito.com.incito.appiumdemo;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import io.appium.java_client.AppiumDriver;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class AndroidAddContactsTest {
    private AppiumDriver driver;

  @BeforeMethod
  public void beforeMethod()throws Exception {
      DesiredCapabilities capabilities = new DesiredCapabilities();
      capabilities.setCapability(CapabilityType.BROWSER_NAME, "");        
      capabilities.setCapability("platformName", "Android");        
      capabilities.setCapability("platformVersion", "4.2");
      capabilities.setCapability("deviceName", "android");        
      capabilities.setCapability("appPackage", "com.android.contacts");
      capabilities.setCapability("appActivity", "com.android.contacts.activities.PeopleActivity");
      driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  }

  @AfterMethod
  public void afterMethod() throws Exception {
      driver.quit();
  }

  @Test
  public void addContact(){
    driver.findElement(By.id("com.android.contacts:id/menu_add_contact")).click();     
      WebElement el = driver.findElement(By.name("Phone contact"));
      el.click();        
      List<WebElement> textFieldsList = driver.findElements(By.name("Name"));
      textFieldsList.get(0).sendKeys("yangqiuqiang");          
      List<WebElement> textFieldsList1 = driver.findElements(By.name("Phone"));        
      textFieldsList1.get(1).sendKeys("18872573204");
      List<WebElement> textFieldsList2 = driver.findElements(By.name("Email"));        
      textFieldsList2.get(1).sendKeys("stephenwang@gmail.com");
      driver.findElement(By.name("Done")).click();
  }
}

错误:

org.testng.TestNGException: java.lang.NullPointerException
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:341)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.NullPointerException
    at org.testng.xml.TestNGContentHandler.resolveEntity(TestNGContentHandler.java:97)
    at com.sun.org.apache.xerces.internal.util.EntityResolverWrapper.resolveEntity(EntityResolverWrapper.java:110)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.resolveEntityAsPerStax(XMLEntityManager.java:988)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1154)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1050)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:964)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
    at org.testng.xml.Parser.parse(Parser.java:172)
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:311)
    ... 3 more

TestNG 插件 版本 6.8.8
Selenium 版本 2.42.2

共收到 6 条回复 时间 点赞

代码在 JUnit 下运行正确,移植到 TestNG 下 就报错了

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>WT98601</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_11"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/arrow-0.1.0.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/commons-lang3-3.3.2.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/gson-2.2.2.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/hamcrest-core-1.3.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/java-client-1.6.2.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/log4j-1.2.16.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/selenium-server-standalone-2.42.2.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

是不是 xml 的问题,你把你 run 的那个 xml 贴出来看看

#3 楼 @cy_suncheng 创建工程时没有 xml

#4 楼 @sas 你用的不是 TestNG 么? 没有 xml 那你怎么 run 的?

org.testng.TestNGException: java.lang.NullPointerException 没有设置 xml 模板,工程右键属性,testNG, template 值需要设置一下。

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