I am writing automation test for my app using appium
- I will send notification to mobile app via external site
- My mobile app will receive notification in an device.
- Is there a way to test to device notification for Android using Appium?
I am writing automation test for my app using appium
no problem.
1 - first you need open notification bar and clear it (for sure that no other notifications in list can affect your test)
to open notification bar use: driver.openNotifications()
to find clear all notification button use id: com.android.systemui:id/clear_all_button
for return into application use either native back button or e.g. try {driver.runAppInBackground(1);} catch (Exception e) {//ignore}
2 - check is your app shows notification while in foreground. if NOT = send application to background first before open notification bar.
send app into background: driver.sendKeyEvent(AndroidKeyCode.HOME)
3 - now send notification to phone -> open notification bar -> locate your message -> tap it
Everything is explained above
@Aleksei @amitjaincoer191 Thanks for your answers. Its working for me.
Till the part to opening notification shade there isn’t any problem. But whenever I try to locate the notification of my app using the text driver.findElement(By.xpath("//android.widget.TextView[@text='TitleText']"));
I am not able to click it because this textview is not clickable. Also, whenever I try to click or tap it appium crashes. error: uncaughtException: Cannot read property 'sockets' of null date
Is there any solution for finding your notification item and click it?
so code is:
//open notification
((AndroidDriver) driver).openNotifications();
sleep(1); //wait while notifications are playing animation to appear to avoid missed taps
nativeNotificationPage = new NativeNotificationPage(driver);
assertTrue("Native notification page is NOT loaded", nativeNotificationPage.isNativeNotificationPage());
int itemsListSize = nativeNotificationPage.getLastItemsContentSize();
String title, text;
int notificationItemNum = 0;
for (int i = 0; i <= itemsListSize; i++) {
title = nativeNotificationPage.getItemTitle(i);
text = nativeNotificationPage.getItemText(i);
System.out.println(" notification title is: " + title);
System.out.println(" notification text is: " + text);
if (title.equals("SOME_TEXT")) {
notificationItemNum = i;
break;
}
}
messagesPage = nativeNotificationPage.tapItemTitle(notificationItemNum); //messagesPage = our client messages screen
public void sleep(int sec) {
try{Thread.sleep(sec*1000);}catch(Exception e){}
}
public class NativeNotificationPage extends Page {
@FindBy(id = "com.android.systemui:id/notification_panel")
private List<WebElement> notificationPanel;
//settings data
@FindBy(id = "com.android.systemui:id/clear_all_button")
private List<WebElement> clearAllBtn;
//last items
@FindBy(id = "com.android.systemui:id/latestItems")
private List<WebElement> lastItemsContainer;
//events data
@FindBy(id = "android:id/status_bar_latest_event_content")
private List<WebElement> lastItemsContent;
@FindBy(id = "android:id/title")
private List<WebElement> itemTitle;
String itemTitle_Locator_Text = "android:id/title";
@FindBys({
@FindBy (id = "android:id/big_text"),
@FindBy (id = "android:id/text")
})
private List<WebElement> itemText;
String itemText_Phone_Locator_Text = "android:id/text";
String itemText_Tablet_Locator_Text = "android:id/big_text";
@FindBy(id = "android:id/time")
private List<WebElement> itemTime;
public NativeNotificationPage(WebDriver driver) {
super(driver);
}
public boolean isNativeNotificationPage() throws Exception {
boolean bool;
setFastLookTiming();
bool = !notificationPanel.isEmpty();
setDefaultTiming();
return bool;
}
public boolean isClearAllBtnLoaded() {
boolean bool;
setLookTiming(3);
bool = !clearAllBtn.isEmpty();
setDefaultTiming();
return bool;
}
public int getLastItemsContentSize() {return lastItemsContent.size();}
public String getItemTitle(int num) {return lastItemsContent.get(num).findElement(By.id(itemTitle_Locator_Text)).getText();}
public String getItemText(int num) {
//System.out.println(lastItemsContent.get(num).findElements(MobileBy.className("android.widget.TextView")).size());
if (isPhone())
return lastItemsContent.get(num).findElements(MobileBy.className("android.widget.TextView")).get(2).getText();
else {
setLookTiming(3);
if (lastItemsContent.get(num).findElements(MobileBy.id(itemText_Tablet_Locator_Text)).isEmpty()) {
setDefaultTiming();
return lastItemsContent.get(num).findElement(MobileBy.id(itemText_Phone_Locator_Text)).getText();
} else {
setDefaultTiming();
return lastItemsContent.get(num).findElement(MobileBy.id(itemText_Tablet_Locator_Text)).getText();
}
}
}
public void tapClearAllBtn() {tapElement(clearAllBtn.get(0));}
public MessagesPage tapLastItemsContent(int num) {
tapElement(lastItemsContainer.get(num));
return new MessagesPage(driver);
}
public MessagesPage tapItemTitle(int num) {
tapElement(lastItemsContent.get(num));
return new MessagesPage(driver);
}
}
Thanks for the code Aleksei. Can you please share the codes for messagepage class and page super class also the setlooktiming methods?
Whenever I open notifications shade and try to findElement from there Appium server just crashes
error: uncaughtException: Cannot read property ‘sockets’
@Karunakaran_M How can we send the notification from an external site?
You can use free messaging apps like free2sms to sms on the device and then test the notification for the same
Can you try by changing your xpath syntax by //td[text()=‘UserID’] as () closing bracket is missing in your path. Or check with ‘contains’ xpath.
Thanks Aleksei. … it works here