How to read toast message in android

Hi @walpokbymon,

There’s not a whole lot to show in a video, but I can try to explain it step by step:

  1. Record a test using the Record Mobile feature of Katalon Studio (you have probably already done this). You will probably not be able to record the toast message - we’ll put that in later.

  2. Once you’ve finished recording your test, open your new test case file

  3. Click the tab at the bottom of the test case for “Script” - you’ll see the code for the test

  4. At the top of the test file, copy and paste the following lines:

    import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
    import io.appium.java_client.AppiumDriver
    import com.kms.katalon.core.util.KeywordUtil
    
  5. Find the spot in the test code right after you perform the action that triggers the toast.

  6. Copy and paste the following lines after the action that triggers the toast:

    AppiumDriver<?> driver = MobileDriverFactory.getDriver()
    def toast = driver.findElementByXPath("//android.widget.Toast[@text='Added to cart']")
    println("Toast element: " + toast)
    if (toast == null) {
        KeywordUtil.markFailed('ERROR: Toast object not found!')
    }
    
  7. Change the 'Added to cart' text to whatever the text of your toast should say.

  8. Save the test file

  9. Run your test

  10. Look at the “Console” tab of the test for the text “Toast element”

  11. If all goes well, your tests will pass. If for some reason the toast cannot be found, the test will fail when detecting it.

Hope this helps,

Chris

4 Likes