1.现在有两个独立的测试用例 SkipTest 和 MoveScreenTest;我使用 testng.xml 单独执行每个用例都没有问题

2.如果在 testng.xml 中同时写入这 2 个测试用例,执行的时候就会报错**

info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Requested a new session but one was in progress)","origValue":"Requested a new session but one was in progress"},"sessionId":"1d3cac22-fa5a-40e9-a86f-4b8d7eebef64"}

找到原因是由于 2 个 driver 同时启动导致的这个问题,在第二个执行的测试用例中把 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) 去掉;改为 driver.startActivity("com.qshealthcare.qshc", "com.qshealthcare.qshc.ui.activity.WelcomeActivity");再次执行,结果就是 APP 能启动起来但是哪个用例都不会执行。哪位大神能告诉我这个到底怎么解决呢?

3.下面贴出代码和报错信息

SkipTest

@Test
public class SkipTest{
    private AndroidDriver driver;  
    @BeforeSuite
    public void setUp() throws Exception {     
        driver.startActivity("com.qshealthcare.qshc", "com.qshealthcare.qshc.ui.activity.WelcomeActivity");
    }
    @AfterSuite
    public void tearDown() throws Exception{
        System.out.println("AfterSuite");
    }
    @Test
    public void swipe() throws InterruptedException {

        Set<String> contextNames = driver.getContextHandles();
        for (String contextName : contextNames) {
            System.out.println(contextName);
            if (contextName.contains("WEBVIEW")){
                driver=(AndroidDriver) driver.context(contextName);
            }else
            {
                System.out.println("no WEBVIEW");
            }
        }
        Thread.sleep(3000);
        for(int i=0;i<5;i++)
            {
            String demo="糖尿病患者可以有性生活吗?";
            System.out.println(i);
            WebElement result=driver.findElementByXPath("//android.widget.ListView/android.widget.RelativeLayout[contains(@index,3)]/android.widget.RelativeLayout[contains(@index,1)]/android.widget.RelativeLayout[contains(@index,1)]/android.widget.TextView[contains(@index,0)]");
            String resulttext=result.getAttribute("text");
            System.out.println( resulttext);
            if(!demo.equals(resulttext)){
                driver.swipe(702,1136,0,234,500);
                System.out.println("没找到");
                continue;
                }
            else{
                System.out.println("找到了");
                result.click();
                break;
                }
            }   
        System.out.println("push");
        Thread.sleep(5000);
    }
}



**MoveScreenTest**

public class MoveScreenTest{
    private AndroidDriver driver;  
    @BeforeSuite  
    public void setUp() throws Exception {  
        //set up appium  
        File classpathRoot = new File(System.getProperty("user.dir"));  
        //获取用户当前的工作目录
        File appDir = new File(classpathRoot, "apps");  
        File app = new File(appDir, "QSHealthMD.apk");  
        //File app = new File("D:\\QSHealthMD.apk")
        //或者可获取当前win系统中某路径下的apk
        DesiredCapabilities capabilities = new DesiredCapabilities();  
        //告诉appium启动的是浏览器还是移动设备,设备的名称、类型、版本等
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");  
        //定义移动浏览器的名称
        capabilities.setCapability("platformName", "Android");  
        capabilities.setCapability("deviceName","4d000e1544325027");  
        capabilities.setCapability("platformVersion", "5.0");
        //capabilities.setCapability("app", app.getAbsolutePath());  
        //安装绝对路径下的app到移动设备中
        capabilities.setCapability("appPackage", "com.qshealthcare.qshc");  
        capabilities.setCapability("appActivity", "com.qshealthcare.qshc.ui.activity.WelcomeActivity");  
        //获取app的首页,可通过appipum工具获取
        //capabilities.setCapability("unicodeKeyboard","True");
        //支持中文输入法
        //capabilities.setCapability("restKeyboard","True")
        //重置输入法为系统默认
        //capabilities.setCapability("noSign","True") 
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);  
    }

    @AfterSuite
    public void tearDown() throws Exception{
        //driver.quit();
        Thread.sleep(3000);
    }

    @Test
    public void lockscreen() throws InterruptedException {

        //获取当前的混合应用类型,判断如果是webview就跳转进入webview
        Set<String> contextNames = driver.getContextHandles();
        for (String contextName : contextNames) {
            System.out.println(contextName);
            if (contextName.contains("WEBVIEW")){
                //driver.context(contextName);
                driver=(AndroidDriver) driver.context(contextName);
                //AndroidDriver adriver =(AndroidDriver) driver.context("NATIVE_APP");
            }else
            {
                System.out.println("no WEBVIEW");
            }
        }           
        Thread.sleep(20000);

        int width=driver.manage().window().getSize().width;
        int height=driver.manage().window().getSize().height;

        //指定一个滑动的x和y坐标
        driver.swipe(width*4/5, height/2, width/5, height/2, 1000);
        Thread.sleep(2000); 
        WebElement loginBtn=driver.findElementByName("社区");
        loginBtn.click();
        System.out.println("success");  
        Thread.sleep(5000);
    }
}

XML


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test" verbose="10" >
  <test name="testcase1" >
    <classes>
        <class name="MoveScreenTest"/>
        <class name="SkipTest"/>
    </classes>
  </test>
</suite>   

appium 服务器没有报任何错误,eclipse 的控制台报错内容为
java.lang.NullPointerException
at SkipTest.setUp(SkipTest.java:21)
21 行的代码为 driver.startActivity("com.qshealthcare.qshc", "com.qshealthcare.qshc.ui.activity.WelcomeActivity");
我就想实现一个用 xml 执行多个测试用例,请大神们帮帮忙,多谢!


↙↙↙阅读原文可查看相关链接,并与作者交流