Assertions

To verify toast message displays on screen:

Create a reusable function using presenceOfElementLocated wait method and XPath

helper.java
  public static void handleToastMessage(String args) {
        final WebDriverWait wait = new WebDriverWait(driver, 10);
        assertNotNull(wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='" + args + "']"))));
    }

To verify toast message displays on screen, import above function from helper.java class

test.java

$ helper.handleToastMessage(args);

To verify text displayed on screen:

Create a reusable function using findElements method and XPath using android.widget.TextView

helper.java
    public static boolean verifyTextExist(String args) {
        if (driver instanceof AndroidDriver) {
            Boolean isElementExist = driver.findElements(By.xpath("//*[@text='" + args + "']")).size() != 0;
            Assert.assertTrue(isElementExist, "Expected Text not found on screen:  " + args);
        } else {
            Boolean isElementExist = driver.findElements(By.xpath("//XCUIElementTypeOther[@name='" + args + "']"))
                    .size() != 0;
            Assert.assertTrue(isElementExist, "Expected Text not found on screen:  " + args);
        }
        return false;
    }

To verify text displayed on screen, import above function from helper.java class

test.java

$ helper.verifyTextExist("Appium");

To verify text not displayed on screen:

Create a reusable function using findElements method and XPath using android.widget.TextView

helper.java
   public static void verifyTextNotExist(String args) throws Exception {
        List<WebElement> elements = driver.findElements(By.xpath("//android.widget.TextView[@text='" + args + "']"));

        boolean isElementExist = elements.isEmpty();
        if (!isElementExist) {
            throw new Exception("Targeted Element should not display, But found on screen --- " + args);
        }
    }

To verify text not displayed on screen, import above function from helper.java class

test.java

$ helper.verifyTextNotExist("Selenium");

Last updated