实现时参考了本站另一个帖子 Toast 消息验证真的可以吗?[已测试验证,可以找到了]。
本帖是直接贴上我自己实现此功能的完整代码,如果想深入了解此功能,请移步到前文提到的帖子。
本人写的使用脚本实现捕获 Android Toast 内容的帖子:Appium+UiAutomator2 通过脚本验证 Toast 消息
使用 UiAutomator2:
OK,接下来就是上完整代码:
package com.example.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.example.test.util.Constants;
import io.appium.java_client.android.AndroidDriver;
public class ToastValidateTest {
private static final Logger LOGGER= Logger.getLogger(ToastValidateTest.class);
private AndroidDriver<WebElement> driver;
private WebDriverWait wait;
@Before
public void setUp() throws MalformedURLException {
LOGGER.debug("setUp...");
File classPathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classPathRoot, "apps");
File app = new File(appDir, Constants.test_app);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android");
capabilities.setCapability(CapabilityType.VERSION, Constants.androidVersion);
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "com.example.test");
capabilities.setCapability("app-activity", ".activity.SplashActivity");
capabilities.setCapability("noReset", true);
capabilities.setCapability("automationName", "UiAutomator2");
driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
wait = new WebDriverWait(driver, 10);
}
@After
public void tearDown() {
LOGGER.debug("tearDown...");
if (null!= driver) {
driver.quit();
}
}
private boolean isElementFoundByText(String key, AndroidDriver<WebElement>driver) {
try {
@SuppressWarnings("unused")
WebElement target= driver.findElementByXPath("//*[contains(@text, '"+ key+ " ')]");
return true;
} catch (Exception e) {
return false;
}
}
@Test
public void testValidateToast() throws InterruptedException {
Thread.sleep(3000);
WebElement loginBtn= driver.findElementById("btn_login");
Assert.assertNotNull(loginBtn);
do {
loginBtn.click();
} while (isElementFoundByText("empty", driver));
WebElement target= wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver arg0) {
return driver.findElementByXPath("//*[contains(@text, 'empty')]");
}
});
String toastString= target.getAttribute("text");
// 方便观察输出,多次重复输出获取到的Toast内容
LOGGER.debug("Toast: "+ toastString);
LOGGER.debug("Toast: "+ toastString);
LOGGER.debug("Toast: "+ toastString);
LOGGER.debug("Toast: "+ toastString);
LOGGER.debug("Toast: "+ toastString);
LOGGER.debug("Toast: "+ toastString);
LOGGER.debug("Toast: "+ toastString);
}
}
测试在控制台的输出:
应用页面截图:
从上两图的对比中可以看到,控制台输出的 Toast 内容与应用页面上显示的 Toast 一致,说明测试确认捕获到了 Toast 的内容。