Opening Safari while running iOS app

Hi
my scenario is like this: I want to test universal links, which is a bit different than deeplinks. I want to test that when I tap on a specific universal link, proper screen is displayed in the app.

I was thinking a bit and probably best way to do this is to expose a website containing those universal links and during the test, I want to open Safari, navigate to website and then tap on a specific universal link to open in app.

My code to open Safari is like this:

private void openSafariURL(String URL) {
List args = new ArrayList();
args.add(ā€œ-Uā€);
args.add(URL);
Map<String, Object> params = new HashMap<>();
params.put(ā€œbundleIdā€, ā€œcom.apple.mobilesafariā€);
params.put(ā€œargumentsā€, args);
driver.executeScript(ā€œmobile: launchAppā€, params);
}

Now, all is good above ā†’ First, I open the app and then I run this script above and Safari gets opened in the proper website, but the problem is that website remains open for 3 - 5 seconds, then it gets automatically closed (like closing the tab) and I end up on the blank page of Safari. :slightly_frowning_face:

Why is that happening? Any suggestion how to solve this?

1 Like
driver.activateApp("com.apple.mobilesafari");

now continue as usual in test code. tap -> enter url -> confirm

  1. there is button with name ā€˜URLā€™ tap on it.
  2. now appears text field with same ā€˜URLā€™ name where you enter url needed and tap last keyboard button.

code with actions above in our projects looks (removed Allure and logs):

    public BrowserPage setURLInput(String urlTxt) {
        Assert.assertTrue(tap(urlInputButton), createAssertionLog("Tap 'urlInputButton' FAILED"));
        // clear url for sure (old url appears often on browser start)
        setLookTiming(1);
        tap(urlClearButton);
        setDefaultTiming();
        Assert.assertTrue(setValueHideKeyboard(urlInput, urlTxt), createAssertionLog("FAILED"));
        return this;
    }

all is left for you is specify: urlInputButton + urlClearButton + urlInput + setValueHideKeyboard + tap (your code also).

2 Likes

Apple has removed the support of -U command line argument from Safari, so there is no direct way to run the process with the given url set, at least on real devices

1 Like

@Aleksei, @mykola-mokhnach thank you for your responses.

I have solved it at the end by removing the -U and just open the Safari, tap on URL and enter the String myself. I thought it would be less stable, but it works ok! :slight_smile:

private void openSafariURL(String URL) {
List args = new ArrayList();
Map<String, Object> params = new HashMap<>();
params.put(ā€œbundleIdā€, ā€œcom.apple.mobilesafariā€);
params.put(ā€œargumentsā€, args);
driver.executeScript(ā€œmobile: terminateAppā€, params);
waitTime(2000);
driver.executeScript(ā€œmobile: launchAppā€, params);
if (isElementPresent(driver, By.id(ā€œURLā€))) {
driver.findElement(By.id(ā€œURLā€)).click();
waitTime(800);
driver.switchTo().activeElement().sendKeys(Keys.DELETE);
driver.switchTo().activeElement().sendKeys(URL);
waitTime(400);
driver.switchTo().activeElement().sendKeys(Keys.ENTER);
} else {
Assert.fail("Something went wrong - URL field not found! ");
}
}

However, for running deeplink tests on iOS 14, we are still using -U to directly push URL into Safari. Do you know when they have removed the support for this?

No idea. I donā€™t work for Apple