Not able to find Alertview in iOS using findElementByName or switchTo().alert() or autoAcceptAlerts

Hi,

I have an alert in my application which has only 1 button “OK”.
I have tried -

  1. autoAcceptAlerts = true in desired capabilities
  2. mydriver.switchTo().alert()
  3. MobileElement myAlert = (MobileElement) mydriver2.findElementByName(“OK”);

But, none of them works.

Actually, i have to find the alert. Assert the alert text and then click on OK. but not able to find element alert.

XCode 5.1
Appium 1.3.4

Code using findElementByName

MobileElement myAlert = (MobileElement) mydriver2.findElementByName("OK");

Logs

info: [debug] Responding to client with success: {"status":0,"value":"","sessionId":"0f328133-7647-481d-b353-b795604a1ab9"}
info: <-- POST /wd/hub/session/0f328133-7647-481d-b353-b795604a1ab9/touch/perform 200 1953.263 ms - 74 {"status":0,"value":"","sessionId":"0f328133-7647-481d-b353-b795604a1ab9"}
info: --> POST /wd/hub/session/0f328133-7647-481d-b353-b795604a1ab9/element {"using":"name","value":"OK"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: "au.getElementByName('OK')"
info: [debug] Sending command to instruments: au.getElementByName('OK')
info: [debug] Didn't get a new command in 240 secs, shutting down...
info: Shutting down appium session
info: [debug] Stopping ios
info: [debug] Closing socket server.
info: [debug] Instruments socket server was closed

Code using findElementByName

Alert myAlert = (Alert) mydriver2.switchTo().alert();

Logs

info: <-- POST /wd/hub/session/2cdbdf0c-9904-437f-b9d9-2e30b69a594e/touch/perform 200 1636.909 ms - 74 {"status":0,"value":"","sessionId":"2cdbdf0c-9904-437f-b9d9-2e30b69a594e"}
info: --> GET /wd/hub/session/2cdbdf0c-9904-437f-b9d9-2e30b69a594e/alert_text {}
info: [debug] Pushing command to appium work queue: "au.getAlertText()"
info: [debug] Sending command to instruments: au.getAlertText()
info: [debug] Didn't get a new command in 240 secs, shutting down...
info: Shutting down appium session
info: [debug] Stopping ios
info: [debug] Closing socket server.
info: [debug] Instruments socket server was closed
info: [debug] Sending sigterm to instruments

@kani19 findelementbyname() works but first you have to wait for it to load/appear. I have used it with expected condition and it’s working for me so far.

If you need to test the content of, or make specific interactions with, an alert you definitely do NOT want to be using autoAcceptAlerts as it’s purpose is to automatically dismiss alerts that popup.

So, you need to write some custom functions to check for and interact with your UIAAlert dialogs. From my research thus far, they will all have the following XPATH available. Hope this helps.

dialog = ‘//UIAAlert’
title_text = ‘//UIAAlert/UIAScrollView/UIAStaticText[1]’
Note: title_text is likely also available via the ‘name’ attribute fo the UIAAlert
body_text = ‘//UIAAlert/UIAScrollView/UIAStaticText[2]’
ok_button = ‘//UIAAlert/UIAButton[contains(@name,“OK”)]’

my tests were passing for appium 1.4.16
mydriver.implicitly_wait(60) context.app.wd.switch_to_alert()
mydriver.find_element_by_name(“OK”).click()

Upgraded to Appium 1.5 ( find_element_by_name is deprecated) I updated the test code to use xpath. (tried a few combinations)

mydriver.find_element_by_xpath("//UIAAlert/UIACollectionView/UIAbutton[@name=‘OK’]").click()
mydriver.find_element_by_xpath("//UIAAlert/UIAbutton[@name=‘OK’]").click()

but appium fails to find element
NoSuchElementException: Message: u’An element could not be located on the page using the given search parameters.’

What am I doing wrong ? (python client) See the XML below. TIA

<UIACollectionView name="" label="" value="page 1 of 1" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/1/2/2" x="114" y="587" width="540" height="88"> <UIACollectionCell name="OK" label="" value="0" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/1/2/2/0" x="114" y="588" width="540" height="88"> <UIAButton name="OK" label="OK" value="" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/1/2/2/0/0" x="114" y="588" width="540" height="88"> </UIAButton> </UIACollectionCell> </UIACollectionView> </UIAAlert>

can you try with //UIAbutton[@name='OK'] instead of complete hierarchy.

findelementbyname is deprecated for appium 1.5 , are you on 1.5 ?

Yes, I’m on 1.5 and yes name is deprecated, if you still want to use name use it with XPath.

iOS 11.0 solution using advanced gesture mobile: alert

https://appium.readthedocs.io/en/stable/en/writing-running-appium/ios-xctest-mobile-gestures/

But first check need to be optimized, as it’s quite slow because of xpath, any solution for this check ?

if(getDriver().findElements(By.xpath("//XCUIElementTypeAlert[contains(@name,'access your location while you are using the app')]")).size()  ) {
                JavascriptExecutor js = (JavascriptExecutor) getDriver();
                HashMap<String, String> tapObject = new HashMap<String, String>();
                tapObject.put("action", "accept");
                tapObject.put("label", "Allow");
                js.executeScript("mobile:alert", tapObject);
              }