Navya Chinnari
Navya Chinnari

Reputation: 21

How to automate a Toast message in Appium without taking screen shot?

I want to test a Toast message without taking screen shot. Is there any other way to automate the Toast message?

Upvotes: 0

Views: 2664

Answers (3)

Yavuz Yoldaş
Yavuz Yoldaş

Reputation: 881

This works excellent for me in python.

    control = False
  
    xmlFormat = self.driver.page_source
    if xmlFormat.find("your toast message") != -1:
     control = True
      
    self.assertEqual(True,control)

Upvotes: 0

Vinod
Vinod

Reputation: 976

Retrieval of toast messages is already supported for Android. Please look at the below release notes for Android.

https://github.com/appium/appium/releases/tag/v1.6.3

You need to use UIAutomator2 to work with toast messages in Android.

Hope this helps.

Upvotes: 0

Ishita Shah
Ishita Shah

Reputation: 4035

You can get toast message and define Success/Fail operation:

By toastContainer = By.xpath("//div[@id='toast-container']//*");
By toastMessageDA = By.xpath("//div[@class='toast-message']");

public String toastUtility() throws Exception {
    toast_container_flag = false;
    try {
        if (driver.findElement(toastContainer).isEnabled()) {

            toast_container_flag = true;

            List<WebElement> findData = driver.findElements(toastContainer);
            for (WebElement element : findData) {
                if (element.getAttribute("class").toString().contains("toast toast")) {
                    toast_success_fail = element.getAttribute("class").toString();
                }
            }
            validationMessage = "Toast: " + driver.findElement(toastMessageDA).getText();
            js.executeScript("arguments[0].click();", driver.findElement(toastMessageDA));

            if (toastr_success_fail.equals("toast toast-success")) {
                System.out.println("Success Message");
            } else if (toastr_success_fail.equals("toast toast-error")) {
                System.out.println("Fail Message");
            } else {
                System.out.println("Other Message");
            }
            System.out.println(validationMessage);
            testResult = validationMessage;
        }
    } catch (Exception e2) {
        testResult = "Toast message is not generated.";
        testlog.info(testResult);
        System.out.println(testResult);
    }
    return testResult;
}

Upvotes: 1

Related Questions