# 设置 capabilitiescaps = {}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)
# 点击 Viewsdriver.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']"#打印 toastXPathprint(driver.find_element_by_xpath(toastXPath))#打印 toastXPath 获取的 textprint(driver.find_element_by_xpath(toastXPath).text)
@BeforeAllpublic 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);}
@Testpublic 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());}
评论