如何使用testng注释进行appium自动化?

时间:2018-05-28 05:36:55

标签: testng appium

Say Base class is:

package tutcheck;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class Base {

    public static void main(String[] args) throws MalformedURLException {
        // TODO Auto-generated method stub

        File f = new File("src");
        File fs=new File(f,"ApiDemos-debug.apk");

        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.56.101:5555");
        cap.setCapability(MobileCapabilityType.APP,fs.getAbsolutePath());
        AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
    }
}

和其他课程是:

package tutcheck;

import java.net.MalformedURLException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.interactions.touch.TouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class Gestures extends Base {
    public static void main(String[] args) throws MalformedURLException {

        AndroidDriver < AndroidElement > driver = Capabilities();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"Views\").instance(0))").click();
        //driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']").click();

        TouchAction t = new TouchAction(driver);
        t.tap(driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']")).perform();
        driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']").click();

        t.press(driver.findElementByXPath("//android.widget.TextView[@text='People Names']")).waitAction(Duration.ofSeconds(10)).release().perform();
        //t.press(driver.findElementByXPath("//android.widget.TextView[@text='People Names']")).waitAction(Duration.ofSeconds(10)).release().perform();
        System.out.println(driver.findElementByXPath("//android.widget.TextView[@text='Sample menu']").getText());

    }
}

所以现在我的问题是如何首先在gestures.java类中删除main方法,我如何在基类中使用testng注释,并使用其他类的testng注释调用其他类好?

1 个答案:

答案 0 :(得分:0)

您好,您可以创建一个测试基类作为 BaseTest.java ,您将 AndroidDriver 和测试类初始化为 Test.java < / strong>将扩展 BaseTest.java 并使用驱动程序执行驱动程序操作。

以下是BaseTest.java的示例代码:

package com.edf.mobile.tests;

import io.appium.java_client.TouchAction;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class AppiumTest extends BaseTest {
    @Test
    public void test(){
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElementsByXPath("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"Views\").instance(0))");
        //driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']").click();

        TouchAction t = new TouchAction(driver);
        t.tap(driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']")).perform();
        driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']").click();

        t.press(driver.findElementByXPath("//android.widget.TextView[@text='People Names']")).waitAction(Duration.ofSeconds(10)).release().perform();
        //t.press(driver.findElementByXPath("//android.widget.TextView[@text='People Names']")).waitAction(Duration.ofSeconds(10)).release().perform();
        System.out.println(driver.findElementByXPath("//android.widget.TextView[@text='Sample menu']").getText());

    }
}

Test.java类如下:

java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET
    at org.apache.http.impl.client.DefaultHttpClient.setDefaultHttpParams(DefaultHttpClient.java:175) ~[stormjar.jar:?]
    at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:158) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) ~[stormjar.jar:?]
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) ~[stormjar.jar:?]
    at com.trinity.sensors.LocationName.getPlaceName(LocationName.java:77) ~[stormjar.jar:?]
    at com.trinity.sensors.ItmsVehicleDataBolt.execute(ItmsVehicleDataBolt.java:126) ~[stormjar.jar:?]
    at backtype.storm.topology.BasicBoltExecutor.execute(BasicBoltExecutor.java:50) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.daemon.executor$fn__6713$tuple_action_fn__6715.invoke(executor.clj:684) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.daemon.executor$mk_task_receiver$fn__6636.invoke(executor.clj:431) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.disruptor$clojure_handler$reify__1319.onEvent(disruptor.clj:58) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:120) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:99) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.disruptor$consume_batch_when_available.invoke(disruptor.clj:80) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.daemon.executor$fn__6713$fn__6726$fn__6777.invoke(executor.clj:813) ~[storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at backtype.storm.util$async_loop$fn__544.invoke(util.clj:479) [storm-core-0.10.0.2.4.3.0-227.jar:0.10.0.2.4.3.0-227]
    at clojure.lang.AFn.run(AFn.java:22) [clojure-1.6.0.jar:?]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_77]
2018-05-29 09:21:27.164 b.s.d.executor [ERROR] 

注意:如果上面的代码不适合您,那么只需尝试将appium java-client版本,selenium版本更改为最新版本。由于大多数方法在当前版本中已弃用,因此您可能希望更新上述代码。

这应该让您了解如何使用testng注释,以及如何摆脱java main方法。

TestNG提供了更多的注释,您可以在官方的testng网站上进行探索。