在同一设备上并行运行的Appium会话

时间:2019-03-28 14:12:08

标签: java testng appium appium-android

我正在尝试在两个不同的设备上并行运行两个不同的测试用例。我正在按照以下步骤运行测试用例:

1)使用以下命令打开两个appium服务器实例

appium -p 4723 -bp 4724 --chromedriver-port 4725 -U "ZY3223QGDP"
appium -p 4726 -bp 4727 --chromedriver-port 4728 -U "ZY223XJFNS"

我已遵循以下appium准则来启动appium服务器:   https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/parallel-tests.md

2)我创建了两个testng类来容纳两个测试用例。下面是类和tetng xml文件的代码。

3)我在两个测试用例中都打印了appium会话。为其打印不同的值。甚至appium日志也表明这两个appium驱动程序都在与各自的服务器进行交互。

在两个测试中,我使用的是不同的应用程序,但是只有一个测试用例通过,而一个失败。在这两个测试案例中,我都截取了屏幕快照,这些屏幕快照给出了相同设备的屏幕快照。

测试1

package com.qklab.register;

import java.io.File;
import java.util.HashMap;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.testng.annotations.Test;
import com.qk.automation.framework.AppiumManagerTestng;
import io.appium.java_client.AppiumDriver;

public class DeviceOneAppOne {

    @Test
    public void test1(HashMap<String,Object> testDetail) throws Exception {

        HashMap<String, String> deviceDetails = new HashMap<String, String>();
        deviceDetails.put("app_activity","com.hdfc.retail.netbanking.HDFCBank");
        deviceDetails.put("app_package","com.snapwork.hdfc");
        deviceDetails.put("appium_port","4726");
        deviceDetails.put("appium_ip","0.0.0.0");
        deviceDetails.put("udid","ZY223XJFNS");
        deviceDetails.put("device_name","moto black");
        deviceDetails.put("platform_version","8.1.0");
        deviceDetails.put("platform_name","Android");

        AppiumManagerTestng appiumManager = new AppiumManagerTestng();
        AppiumDriver appiumDriver = appiumManager.getAppiumDriverForTestng(deviceDetails);

            try {

                System.out.println("got session id : " + appiumDriver.getSessionId());

                //Take screenshot
                File srcFile=appiumDriver.getScreenshotAs(OutputType.FILE);
                File targetFile=new File("ZY223XJFNS" +".jpg");
                System.out.println(targetFile.getAbsolutePath());
                FileUtils.copyFile(srcFile,targetFile);

                appiumDriver.findElement(By.xpath("//*[@*[contains(.,'SKIP INTRODUCTION')]]")).click();

            }catch(Exception ex) {
                throw ex;
            }       
    }

}

2)测试2

package com.qklab.register;

import java.io.File;
import java.util.HashMap;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.testng.annotations.Test;
import com.qk.automation.framework.AppiumManagerTestng;
import io.appium.java_client.AppiumDriver;


public class DeviceTwoAppTwo {

    @Test
    public void test2(HashMap<String,Object> testDetail) throws Exception {
        HashMap<String, String> deviceDetails = new HashMap<String, String>();
        deviceDetails.put("app_activity","com.atomyes.MainActivity");
        deviceDetails.put("app_package","com.atomyes");
        deviceDetails.put("appium_port","4723");
        deviceDetails.put("appium_ip","0.0.0.0");
        deviceDetails.put("udid","ZY3223QGDP");
        deviceDetails.put("device_name","moto silver");
        deviceDetails.put("platform_version","7.0");
        deviceDetails.put("platform_name","Android");

        AppiumManagerTestng appiumManager = new AppiumManagerTestng();
        AppiumDriver appiumDriver = appiumManager.getAppiumDriverForTestng(deviceDetails);

        try {
            //Take screenshot
            File srcFile=appiumDriver.getScreenshotAs(OutputType.FILE);
            File targetFile=new File("ZY3223QGDP" +".jpg");
            FileUtils.copyFile(srcFile,targetFile);

            appiumDriver.findElement(By.xpath("//*[@text='Skip To Login']")).click();
        }catch(Exception ex) {
            Thread.sleep(10000);
            throw ex;
        }

    }


}

3)AppiumDriver生成器

package com.qk.automation.framework;

import java.net.URL;
import java.util.HashMap;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;

public class AppiumManagerTestng {

    public AppiumDriver getAppiumDriverForTestng(HashMap<String, String> deviceDetails) throws Exception {
        AppiumDriver driverForTestng = null;
        String app_activity = deviceDetails.get("app_activity");
        String app_package = deviceDetails.get("app_package");
        String appium_port = deviceDetails.get("appium_port");
        String appium_ip = deviceDetails.get("appium_ip");
        String udid = deviceDetails.get("udid");
        String device_name = deviceDetails.get("device_name");
        String platform_version = deviceDetails.get("platform_version");
        String platform_name = deviceDetails.get("platform_name");
            try
            {
                DesiredCapabilities dc = new DesiredCapabilities();
                dc.setCapability(MobileCapabilityType.DEVICE_NAME, device_name);
                dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, platform_version);
                dc.setCapability(MobileCapabilityType.PLATFORM_NAME, platform_name);
                dc.setCapability(MobileCapabilityType.UDID, udid);
                dc.setCapability(MobileCapabilityType.NO_RESET, false);
                //dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
                dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
                dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, app_package);
                dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, app_activity);
                driverForTestng = new AppiumDriver(new URL("http://" + appium_ip + ":" + appium_port + "/wd/hub"),dc);  
            }

            catch(Exception objDriverException){
                objDriverException.printStackTrace();

            }

        return driverForTestng;
    }

}

4)Testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
  <test name="Test1">
    <classes>
      <class name="com.qklab.register.DeviceOneAppOne"> 
       <methods>
        <include name="test1" />
      </methods>
      </class>
    </classes>
  </test>
   <test name="Test2">
    <classes>
      <class name="com.qklab.register.DeviceTwoAppTwo"> 
       <methods>
        <include name="test2" />
      </methods>
      </class>
    </classes>
  </test> 
</suite>

以上代码应彼此独立地运行两个测试用例。但是,这没有发生。任何帮助将不胜感激。

0 个答案:

没有答案