Autoplay



Transcripted Summary

# Automate Web Testing on Safari and iOS devices

In this demo, we will learn how to automate web testing on Safari and iOS devices.

Here, this is our test case, and we just need to change the desired capabilities from Android to say that the platformName is iOS, the browserName is Safari, not Chrome, in the case of Android, and the deviceName.

We just need to add one additional capability, which is safari:useSimulator and we set it to true.

This will help us to run Safari using simulators on our test cases.


import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class iOS_Safari_Simulator_Device_Test {
    private AppiumDriver driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("platformVersion", "14.4");
        capabilities.setCapability("browserName", "Safari");
        capabilities.setCapability("deviceName", "iPhone 11 Pro");
        capabilities.setCapability("safari:useSimulator", true);
        driver = new IOSDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    @Test
    public void userLogin() {
        driver.get("https://the-internet.herokuapp.com/login");
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
        WebElement username = driver.findElementByCssSelector("input#username");
        username.sendKeys("tomsmith");
        WebElement password = driver.findElementByCssSelector("input#password");
        password.sendKeys("SuperSecretPassword!");
        WebElement loginBtn = driver.findElementByCssSelector("button.radius");
        loginBtn.click();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.urlContains("secure"));
        Assert.assertTrue(driver.getCurrentUrl().contains("secure"));
        System.out.println(driver.getCurrentUrl());
    }
}

We are using the same script that we did in the Android web testing.

So let's run our test case and check how it will work or what the test results will be on our application.

We will close the web browser and then we start opening Safari on the simulator, then redirect to the URL, and then we'll start our testing.

Then our test case passed, and here also, we have the URL.

So all we needed to change was the desired capabilities, but the script is the same.

So this is the main power of Appium - we can use the same script to run on different platforms and different versions using the desired capabilities.

If we wanted to run the IOS web testing on a Safari on a real device, we need to use the same desired capabilities that we used before on the application, like the xcodeOrgId, xcodeSigningId, udid, derivedDataPath, and useNewWDA = true.


import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class iOS_Safari_Real_Device_Test {
    private AppiumDriver driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("platformVersion", "14.3");
        capabilities.setCapability("automationName", "XCUITest");
        capabilities.setCapability("browserName", "Safari");
        capabilities.setCapability("xcodeOrgId", "");
        capabilities.setCapability("xcodeSigningId", "iPhone Developer");
        capabilities.setCapability("udid", "");
        capabilities.setCapability("deviceName", "");
        capabilities.setCapability("useNewWDA", "true");
        capabilities.setCapability("derivedDataPath",
                "");
        driver = new IOSDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    @Test
    public void userLogin() {
        driver.get("https://the-internet.herokuapp.com/login");
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
        WebElement username = driver.findElementByCssSelector("input#username");
        username.sendKeys("tomsmith");
        WebElement password = driver.findElementByCssSelector("input#password");
        password.sendKeys("SuperSecretPassword!");
        WebElement loginBtn = driver.findElementByCssSelector("button.radius");
        loginBtn.click();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.urlContains("secure"));
        Assert.assertTrue(driver.getCurrentUrl().contains("secure"));
        System.out.println(driver.getCurrentUrl());
    }
}

With the same script, we don't need to change anything in the script, just the desired capabilities need to be changeable and the browserName.

Also, we removed the simulator from the previous demo and just used the browserName.

Let's run our test script; I already connected my device.

I open the iPad Pro, so now we are installing the WebDriverAgent and then we will open the Safari browser.

So our test is now running and then we will check the test results.

The browser is open now and by the way, it by default redirects to the Appium website, and then we go to our application and then our test cases pass.

This is how we can run the web testing for iOS real devices.

We just need to add the desired capabilities that are required for the real device and the same script is running without any



Resources



Quiz

The quiz for this chapter can be found in Chapter 6.3

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR