Push Notification Testing in TestGrid Real Device
Push notification testing is an essential part of verifying the functionality of your app on real devices. This guide explains how to perform push notification testing manually and through automation on the TestGrid platform.
Overview
This article provides a step-by-step process for conducting push notification testing in TestGrid using real devices.
Key Highlights:
- Manual testing of push notifications on connected devices.
- Automating push notification testing using Appium and sample Java code.
By following this guide, users will be able to test notifications efficiently to ensure they work seamlessly.
Part 1: Manual Testing
Steps for Manual Testing:
- Log in:
Access your TestGrid account by logging in with your valid credentials. - Connect a Device:
Navigate to the Device Cloud Dashboard and connect to a real device of your choice. - View Notifications:
On the connected device, swipe down from the top of the screen to access the notification shade.
Check if your push notification appears.
Part 2: Automation Testing
Automating push notification testing allows you to streamline the process and run it across multiple devices or scenarios. Follow the steps below to set it up:
Steps for Automation Testing:
- Retrieve Appium Capabilities:
Obtain the Appium desired capabilities from the TestGrid Device Cloud for the specific device you want to test. - Use Sample Java Code:
Below is a sample Java code snippet to automate push notification testing. Modify the code as needed for your specific use case:
Automation for Android.
import io.appium.java_client.MobileBy; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.URL; public class PushNotification { public static void main(String[] args) { AndroidDriver<MobileElement> driver = null; try { // Desired Capabilities DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "Pixel 4"); capabilities.setCapability("platformVersion", "12"); capabilities.setCapability("platformName", "android"); capabilities.setCapability("automationName", "UiAutomator2"); capabilities.setCapability("udid", "xxxxxx"); capabilities.setCapability("tg:userToken", "xxxxxxx"); capabilities.setCapability("systemPort", "xxxx"); capabilities.setCapability("uiautomator2ServerLaunchTimeout", "90000"); capabilities.setCapability("autoGrantPermissions", true); // Initialize Appium Driver driver = new AndroidDriver<>(new URL("http://{{your_Public_URL}}:4723/wd/hub"), capabilities); Thread.sleep(10000); // Open Notifications Shade driver.openNotifications(); Thread.sleep(2000); // Find Notification WebElement notification = driver.findElement( MobileBy.AndroidUIAutomator("new UiSelector().textContains(\"Your Notification Text\")") ); if (notification != null) { System.out.println("Notification found: " + notification.getText()); notification.click(); // Click notification } else { System.out.println("Notification not found."); } } catch (Exception e) { e.printStackTrace(); } finally { if (driver != null) { driver.quit(); // Close Appium session } } } }
Code Highlights:
- Open Notification Shade:
Thedriver.openNotifications()
method opens the notification shade on the device. - Find Notification by Text:
UseUiSelector
to locate the notification using partial or full text. Modify"Your Notification Text"
to match your specific case. - Interact with Notification:
Once located, you can click the notification or perform other actions as needed.
Automation for iOS
For iOS, Appium capabilities and touch actions are used to interact with the notification shade and validate notifications. Below is the sample Java code for automating push notification testing on iOS.
Steps for iOS:
- Retrieve Appium Capabilities:
Obtain the Appium desired capabilities from the TestGrid Device Cloud for your selected iOS device. - Use Sample Java Code:
package org.example; import io.appium.java_client.ios.IOSDriver; import io.appium.java_client.ios.IOSElement; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.interactions.PointerInput; import org.openqa.selenium.interactions.Sequence; import java.net.URL; import java.time.Duration; import java.util.Collections; public class iOS_PushNotificationTest { public static void main(String[] args) { IOSDriver<IOSElement> driver = null; try { // Set desired capabilities for iOS device DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "iPhone 13 Pro Max"); capabilities.setCapability("platformVersion", "18.1"); capabilities.setCapability("platformName", "iOS"); capabilities.setCapability("automationName", "XCUITest"); capabilities.setCapability("udid", "your-device-udid"); capabilities.setCapability("tg:userToken", "your-user-token"); capabilities.setCapability("appium:noReset", true); // Initialize the driver driver = new IOSDriver<>(new URL("http://{{base_url}}:4723/wd/hub"), capabilities); // Swipe down to open notification center int screenWidth = driver.manage().window().getSize().width; int screenHeight = driver.manage().window().getSize().height; int startX = screenWidth / 2; int startY = (int) (screenHeight * 0.01); // Top of the screen int endY = (int) (screenHeight * 0.6); // Middle of the screen PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger"); Sequence swipe = new Sequence(finger, 1); swipe.addAction(finger.createPointerMove(Duration.ofMillis(0), PointerInput.Origin.viewport(), startX, startY)); swipe.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg())); swipe.addAction(finger.createPointerMove(Duration.ofMillis(800), PointerInput.Origin.viewport(), startX, endY)); swipe.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); driver.perform(Collections.singletonList(swipe)); // Wait for notification center to load Thread.sleep(3000); // Find a notification by its text try { WebElement notification = driver.findElement(By.xpath("//XCUIElementTypeStaticText[contains(@name, 'Your Notification Text')]")); if (notification != null) { System.out.println("Notification found: " + notification.getText()); notification.click(); // Click to open the notification } else { System.out.println("Notification not found."); } } catch (Exception e) { System.out.println("Error locating notification: " + e.getMessage()); } } catch (Exception e) { e.printStackTrace(); } finally { // Quit the driver if (driver != null) { driver.quit(); } } } }
Happy Testing!
By following the steps outlined above, you can perform both manual and automated push notification testing effectively on TestGrid’s real devices. For further assistance, feel free to contact the TestGrid support team.