# 设置 capabilities
caps = {}
caps["platformName"] = "Android"
caps["appPackage"] = "io.appium.android.apis"
caps["appActivity"] = ".ApiDemos"
#必须使用uiautomator2框架
caps["automationName"] = "uiautomator2"
caps["deviceName"] = "hogwarts"
# 与Appium Server 建立连接
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
# 设置隐式等待
driver.implicitly_wait(5)
# 点击 Views
driver.find_element_by_accessibility_id("Views").click()
# 滑动页面
TouchAction(driver).press(380, 1150)\
.move_to(380, 150).release().perform()
# 点击 `Popup Menu` 项目
driver.find_element_by_xpath(
"//*[@content-desc='Popup Menu']").click()
# 点击 `Make a Popup`
driver.find_element_by_xpath(
"//*[@content-desc='Make a Popup!']").click()
# 点击 'Search'
driver.find_element_by_xpath("//*[contains(@text,'Search')]").click()
toastXPath = "//*[@class='android.widget.Toast']"
#打印 toastXPath
print(driver.find_element_by_xpath(toastXPath))
#打印 toastXPath 获取的 text
print(driver.find_element_by_xpath(toastXPath).text)
@BeforeAll
public static void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("appPackage", "io.appium.android.apis");
desiredCapabilities.setCapability("appActivity", ".ApiDemos");
desiredCapabilities.setCapability("automationName", "uiautomator2");
desiredCapabilities.setCapability("deviceName", "hogwarts");
URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void toastTest() {
//点击 Views
driver.findElement(MobileBy.AccessibilityId("Views")).click();
//滑动页面
TouchAction action = new TouchAction(driver);
PointOption pressPointOne = PointOption.point(380, 1150);
PointOption movePointOne = PointOption.point(380, 150);
action.press(pressPointOne).moveTo(movePointOne).release();
//点击 `Popup Menu` 项目
driver.findElement(By.xpath("//*[@content-desc='Popup Menu']")).click();
//点击 `Make a Popup`
driver.findElement(By.xpath("//*[@content-desc='Make a Popup!']")).click();
//点击 'Search'
driver.findElement(By.xpath("//*[contains(@text,'Search')]")).click();
By toastXPath = By.xpath("//*[@class='android.widget.Toast']");
//打印 toastXPath
System.out.println(driver.findElement(toastXPath));
//打印 toastXPath 获取的 text
System.out.println(driver.findElement(toastXPath).getText());
}
评论