How to deal with custom system alerts

Hello,
In my tested app there is pop up of the system where I have dialer and than I have two lines in my pop up :

  1. call to “number”
  2. cancel
    also there is pop up that comes from bluethooth that wants to pair with my device and the options there are :
  3. pair
  4. cancel
    How to handke these two pop ups?
    and also I tried in the first pop up to click on “call” and I managed to do these in while loop (with no connection to alerts) does it mean something that that is not a system pop up because finally it was managed to click?
  • Platform = iOS
  • Appium version: 1.21
  • Real Device
  • Client langauage : JAVA
  • OS = MAC

Thanks

You can always identify an alert from iOS by the presence of an ‘XCUIElementTypeAlert’ element. Once you’ve identified it as an alert, treat it like any other screen, identify it by either text or accessibility id, and then interact with it. For example you may want to have a test where you ‘cancel’ the call. In that case you would:

  1. Identify that you are on an alert screen
  2. Identify which alert screen
  3. Interact with the screen according to the test
  4. Move on to next screen

@wreed Thank you, and how do I move to the alert? because I tried the approach that you offer and sometimes it succed and sometimes not (that why I use for loop)

The alert should be on top when it shows. Maybe you are moving too fast? Try an implicit wait:

https://appium.io/docs/en/commands/session/timeouts/implicit-wait/

@wreed I see the alert and it’s on top, there is need to move to element (to the alert)?
I saw post that wait for the alert here

Sorry if I’m not being clear, I’m going to reference some code that might help. Once you’ve identified that you have an alert, then you need to do something like this (in Java):

driver.SwitchTo().Alert().Accept();

You may want to create a method like this one: Alert Method
^^^Very similar to @Aleksei’s code that you referenced above.

Or if you are using Page Object model, then create a class that represents the alert and can make decisions based on elements found in the alert (like the text).

Thank you @wreed , I did something simmilar where I send to the method (it’s Page Object Model) the element to click on it and click on it.
so questions:

  1. do I need to tap on it or click on it?
  2. after I click / tap on the alert, and let’s say the alert is gone, do I need to switch the driver back to the main app or its move automaticly?

These are good questions that I haven’t thought about in a long time. Let me answer in order:

  1. I have never used the ‘tap’ feature in Appium. My work is mostly in iOS, and I only use click so I would think that would be the way to go. Edited to add: Actually, you don’t need to use ‘click’ or ‘tap’. The ‘Alert().accept()’ (or ‘Alert().dismiss()’) will take care of that for you. It seems that may also get you out of alert mode. I took a look at the Appium github page but I couldn’t find exactly what happens there.
  2. I looked through my code for this. We do the in the constructor (Ruby) but we don’t ever switch back. I think once the alert is dismissed you automatically go back to the main driver.

@wreed Thank you,
I also work in IOS and do the tap on all the element because I think that not all the elements are Clickable (you can tap where ever you want but not click where ever you want) I think that these is the difference maybe?
And about the alert, my alert is not with Accept or dismiss, it with “call to 911” for example (the number is changed each time so I need to click on the phone image over there) or cancel and approve so I can’t use the regular options and I need to pass the option to choose from.
About point number 2 it is very interest because in the web there is need to go back.

I looked at the Alert code for Java, which is here:

Java-Alert

Seems like you will have to tap. For getting back to the regular driver I am unsure. I don’t see anything in there indicating one way or another. You will probably need to test it out.

For the tap, just find the element and divide height/2 and width/2, then you can perform a tap on the middle.



Attached two pictures from the mobile
About the going back , I tried…and look like no need to.
About the tap, I didn’t understand why there’s need to tap on the middle? if it’s another device?
I did method like these where it gets the option to choose from:

public boolean clickInAlertElement(By elem) {
try{
wait.until(ExpectedConditions.alertIsPresent());
driver.findElement(elem).click();
return true;
} catch (Throwable t) {
return false;
}
}

If you are clicking you don’t need to worry about coordinates, but if you choose to tap you’ll want it to adjust for different device sizes because it’s by coordinates that will change.

Thank you,
Not sure because I did tap on element without coordinates and it worked and you see according to the pictures that I need to choose the element itself, but eventualy it was clicked