How to handle random Webview Popups

Hello!

Info:
I’ve been getting to know Appium a little bit and want to automate testing of some features of our native apps with our webshop.

As you can probably tell, the native app is most of the time just a wrapper for our webshop, which runs in a Webview, therefore most of the time testing should take place in webview context.

Parts of the webshop are already tested by Cypress. I want to do “the same thing” in Appium now, atleast for the most critical functions. (I dont want to fully replicate my Cypress tests to Appium tests, this would serve no one I think, apart from "testing the devices native webengine)

The Problem
Sometimes our webshop produces popups for the customer, for example when the webshop is currently closed.

The Solution (for Cypress)
Therefore we emit a javascript event (when a popup opens/is open), which I then can listen to in Cypress and react accordingly to the popup (after finding out, which type of popup it is)

Question
Is something like this also possible in Appium (listeneing to a javascript event in webview context and execute a function) (or rather say driver) or must I include a big “if else” block before all of my tests which “maybe, maybe not find the popup elements in my webview”)

– or do you have other input which might help? :slight_smile:

Thanks!

Not sure if this will help you or not, but the way we do it is by using Page Object Model principles (each Page or Screen is a discreet object) and then only instantiating with Factory Design Pattern:
https://en.wikipedia.org/wiki/Factory_method_pattern
https://www.selenium.dev/documentation/test_practices/encouraged/page_object_models/

So anytime we want to know what screen we are on, we ask the Factory to instantiate the current screen. At that point it’s trivial to add some checks for popups, notifications, etc. The Factory handles all this and the code is very reliable. Because we only call Factory to access Page/Screen objects, popups are handled no matter when they appear.

Another benefit is that code is all decoupled. If Developers change workflow or add a screen, it’s pretty easy to add that screen to code, or change workflow in tests, without having to change underlying infrastructure.

I would say this is an advanced programming paradigm. May not be for everyone. But we have found it useful.

Hello wreed,

thanks for your input.

Actually I do that already. I’ve somewhat followed this boilerplate: https://github.com/webdriverio/appium-boilerplate/tree/main/tests

However its still unclear to me, how I should react - or even fetch that popup. (Or multiples)
Meaning in my page objects I always need to execute code in order to find the popup (if they exist, then execute this…) - I thought it would be cleaner to “listen” to a javascript thrown event (from my Webview) and then do X (before testing actually).

I guess I read up some more and keep trying. If I find nothing theres always the “hacky” solution with the big “if popup then x” block. :wink:

Thanks!

Looking at the webdriverio project, there is a customizable reporter. Maybe you could use this:

But I am not familiar with how to do that.

If you decide to create a Factory for your screens, I have found that getting the raw XML of a screen is a very fast way to identify it. In addition, on iOS at least, we can look for elements like XCUIElementTypeAlert. If we see that element, we know there is an alert present and can then call a subroutine to handle this. I’m sure there are other ways to do this. Good luck!

Just a follow up, in case someone else has this in the future.

I couldnt get this done in Appium. (Or rather testing framework I used --> WebdriverIO (Javascript))

Yea, thats not what I wanted at first. But hear me out.

I saw the following solutions:

  • Bring it to the App and do it there
    ** Didnt like it, since I have many flavours and this only needed to live in the Debug or “Test” flavour of the app.
  • Doing it in Appium (or testing framework)
  • Using a proxy to add javascript to the pages response

I did the proxy approach.
Basically I said to the OS (of the Apps) please use a proxy, and in the proxy (I used mitmproxy) I included javascript which I control and do stuff when the popup comes up.

Works okay for my testing purposes. Havent used it outside of simulators, but I reckon real devices should work aswell (simply need to trust the cert).