Custom ActionsVerify Display of 'Continue as' Button in Mobile Web Browser Using Appium

Verify Display of 'Continue as' Button in Mobile Web Browser Using Appium

This custom action is a method written in Java for Appium automation testing. It is written to verify whether the "Continue as <username>" button is displayed on a mobile web page in the Chrome browser.

Custom Method

public boolean VerifyDisplayedContinueas() {
        boolean isVerified = false;
        WebBrowser.setCurrentBrowser(0);
        browser = WebBrowser.driver_mobile;
        int i = 0;
        while (i < 5) {
            try {
                browser.context("NATIVE_APP");
                WebElement continueButton = browser
                        .findElement(By.id("com.android.chrome:id/account_picker_continue_as_button"));
                if (continueButton.isDisplayed()) {
                    isVerified = true;
                }
                if (isVerified) {
                    browser.context("CHROMIUM");
                    break;
                }
                i++;
            } catch (Exception ex) {
                browser.context("CHROMIUM");
                isVerified = false;
                i++;
            }
        }
        return isVerified;
    }

Functionality Summary

The following table provides method's functionality breakdown:

FieldDescription
Setting Browser Context:It sets the browser context to "NATIVE_APP" to interact with native elements of the mobile app and then switches back to "CHROMIUM" context to interact with the web elements.
Finding the ButtonIt attempts to find the "Continue as" button using its ID ("com.android.chrome:id/account_picker_continue_as_button").
Verifying DisplayIf the button is found and is displayed (such as, visible on the screen), the isVerified flag is set to true.
Breaking the LoopIf the button is found and verified, the loop is broken, and the method returns true
Exception HandlingIf an exception occurs while finding or verifying the button, the method catches the exception, sets isVerified to false, and continues the loop.
Loop LimitThe loop runs a maximum of 5 times (i < 5) to retry finding and verifying the button.
Return ValueThe method returns true if the "Continue as" button is found and displayed within the specified number of attempts; otherwise, it returns false.

This custom action is useful for scenarios where the presence of the "Continue as" button needs to be verified before proceeding with further actions in the test script.