Checking Apple Pay Via Selenium?

Has anyone successfully written Selenium/Appium automation that checks Apple Pay on iOS devices with ecommerce web pages (not native apps)?

We were originally told we could do this via a Saucelabs private device. But it appears to be untrue, at least using the latest iOS devices (i.e., those w/out physical home buttons). Iโ€™m hearing recent Apple XCUITest has removed the ability to interact with Apple Pay screens and home button via automation.

3 Likes

I personally donโ€™t have exp with it but I might be a weird suggestion but have you tried asking chatGPT? It often comes up with good stuff for me:

It is true that Apple has made changes to their XCUITest framework that may impact the ability to interact with Apple Pay screens and home button via automation. However, it is still possible to automate Apple Pay on iOS devices with ecommerce web pages using Appium and Selenium.

Some Appium and Selenium users have reported success in automating Apple Pay on iOS devices with ecommerce web pages. However, the specific implementation may vary depending on the website being tested and the iOS device being used. It is possible that the ability to interact with Apple Pay screens and home button via automation may be limited on some devices or with certain versions of iOS.

If you are having trouble automating Apple Pay on iOS devices with ecommerce web pages, it may be helpful to reach out to the Appium or Selenium community for guidance or to consider exploring other testing tools or frameworks that may better support this functionality.

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;

public class ApplePayTest {
    public static void main(String[] args) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "14.5");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 12 Pro Max");
        capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
        capabilities.setCapability("automationName", "XCUITest");
        capabilities.setCapability("useNewWDA", true);
        capabilities.setCapability("wdaLaunchTimeout", 240000);
        capabilities.setCapability("xcodeSigningId", "iPhone Developer");
        capabilities.setCapability("xcodeOrgId", "YOUR_ORG_ID");
        capabilities.setCapability("updatedWDABundleId", "YOUR_BUNDLE_ID");
        capabilities.setCapability("startIWDP", true);

        IOSDriver driver = new IOSDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
        driver.get("https://example.com/checkout");

        // Check if the user's device supports Apple Pay
        boolean isApplePaySupported = driver.executeScript("return window.ApplePaySession !== undefined");
        if (isApplePaySupported) {
            // Create a payment request
            String merchantIdentifier = "YOUR_MERCHANT_IDENTIFIER";
            String countryCode = "US";
            String currencyCode = "USD";
            String totalLabel = "Total";
            String totalAmount = "10.00";
            String paymentRequestScript = "var paymentRequest = {\n" +
                    "        countryCode: '" + countryCode + "',\n" +
                    "        currencyCode: '" + currencyCode + "',\n" +
                    "        total: {\n" +
                    "          label: '" + totalLabel + "',\n" +
                    "          amount: '" + totalAmount + "'\n" +
                    "        }\n" +
                    "      };\n" +
                    "      ApplePaySession.canMakePaymentsWithActiveCard('" + merchantIdentifier + "')\n" +
                    "        .then(function(canMakePayments) {\n" +
                    "          if (canMakePayments) {\n" +
                    "            var session = new ApplePaySession(1, paymentRequest);\n" +
                    "            session.begin();\n" +
                    "          }\n" +
                    "        })\n";
            driver.executeScript(paymentRequestScript);

            // Wait for the payment sheet to appear
            Thread.sleep(5000);

            // Click the "Buy with Apple Pay" button
            driver.findElementByClassName("apple-pay-button").click();

            // Wait for the payment to be processed
            Thread.sleep(5000);

            // Verify that the payment was successful
            String pageSource = driver.getPageSource();
            if (pageSource.contains("Thank you for your order!")) {
                System.out.println("Payment successful!");
            } else {
                System.out.println("Payment failed.");
            }
        } else {
            System.out.println("Apple Pay is not supported on this device.");
        }

        driver.quit();
    }
}

This test script launches Safari on an iPhone 12 Pro Max device running iOS 14.5, navigates to an ecommerce website, and interacts with the Apple Pay button to initiate a payment. The test script then waits for the payment to be processed and verifies that the payment was successful by checking the page source for a confirmation message. Finally, the script quits the driver.

2 Likes

I would ask - how often do you need to test purch flow, really, how often does it change in ways that a manual test would not easily cover you for risk? Sorry cannot help there though, have had similar roadblocks thrown up myself, so very interested in this conversation outcome.

Right @conrad.connected . Thatโ€™s a good question. Apparently there are different Apple Pay configs for our various global site. We want to automate one happy path checkout test, and run it for each of about 15 regional websites. Humans could obviously do it but it would be tedious.

Ah - yeah thatโ€™s a good use case for automated test - you cannot just test one of them and assume the others will all be correct. Itโ€™s also rubbish to manually test that because too easy to make mistakes. Damn - It feels like a job for someone who has a lot of patience. Good luck, let us know.

Hi @ejacobson ,
Did you ever find a solution to this issue? Iโ€™ve opted for BrowserStack rather than SauceLabs, but the challenge is smiliar.

Do you want to test Apple Pay or your app?