Appium java+testNg 批量运行多个 class,运行时每个类都运行后立马错误.

roc · 2018年04月25日 · 最后由 一条虫 回复于 2019年01月08日 · 2710 次阅读

java+testNg 批量运行多个 class,

大部分的 class 只有一个@Test,只有一两个有多个@Test.
每个 class 都有单独的 seesion 启动配置.但都是一样的.

每个 class 单独运行时都正常.但是批量运行时.每个 class 运行刚开始就会出错.且错误相同

org.openqa.selenium.NoSuchSessionException: A session is either terminated or not started (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8 milliseconds
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'KMNM-PC', ip: '169.254.48.218', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.android.ayplatform.release, deviceScreenSize=1080x2160, networkConnectionEnabled=true, noReset=true, noSign=true, warnings={}, databaseEnabled=false, deviceName=MKJNW17C20005057, platform=LINUX, deviceUDID=MKJNW17C20005057, appActivity=com.android.ayplatform.activity.WelcomeActivity, desired={appPackage=com.android.ayplatform.release, appActivity=com.android.ayplatform.activity.WelcomeActivity, noReset=true, noSign=true, platformVersion=8.0, browserName=, sessionOverride=true, platformName=Android, deviceName=MKJNW17C20005057}, platformVersion=8.0.0, webStorageEnabled=false, locationContextEnabled=false, takesScreenshot=true, browserName=, javascriptEnabled=true, deviceModel=LLD-AL00, sessionOverride=true, platformName=Android, deviceManufacturer=HUAWEI}]
Session ID: 951e201a-ecc5-445b-a6b4-e7d8615095d5
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:235)
org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:500)
com.zhangyiwen.case3_1_1.tearDown(case3_1_1.java:92)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:220)
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

这是我的 testng.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<suite name="testsuit" parallel="true">
    <test name="test" preserver-order="true">
        <classes>

            <class name="com.readyData.createNewWork" />
            <class name="com.luopeng.case2_1_67" />
            <class name="com.luopeng.case3_3" />
            <class name="com.zhangzongchao.case2_2_1" />
            <class name="com.zhangzongchao.case2_2_2" />
            <class name="com.zhangyiwen.case3_1_1" />
            <class name="com.zhangyiwen.case3_2_1" />


        </classes>
        <listeners>
            <listener class-name="org.uncommons.reportng.HTMLReporter" />
            <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
        </listeners>

    </test> <!-- Test -->
</suite> <!-- Suite -->

每个 class 的结构都是这样:



public class case2_2_1 extends TestInfo {
    private static AppiumDriver driver;

    @BeforeClass
    public void setup() throws Exception {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(CapabilityType.BROWSER_NAME, "");
        cap.setCapability("platformName", Platform);
        cap.setCapability("deviceName", PhoneCode);
        cap.setCapability("platformVersion", Version);
        cap.setCapability("appPackage", ApkPackage);
        cap.setCapability("appActivity", ApkActivity);
        cap.setCapability("sessionOverride", true);
        cap.setCapability("noReset", true);
        cap.setCapability("noSign", true);

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

    @Test // 工作台切换
    public static void case2_2_1() throws InterruptedException {

        Thread.sleep(3000);
        driver.findElementByXPath(MainPage_Bar_QiLiao_Xpath).click();/

        Thread.sleep(3000);
        driver.findElementByXPath(MainPage_Bar_DashBoard_Xpath).click();

        Thread.sleep(3000);
        driver.findElementByXPath(MainPage_Bar_App_Xpath).click();

        Thread.sleep(3000);
        driver.findElementByXPath(MainPage_Bar_Discovery_Xpath).click();

        Thread.sleep(3000);
        driver.findElementByXPath(MainPage_Bar_Work_Xpath).click();

    }

    @AfterClass
    public void tearDown() throws Exception {
        AppBase.textPrint("test end!");
        driver.quit();

    }

或多个@Test

我想出错应该可能是每个 class 都自己有创建一个 session,批量在一起跑就出问题了.

那么如果将多个用例公用一个 session,该怎么改呢?

或者是在 xml 文件里配置?

还是有什么方法让多个 seesion 批量运行不出错呢?

共收到 1 条回复 时间 点赞

我也遇到一样的问题了,楼主解决了吗

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