how to handle alert box while automating web view (appium hangs)

5,681 views
Skip to first unread message

Amit Dewan

unread,
Oct 8, 2013, 9:09:53 AM10/8/13
to appium-...@googlegroups.com
Hi There,
I have ran into a problem while automating my website.
I am able to automate my site in ios completely within safari web browser.
But now I have landed into issue where there is an alert box with cancel and ok button. It looks like native UIAlertView.
When AlertView appears on page, appium just does not accept any commands and hangs itself.
Things I tried but didn't worked
1) wd.switchTo().alert().accept();

Amit Dewan

unread,
Oct 8, 2013, 9:15:47 AM10/8/13
to appium-...@googlegroups.com
I also used mobile:tap  but its also not working for me.

Please suggest something so that i can handle alert in webviews and proceed further.
Thanks in advance.

Jonathan Lipps

unread,
Oct 8, 2013, 12:51:46 PM10/8/13
to Amit Dewan, appium-...@googlegroups.com
The tests for alert handling in safari are currently passing. I'm not sure what else to do. Can you send a repro script?

--
http://appium.io
---
You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.
Visit this group at http://groups.google.com/group/appium-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

Amit Dewan

unread,
Oct 9, 2013, 1:30:08 AM10/9/13
to appium-...@googlegroups.com, Amit Dewan
Hi Jonathan,
I have delete confirmation alert.
And when in my script I tap on delete button, the alert appears and script hangs.
This is my code:
WebElement deleteButton = optionList.findElement(By.linkText("Delete"));
        if(deleteButton.isDisplayed()){
            System.out.println("Test Folder deleted successfully");
            deleteButton.click();
            Thread.sleep(3000);
        }

After this, appium server accepts no commands and just hangs script.
I am not even able to get window handle after this.

Amit Dewan

unread,
Oct 9, 2013, 1:41:15 AM10/9/13
to appium-...@googlegroups.com
Hi Jonathan,
Below are the logs from appium console
info: atom did not return yet, checking to see if we are blocked by an alert

debug: Sending command to instruments: au.alertIsPresent()

info: Pushing command to appium work queue: "au.alertIsPresent()"
info: [INSTSERVER] Sending command to instruments: au.alertIsPresent()

Please tell how to proceed in my script now.

Amit Dewan

unread,
Oct 9, 2013, 9:42:24 AM10/9/13
to appium-...@googlegroups.com
Please provide some solution

Amit Dewan

unread,
Oct 9, 2013, 9:58:11 AM10/9/13
to appium-...@googlegroups.com
I will really really appreciate any  solution you provide.
Its very important for me to resolve this.

Jonathan Lipps

unread,
Oct 9, 2013, 11:21:07 AM10/9/13
to Amit Dewan, appium-...@googlegroups.com
Where is your code for accepting/dismissing the alert?

Jonathan Lipps

unread,
Oct 9, 2013, 11:29:40 AM10/9/13
to Amit Dewan, appium-...@googlegroups.com
If you'd like help, you need to provide a repro case. At this point it's difficult to help because the tests for this feature are working just fine so it's not obvious yet that there's a problem with appium.

On Oct 9, 2013, at 6:58 AM, Amit Dewan <amit1...@gmail.com> wrote:

I will really really appreciate any  solution you provide.
Its very important for me to resolve this.

Anil Kumar

unread,
Oct 9, 2013, 11:46:09 AM10/9/13
to appium-...@googlegroups.com
Which appium version are you running? As John mentioned, its difficult to help with the amount of information you have provided. Can you send full logs of appium? (From starting point of appium server upto issuing alert accept command, using smallest possible test case). Otherwise, I can get on team viewer/ skype to have to look.

Anil Kumar

unread,
Oct 9, 2013, 11:57:47 AM10/9/13
to appium-...@googlegroups.com, Amit Dewan
Hi, I could not locate tests for this feature. Can you point me to test exercising alerts on mobile safari?

Jonathan Lipps

unread,
Oct 9, 2013, 12:10:55 PM10/9/13
to Amit Dewan, appium-...@googlegroups.com
On Oct 9, 2013, at 6:58 AM, Amit Dewan <amit1...@gmail.com> wrote:

I will really really appreciate any  solution you provide.
Its very important for me to resolve this.

Amit Dewan

unread,
Oct 10, 2013, 3:18:05 AM10/10/13
to appium-...@googlegroups.com
My problem is:
I am automating safari web-browser on real device using safari launcher app and ios-webkit proxy.
In my script, I reach at delete button and tapping on it generates alert-view.
After alert view is generated, script just accepts no command and appium server waits infinitely.
//code before generating alert   
        WebElement deleteButton = optionList.findElement(By.linkText("Delete"));
        if(deleteButton.isDisplayed()){
            System.out.println("Test Folder deletion process");
            deleteButton.click();//this line pops up alert
            Thread.sleep(300);
        }
        //code after generating alert (This code is never executed)
        ((JavascriptExecutor) wd).executeScript("mobile: leaveWebView");
        wd.switchTo().alert().accept();
        System.out.println("sssssssssssssssssssssssssssssssssssss");
        System.out.println(wd.getPageSource());

I am also attaching my appium server logs here.
Can you now please tell the solution

appium_hangs_logs.txt

Amit Dewan

unread,
Oct 10, 2013, 8:21:24 AM10/10/13
to appium-...@googlegroups.com
Thanks Anil for telling somewhat:)
Now I am hitting the delete button(which brings alert) with coordinates and my control is going forward.
But now I am not able to dismiss/accept the alert.
I have already tried tapping with coordinates which again hangs appium.
Even switching to alert does not work.

Please tell now how should I /accept the alert?

Amit Dewan

unread,
Oct 10, 2013, 8:35:00 AM10/10/13
to appium-...@googlegroups.com
Below is my code I execute after hitting delete button with coordinates

Set<String> handles = wd.getWindowHandles();

    System.out.println("# of Window Hndlerrs "+handles.size());

    wd.switchTo().alert().accept();


And I am also attaching appium console logs here.


I think this will help you in telling how should I accept the alert?

appium_alert.txt

Anil Kumar

unread,
Oct 10, 2013, 9:15:34 AM10/10/13
to appium-...@googlegroups.com
Hi Amit, I tried testing alerts at my end, and it seems to work fine. Files are attached. I'll be able to help more only when I can run tests on your web application only. Is your application available on a public domain? Did you tried running tests on simulator? Did you tried "mobile: leaveWebView" before switching to alert? Also, your appium log indicates that you was retrieving alert text, can you try with accept(), instead of getting text and see if that works?

Let me know.


On Tuesday, October 8, 2013 6:39:53 PM UTC+5:30, Amit Dewan wrote:
AlertTest.java

Anil Kumar

unread,
Oct 10, 2013, 9:19:06 AM10/10/13
to appium-...@googlegroups.com
Appium Log attached again.


On Tuesday, October 8, 2013 6:39:53 PM UTC+5:30, Amit Dewan wrote:
AppiumAlertLog.txt

Amit Dewan

unread,
Oct 11, 2013, 8:40:24 AM10/11/13
to appium-...@googlegroups.com
Now problem has been analyzed by me. Its with configuration. Now you have to provide the solution.
I am using appium.app(GUI for Mac) v0.10.1
I am using safari launcher app with 30 seconds delay to open safari browser on ios device and running ios-webkit-proxy in the background.

I am pasting whole of my contents of class created so that you can know my desired capabilities for the script.
The problem is the script which is working for you , is not working for me and it hangs appium server infinitely.
I am also pasting logs here.
*************************************************************************************************************************************************************************
package com.testing.box.webapp;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class MainTest {

    private static WebDriver wd;
   
    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
        capabilities.setCapability(CapabilityType.VERSION, "6.1");
        capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
        capabilities.setCapability("app", "/Users/adewan/appium_tool_evaluation/deviceSafariLauncher/SafariLauncher.app");
        capabilities.setCapability("device", "iPhone Simulator");
        wd = new RemoteWebDriver(new URL("http://127.0.0.1:8080/wd/hub"), capabilities);
        wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
         Thread.sleep(35000);

           Set<String> handles = wd.getWindowHandles();
               System.out.println("# of Window Hndlerrs "+handles.size());
              
                   wd.switchTo().window(handles.iterator().next());
                   wd.navigate().to("http://www.nowonline.in/");
                   Thread.sleep(4000);
                   // Tap sign in button
                wd.findElement(By.xpath("//input[@value='Sign In']")).click();

                // Accept alert
                wd.switchTo().alert().accept();
                wd.quit();
    }

}

Thanks in advance.

Appium_logs_11October.txt

Amit Dewan

unread,
Oct 11, 2013, 8:57:00 AM10/11/13
to appium-...@googlegroups.com
Even mobile:leavewebview doesn't help.
Following is my code.
*****************************************************************************************************************
WebDriver augmentedDriver = new Augmenter().augment(wd);
        JavascriptExecutor js = (JavascriptExecutor) augmentedDriver;
        HashMap<String, Double> tapObject = new HashMap<String, Double>();
        tapObject.put("x", 0.80); // percentage of screen from left
        tapObject.put("y", 0.48); // percentage of screen from top
        js.executeScript("mobile: tap", tapObject);
        Thread.sleep(2000);
//this is the code which gets executed after generating alert (could not accept/dismiss alert)

((JavascriptExecutor) wd).executeScript("mobile: leaveWebView");
       ((JavascriptExecutor) wd).executeScript("UIATarget.localTarget().frontMostApp().alert().defaultButton().tap()");

Now can you help me??

Amit Dewan

unread,
Oct 11, 2013, 9:13:25 AM10/11/13
to appium-...@googlegroups.com
Also, if I execute below code I get error  Result is not protocol compliant, wrapping
********************************************************************************************************************


    ((JavascriptExecutor) wd).executeScript("mobile: leaveWebView");
            WebDriver augmentedDriver = new Augmenter().augment(wd);
        JavascriptExecutor js = (JavascriptExecutor) augmentedDriver;
        HashMap<String, Double> tapObject = new HashMap<String, Double>();
        tapObject.put("x", 0.80); // percentage of screen from left
        tapObject.put("y", 0.48); // percentage of screen from top
        js.executeScript("mobile: tap", tapObject);             //this code generates alert
        Thread.sleep(2000);
       ((JavascriptExecutor) wd).executeScript("UIATarget.localTarget().frontMostApp().alert().buttons()[\"OK\"].tap()");
*************************************************************************************************************************
I am also attaching the logs
Appium_logs_11October.txt

Anil Kumar

unread,
Oct 11, 2013, 9:23:13 AM10/11/13
to Amit Dewan, appium-...@googlegroups.com
Hi Amit,

What if you pass:
capabilities.setCapability("app", "safari");

instead of:
capabilities.setCapability("app", "/Users/adewan/appium_tool_evaluation/deviceSafariLauncher/SafariLauncher.app");

--
http://appium.io
---
You received this message because you are subscribed to a topic in the Google Groups "Appium-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/appium-discuss/c0Lji96B90I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to appium-discus...@googlegroups.com.

Amit Dewan

unread,
Oct 11, 2013, 10:16:38 AM10/11/13
to appium-...@googlegroups.com, Amit Dewan
If I pass  below code in desired capabilities

capabilities.setCapability("app", "safari");

then I am able to automate only in simulator and not in real ios device.

and this is the only reason I am using SafariLauncher.app

Amit Dewan

unread,
Oct 11, 2013, 10:19:59 AM10/11/13
to appium-...@googlegroups.com, Amit Dewan
Are you able to automate in real ios device without using safari launcher app.
Do you use ios-webkit-proxy?

Jonathan Lipps

unread,
Oct 11, 2013, 12:21:00 PM10/11/13
to Amit Dewan, appium-...@googlegroups.com
Hmm, I'm not sure it's possible to automate alerts on safari on real devices.

Alerts are part of the native layer, not the web layer, so we need to automate the native layer with instruments in order to accept/dismiss alerts. This is exactly what's NOT happening in safari on real devices, where the "app" we're automating is SafariLauncher, not safari. once safari pops up, we're controlling it via the remote debug layer, which only gives us javascript access to the webview, not native access.

So I think this is unsolvable, unless anyone else has had success with this?

Jonathan

You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.

David Vacca

unread,
Oct 11, 2013, 4:46:27 PM10/11/13
to appium-...@googlegroups.com, Amit Dewan
I fixed the ios7 alert problem doing just this:

driver.findElement(By.name("OK")).click();

I don't know if it works in real devices. 

David.

Amit Dewan

unread,
Oct 14, 2013, 1:33:24 AM10/14/13
to appium-...@googlegroups.com, Amit Dewan
Hi Anil,
Are you able to automate safari web browser in real ios device without using safari laubcher app.?

Anil Kumar

unread,
Oct 14, 2013, 1:38:37 AM10/14/13
to Amit Dewan, appium-...@googlegroups.com
I will test this today on how to handle alerts on real device.


On Mon, Oct 14, 2013 at 11:03 AM, Amit Dewan <amit1...@gmail.com> wrote:
Hi Anil,
Are you able to automate safari web browser in real ios device without using safari laubcher app.?

--
http://appium.io
---
You received this message because you are subscribed to a topic in the Google Groups "Appium-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/appium-discuss/c0Lji96B90I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to appium-discus...@googlegroups.com.

Amit Dewan

unread,
Oct 14, 2013, 1:48:54 AM10/14/13
to appium-...@googlegroups.com, Amit Dewan
Thanks and do update me.

Amit Dewan

unread,
Oct 15, 2013, 1:18:57 AM10/15/13
to appium-...@googlegroups.com, Amit Dewan

Hi Anil,
Have you tested alerts in real ios device?

Anil Kumar

unread,
Oct 15, 2013, 1:24:41 AM10/15/13
to Amit Dewan, appium-...@googlegroups.com
I couldn't get it to work using safari-launcher app yet. Manually, safari-launcher app launches safari fine, but when I use appium, it fails to launch safari at all. I will be trying again today.

Btw, did you ever tried using iWebDriver?


On Tue, Oct 15, 2013 at 10:48 AM, Amit Dewan <amit1...@gmail.com> wrote:

Hi Anil,
Have you tested alerts in real ios device?

--

Amit Dewan

unread,
Oct 15, 2013, 1:54:54 AM10/15/13
to appium-...@googlegroups.com
Hi Anil,.

Follow the instructions from  https://github.com/appium/appium/blob/master/docs/hybrid.md   to work on real device.

Download safariLauncher app as zip file from https://github.com/budhash/SafariLauncher. I changed the default delay time of launching safari to 30

Anil Kumar

unread,
Oct 15, 2013, 2:05:07 AM10/15/13
to Amit Dewan, appium-...@googlegroups.com
I have already download source and built it using xCode, and application is installed on real device. It works fine manually and launches safari, it just doesn't work with appium.

Why do you need to change default delay to 30?


--

Amit Dewan

unread,
Oct 15, 2013, 2:12:17 AM10/15/13
to appium-...@googlegroups.com, Amit Dewan
Configure appium server as shown in the screenshot
appium screen.png

Anil Kumar

unread,
Oct 15, 2013, 2:33:52 AM10/15/13
to Amit Dewan, appium-...@googlegroups.com
ok, now safari launcher works fine, but appium fails with-

info: Responding to client with success: {"status":0,"value":{"build":{"version":"0.10.1","revision":"d0827d6b2507c982802d02b2c6d2bb4d6906bdb3"}}}


debug: Appium request initiated at /wd/hub/status


GET /wd/hub/status 200 5ms - 144
info: Responding to client with success: {"status":0,"value":[]}
GET /wd/hub/sessions 200 0ms - 32
info: App is an iOS bundle, will attempt to run as pre-existing
info: Creating new appium session ef88ac46-2799-4599-a5d7-6a93e310e13c
info: Removing any remaining instruments sockets
info: Cleaned up instruments socket /tmp/instruments_sock
info: Localizable.strings is not currently supported when using real devices.
info: Not setting device type since we're connected to a device
info: instruments is: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments
info: [INSTSERVER] Instruments socket server started at /tmp/instruments_sock
info: Attempting to run app on real device with UDID 7d881718c7bdddd8654fdef13e5b9930380a5be7
info: Spawning instruments with command: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate -w 7d881718c7bdddd8654fdef13e5b9930380a5be7 com.bytearc.SafariLauncher -e UIASCRIPT /Applications/Appium.app/Contents/Resources/node_modules/appium/app/uiauto/bootstrap.js -e UIARESULTSPATH /tmp/appium-instruments/
info: And extra without-delay env: {}
info: Responding to client with error: {"status":6,"value":{"message":"A session is either terminated or not started (Original error: Instruments did not launch successfully--please check your app paths or bundle IDs and try again)","origValue":"Instruments did not launch successfully--please check your app paths or bundle IDs and try again"},"sessionId":"ef88ac46-2799-4599-a5d7-6a93e310e13c"}
POST /wd/hub/session 500 90072ms - 385
info: Clearing out appium devices


debug: 127.0.0.1 - - "GET /wd/hub/status HTTP/1.1" 200 144 "-" "Appium (unknown version) CFNetwork/596.5 Darwin/12.5.0 (x86_64) (MacBookPro9%2C2)"
debug: Appium request initiated at /wd/hub/sessions
debug: 127.0.0.1 - - "GET /wd/hub/sessions HTTP/1.1" 200 32 "-" "Appium (unknown version) CFNetwork/596.5 Darwin/12.5.0 (x86_64) (MacBookPro9%2C2)"
debug: Appium request initiated at /wd/hub/session
debug: Request received with params: {"desiredCapabilities":{}}
debug: No device id or app, not installing to real device.
debug: Creating instruments
error: Instruments socket client never checked in; timing out
error: Instruments did not launch successfully, failing session
error: Failed to start an Appium session, err was: Error: Instruments did not launch successfully--please check your app paths or bundle IDs and try again
debug: 127.0.0.1 - - "POST /wd/hub/session HTTP/1.1" 500 385 "-" "Appium (unknown version) CFNetwork/596.5 Darwin/12.5.0 (x86_64) (MacBookPro9%2C2)"


info: [INST] Instruments Trace Complete (Duration : 89.384102s; Output : /Applications/Appium.app/Contents/Resources/node_modules/appium/instrumentscli9.trace)
       


error: Instruments did not launch successfully, failing session


info: [INSTSERVER] Instruments exited with code 0




On Tue, Oct 15, 2013 at 11:42 AM, Amit Dewan <amit1...@gmail.com> wrote:
Configure appium server as shown in the screenshot

--

Amit Dewan

unread,
Oct 15, 2013, 2:50:04 AM10/15/13
to appium-...@googlegroups.com
This was the reason why I asked you to change the default safari launching time by safarilauncher app to 30 sec. You can change it in root.plist

And then code for driving safari through safari launcher app is given below:-
 package com.saucelabs.appium;

import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class DeviceSafari {

    private WebDriver wd;

    /**
* Instantiates the {@link #driver} instance by using DesiredCapabilities which specify the
* 'iPhone Simulator' device and 'safari' app.
* @throws Exception
*/
    @Before
    public void setUp() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
capabilities.setCapability(CapabilityType.VERSION, "6.1");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("app", "/Users/adewan/appium_tool_evaluation/deviceSafariLauncher/SafariLauncher.app");
capabilities.setCapability("device", "iPhone Simulator");
wd = new RemoteWebDriver(new URL("http://127.0.0.1:8080/wd/hub"), capabilities);
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

    /**
* Navigates to http://saucelabs.com/test/guinea-pig and interacts with the browser.
*
* @throws Exception
*/
    @Test
    public void runTest() throws Exception {
    Thread.sleep(35000);

    //System.out.println("oooooooooooooooooooooooo"+wd.getPageSource());

    Set<String> handles = wd.getWindowHandles();
    System.out.println("# of Window Hndlerrs "+handles.size());
   
    wd.switchTo().window(handles.iterator().next());
     
    System.out.println("window title ::"+ wd.getTitle()); 
    System.out.println(wd.getPageSource());
 }
    /**
* Closes the {@link #driver} instance.
*
* @throws Exception
*/
    @After
    public void tearDown() throws Exception {
        wd.quit();
    }

Gadagoju Santosh

unread,
Oct 15, 2013, 8:10:59 PM10/15/13
to appium-...@googlegroups.com
Hi Amit,
This is not actually the solution to the issue.
Just wanted to let you know, you need not change the safari launching time in root.plist and rebuild the app.
You can increase the time directly from the safari launcher app that is already built on the device.
Go to Device Settings > Safari Launcher.
You can drag the button till end to increase the time to 30 sec.
Default is set to 20 sec and max is set to 30.
Hence, dragging it till the end sets the time to 30 sec.

Even am facing the same alert box issue in my application.
Hope we/some one resolves the issue :)

Amit Dewan

unread,
Oct 16, 2013, 1:19:11 AM10/16/13
to appium-...@googlegroups.com
Hi Gadagoju Santosh,
Thanks for telling me the new solution to increase the default safari launching time.
And I also hope someone will solve the alert box issue on device.

Thanks,
Amit Dewan
Reply all
Reply to author
Forward
0 new messages