如何使用测试概念进行应用自动化?



说基类是:

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());
}
}

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

嗨,您可以创建一个测试基类作为 BaseTest.java您将在其中初始化AndroidDriver和一个测试类作为Test.java这将扩展BaseTest.java并使用驱动程序执行驱动程序操作。

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

package com.edf.mobile.tests;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class BaseTest {
public AppiumDriver<?> driver;
//initialize your driver here
@BeforeTest
public void beforeTest(){
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());
try {
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}

测试.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());
}
}

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

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

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

最新更新