Back goes to wrong page in Hybrid Android App

I am automating an Hybrid Android app using Node.js + Chai + Appium + SauceLabs.

In my app I have a button that opens a PDF file in the Android PDF viewer. The PDF opens alright, but when I go back, it goes to first page instead of the page I was before.

I use the following code:

it("Should click pdf file and go back'", function() {
        return driver
            .waitForElementByXPath('//*[@id="openPDF"]')
            .click()
            .deviceKeyEvent(4);
});

Is it a bug or by design? How can I solve it?

What happens if you run your scenario manually?

It works as it should. Clicking on the device back button or the ‘home as up’ back button goes back to the right place.

change driver context to native and then press back button (key-event)

The same thing happens.

it("Should click pdf file and go back'", function() {
        return driver
            .waitForElementByXPath('//*[@id="openPDF"]')
            .click()
            .context('NATIVE_APP')
            .deviceKeyEvent(4);
});

I think, you should give some time delay after switched to native context before clicking on back button.

use Thread.sleep(8000) after native context.

It doesn’t work either.

I had faced similar issues earlier. Note : Generally in Hybrid view, when the MWeb page opens up there are two back buttons actions. One is from the device back button, second is from the back button within the WebView. Sometimes the back button action from both the sources vary. The Back Button on the Mweb would take you a page back in the WebView, whereas the native Back button might take you out of the Web-View Container to the page before. This can be programmatically synced to perform the same action by your Dev team.

Let me know if this helps.

1 Like

Yes, that makes sense. Thanks