2017-04-19 98 views
0

我寫了一個包含一個按鈕的應用程序,它會顯示一個Toast消息。如何使用uiautomator2通過appium驗證吐司消息?

public void onClickShowToast(View view) { 
    Toast.makeText(getBaseContext(), "Change port successfully", Toast.LENGTH_LONG).show(); 
} 

然後,我用appium編寫一個測試用例來驗證它。

//APPIUM Version: 1.6.3; 
//JAVA: 1.8; 
//JAVA Client: 5.0.0; 
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2); 

driver.findElementByXPath("//*[contains(@text,'Button')]").click(); 
WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(@text, 'port')]"))) 

但是,結果是失敗的:

org.openqa.selenium.TimeoutException:預期條件失敗: 等待位於由元素的存在:By.xpath: // *包含(@Text, '口')

回答

0

您可以捕獲這樣的敬酒:

WebElement toastView = androidDriver.findElement(By.xpath("//android.widget.Toast[1]")); String text = toastView.getAttribute("name");

Here是我的解決方案。