Zhenlei Cai
Zhenlei Cai

Reputation: 123

How can I programmatically click on "Buy" button in automated Android in-app billing testing

I am writing automated testing code (using ActivityInstrumentationTestCase2) to test an app that involves in-app billing version 2 (subscriptions). I need help on figuring out how to programmatically get my testing code to click on the "Accept and Buy" button that appears inside the "Pay with credit card/Secured by Google Wallet" dialog which is supposedly created by some Google SDK code.

I can get the testing code to click on other buttons that are created by my own code/acitivty, usually by accessing the buttons from its parent activity or view, and then call the .performClick() method on the button. But this "Buy" button is not reachable from any activities as far as I know.

It appears this dialog may be created by com.google.android.finsky.activities.IabActivity based on the logcat output. I used an ActivityMonitor watching this activity, but it was never triggered. If you can help either directly, or point me to the relevant Google SDK source code that's responsible for creating the "Pay with credit card" dialog or the source for the com.google.android.finsky.activities.IabActivity class, I'd be very grateful.

Upvotes: 2

Views: 2651

Answers (5)

Yessy
Yessy

Reputation: 1352

    UiAutomation uiAutomation = new UiAutomation(Looper.getMainLooper(), new UiAutomationConnection());
    uiAutomation.setOnAccessibilityEventListener(new UiAutomation.OnAccessibilityEventListener() {
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            if (event.getEventType() == TYPE_WINDOW_STATE_CHANGED) {
                if ("com.google.android.finsky".contentEquals(event.getPackageName())) {
                    if ("com.google.android.finsky.activities.IabActivity".contentEquals(event.getClassName())) {
                        final List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = event.getSource().findAccessibilityNodeInfosByViewId("<package name>:id/<control id>"); // find by layout inspector
                        if (accessibilityNodeInfosByViewId.size() > 0) {
                            accessibilityNodeInfosByViewId.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        }
                    }
                }
            }
        }
    });
    uiAutomation.connect();
    //release when exit
    uiAutomation.disconnect();

Upvotes: 0

G. Steigert
G. Steigert

Reputation: 177

Using uiautomator, this is the best I got:

device.findObject(new UiSelector().resourceId("com.android.vending:id/continue_button")).click();

Upvotes: 1

Zhenlei Cai
Zhenlei Cai

Reputation: 123

Thanks to Christopher. I managed to use UI Automation to get the effect I want. I had to write a shell script to call my Instrumentation Test first, which brings up the Buy dialog. Then call
adb shell uiautomator runtest to launch the UI automation code to click on the "Buy" button. Not sure how to sync between them so I just use some sleep code to time the click. Here is the UI automation code:

    UiDevice dev = getUiDevice();
    // wait for buy button to appear 
    while (true) {
        try {
            Thread.sleep(2000l);
            UiObject okButton = new UiObject(new UiSelector().text("Accept & buy").className("android.widget.Button"));
            okButton.click();
            break;
        } catch (Exception e) {
        }
    }   
    dev.waitForIdle();

Upvotes: 1

Christopher Orr
Christopher Orr

Reputation: 111575

Normally you can't instrument Activities outside of your own package.

You'd have to use the UI Automator framework to click the Buy button.

Upvotes: 3

Herry
Herry

Reputation: 7087

Here are suggestion for making Testcase for Button click in android.

You need to make ActivityInstrumentationTestCase2 for your Activity then do following .

Initialize your button in

protected void setUp() throws Exception {
        super.setUp();
        mainActivity = getActivity();

        btnAcceptBuy= (Button) mainActivity.findViewById(R.id.btnaccept_buy);
    }



public void testFragmentKeypad(){

      AppLog.showLogE("TEST", "Keypad");

      TouchUtils.clickView(this, btnAcceptBuy);

  }

By this way you can perform click testing of your button in android.

Upvotes: 0

Related Questions