Dealing with Alerts and Popups in Appium – DZone – Uplaza

It’s common for any cellular utility to indicate alerts and popups. These alerts and popups immediate customers to grant varied permissions, like contacts, notifications, and so forth. Nevertheless, they should be dealt with when performing cellular utility testing. Although there are numerous approaches to dealing with these interactions, choosing the proper instrument and method is vital to make sure a seamless cellular app testing expertise. 

Dealing with alerts and popups in Appium is crucial for reaching environment friendly take a look at execution, enhanced take a look at protection, and extra. On this Appium tutorial, let’s perceive the several types of alerts and popups in Appium and tips on how to deal with them in our cellular automated assessments.

What Is Appium?

Appium is an open-source take a look at automation framework used to jot down automated assessments for cellular functions. It really works like a shopper/server setup, the place the server launches completely different cellular apps, and the shopper is used to jot down the take a look at scripts within the desired programming languages. 

It helps a number of languages like Java, JavaScript, Python, and so forth., enabling testers to jot down take a look at scripts for Android and iOS environments. This makes it an amazing answer to automate the cellular app testing course of.

The Appium structure for Android and iOS is proven under. 

Android

iOS

Furthermore, Appium provides superior options like automating contact actions, swipe, zoom, pinch, and so forth., which makes it an amazing selection for cellular automation assessments. It helps bodily and digital gadgets in order that testers can run the assessments on actual and emulated cellular gadgets.

If you’re seeking to get began with Appium for cellular app testing, try the under cellular app testing tutorial that covers tips on how to carry out Appium automation with examples.

Alerts and Popups: An Overview

Earlier than dealing with alerts and popups in Appium, let’s first perceive what they’re and the way they seem in Android and iOS functions.

  • Alerts: These are small home windows that seem on the display screen to show a message or affirmation. They sometimes have restricted choices, like “OK” or “Cancel.”
  • Popups: Separate home windows that seem on prime of the present app or net web page. Popups could be extra complicated, containing types, movies, or different interactive components.

Alerts in Android

Under is an instance of alerts from the Proverbial app by LambdaTest. 

You may as well discover the alert notifications showing within the notification panel. 

Popups in Android

Under is an instance of popups asking for permission from the consumer. 

Alerts in iOS

Popups in iOS

Why Deal with Alerts and Popups?

Within the case of popups, when the cellular utility begins, the main focus is about on the popup window, and till the popup is accepted or dismissed, the main focus will not be set on the precise utility. 

These popups should be dealt with to maneuver the management to the app so we are able to simulate the precise consumer actions on the cellular utility. The identical factor applies to alerts.

We will deal with alerts and popups within the following methods.

  • Grant all permission for the Android utility.
  • Enable or deny all permissions for the iOS utility.
  • Enable or deny the alert/popup by finding the factor and clicking on it accordingly.

Dealing with alerts and popups is equally vital and significant when testing cellular functions. Nevertheless, it’s a tedious activity when executed manually. To scale back guide efforts, Appium, one of many in style cellular app testing instruments, gives a variety of functionalities and strategies to deal with alerts and popups in an automatic means. 

Within the subsequent part, we’ll have a look at tips on how to deal with alerts and popups in Appium.

How To Deal with Alerts and Popups in Appium

Earlier than demonstrating alerts and popups in Appium, let’s have a look at the instruments utilized in writing and working the assessments:

  • Programming language: Java
  • Cellular automation framework: Appium (Server Model 2.5.4, Appium Java shopper – 9.2.2)
  • Check runner: TestNG
  • Construct instrument: Maven
  • Software beneath take a look at: Android Proverbial app and iOS Proverbial app

To run the assessments, we’ll leverage cloud-based testing platforms like LambdaTest. It’s an AI-powered take a look at orchestration and execution platform that allows you to carry out cellular automation testing at scale on a web-based gadget farm of actual Android and iOS gadgets. Apart from Appium, you can even automate cellular apps utilizing frameworks like Espresso and XCUITest.

Check Situation

Let’s focus on the take a look at state of affairs to exhibit dealing with alerts and popups in Appium.

  1. Upon loading the applying, step one is to simply accept any preliminary popup alert which will seem. This ensures that the automated take a look at begins with out interruption.

2. Click on on the hamburger menu (three strains) on the left prime to open the menu. Click on on the Push Notifications menu. It is going to generate an alert, which will probably be displayed on the prime of the applying. We have to test if this alert is generated.

3. Subsequent, we’ll confirm the textual content of the alert notification by opening the notification panel.

Setup

First, create a Maven undertaking and add the Appium Java shopper and TestNG dependencies. 

The variations of the dependencies are set in a separate block. 

When the app begins, a popup message asks the consumer to supply permission to take footage and report movies. We will solely use the app as soon as we settle for this popup. This helps keep the variations, so if we have to replace them, we are able to do it simply with out looking out by means of the pom.xml file. 



   4.0.0

   io.github.mfaisalkhatri
   react-native-app-mobile-testing
   1.0-SNAPSHOT

   
       9.2.2
       7.9.0
       1.18.32
       3.13.0
       3.2.5
       2.16.0
       17
       UTF-8
       testng.xml
       -Dfile.encoding=UTF-8 -Xdebug -Xnoagent
       UTF-8
   

   
       
       
           org.testng
           testng
           ${testng-version}
           take a look at
       
       
       
           io.appium
           java-client
           ${appium-java-client-version}
       
       
       
           org.projectlombok
           lombok
           ${lombok-version}
           offered
       
       
       
           commons-io
           commons-io
           ${commons-io.model}
       
   

   
       
           
               org.apache.maven.plugins
               maven-compiler-plugin
               ${maven.compiler.model}
               
                   ${java.launch.model}
                   ${maven.supply.encoding}
                   true
               
           
           
               org.apache.maven.plugins
               maven-surefire-plugin
               ${surefire-version}
               
                   
                       
                           take a look at
                       
                   
               
               
                   false
                   
                       
                           usedefaultlisteners
                           false
                       
                   
                   
                       ${suite-xml}
                   
                   ${argLine}
               
           
       
   

As soon as the dependencies are up to date, we’ll proceed to jot down the automated assessments.

Implementation

On this part, we’ll have a look at tips on how to deal with alerts and popups in Android and iOS functions.

Dealing with Alerts within the Android Software

As talked about earlier within the take a look at state of affairs, after the applying opens, we’ll click on on the hamburger menu to open the left menu bar. Then, we’ll click on on the Push Notifications menu. 

It is going to present a push notification alert on the highest of the display screen. 

We’ll test if the alert is displayed. Lastly, the notification textual content will probably be verified by opening the notification panel and swiping down the display screen. 

Right here is the @Check technique:

public class ProverbialAppNotificationTests extends BaseTest {

   @Check
   public void testNotification () {
       HomePage homePage = new HomePage (driverManager);
       homePage.openMenu ();
       homePage.clickPushNotificationMenu ();
       NotificationPage notificationPage = new NotificationPage (driverManager);
       notificationPage.openNotificationPanel ();
       assertEquals (notificationPage.getFirstNotificationTitle (), "Test Notification");
       assertEquals (notificationPage.getFirstNotificationText (), "Please enjoy this notification");
   }

}

The automation script written to check the alert is kind of clear. The next strains of code will open the hamburger menu from the HomePageClick on on the Push Notification hyperlink to generate a brand new alert notification and show it on the prime of the app. 

Subsequent, the next line of code will open the notification panel and assert the textual content of the alert message.

The code snippet under helps open the notification panel.

The code snippet reveals the working of the openNotificationPanel() technique.

Notice: The openNotifications() technique is on the market for AndroidDriver solely, nevertheless it doesn’t work with iOSDriver

Dealing with Popups within the Android Software

You possibly can deal with popups utilizing the Appium desired capabilities autoGrantPermissions to trueThis functionality will robotically grant all of the permissions to the Android app after it’s put in on the respective gadget. 

This functionality could be added to the automated take a look at configuration utilizing the DesiredCapabilities class. Appium makes use of W3C Protocol with its newest variations. Therefore, we are able to set these capabilities utilizing the W3C protocol. 

The specified capabilities could be set as follows:

personal HashMap getLambdaTestOptions() {

   ultimate HashMap ltOptions = new HashMap();
   ltOptions.put("w3c", true);
   ltOptions.put("platformName", this.platformName);
   ltOptions.put("platformVersion", this.platformVersion);
   ltOptions.put("deviceName", this.deviceName);
   ltOptions.put("app", this.app);
   ltOptions.put("build", this.buildName);
   ltOptions.put("name", this.testName);
   ltOptions.put("autoGrantPermissions", true);
   ltOptions.put("isRealMobile", true);
   ltOptions.put("visual", true);
   ltOptions.put("console", true);
   ltOptions.put("devicelog", true);
   ltOptions.put("plugin", "java-testNG");

   return ltOptions;
}

You should utilize the LambdaTest Automation Capabilities Generator, which turns out to be useful for setting the specified capabilities. It helps all of the capabilities and gives an auto-generated code as we set the capabilities from the UI. It additionally helps the most recent Appium 2 model with W3C Protocol. 

Wanting on the code above, you’ll discover that we’ve used the next line of code to grant all permissions.

Notice: If the noReset functionality is about to true, the autoGrantPermissions functionality doesn’t work. Therefore, it ought to be famous that we don’t present the noReset functionality together with autoGrantPermissions. 

Dealing with Alerts within the iOS Software

Let’s now write a take a look at to confirm the alert within the iOS utility. We’ll carry out the identical steps we did for the Android utility. 

Right here is the @Check technique:

@Check
public void notificationTest () {
   clickOn (homePage.notificationBtn ());
   assertTrue (homePage.notificationBar ()
       .isDisplayed ());
   NotificationPage notificationPage = new NotificationPage (driverManager);
   notificationPage.openNotificationPanel ();
   assertTrue (notificationPage.getNotificationText ()
       .accommodates ("Test Notification, Please enjoy this notification"));

}

We’ll click on the notification button to say that the alert notification is current. The next strains of code assist us obtain this: 

Subsequent, we’ll open the notifications panel and test if it accommodates the textual content Check Notification, Please get pleasure from this notification.

As talked about earlier, the openNotifications() technique is on the market for AndroidDriver solely. Therefore we must create a technique to carry out a swipe on the iOS gadget and open the notifications panel. 

Right here is the strategy that may carry out the swipe motion:

public void openNotificationPanel () {
   Thread.sleep (5000);
   var screenSize = driverManager.getDriver ()
       .handle ()
       .window ()
       .getSize ();
   var yMargin = 5;
   var xTop = screenSize.width;
   var prime = new Level (xTop, yMargin);
   var backside = new Level (xTop, screenSize.top - yMargin);

   PointerInput finger = new PointerInput (PointerInput.Variety.TOUCH, "finger");
   Sequence swipe = new Sequence (finger, 1);
   swipe.addAction (
       finger.createPointerMove (Period.ofMillis (0), PointerInput.Origin.viewport (), prime.x, prime.y));
   swipe.addAction (finger.createPointerDown (PointerInput.MouseButton.LEFT.asArg ()));
   swipe.addAction (
       finger.createPointerMove (Period.ofMillis (1000), PointerInput.Origin.viewport (), backside.x, backside.y));
   swipe.addAction (finger.createPointerUp (PointerInput.MouseButton.LEFT.asArg ()));
   driverManager.getDriver ()
       .carry out (Record.of (swipe));

}

We have to get the display screen measurement for setting the highest and backside coordinates handed as parameters to the Level class. The underside of the screens is derived utilizing the display screen width and (top — the yMargin), which is taken as hardcoded 5. 

The PointerInput class is used to outline the finger-touch motion. Utilizing Sequence class, we outline the swipe interplay. 

To carry out a swipe, we’ll first place and press a finger on a particular location on the display screen, carry out a swipe, after which take away our finger from the display screen. 

The next strains of code carry out the identical actions: 

Dealing with Popups within the iOS Software

This may be dealt with utilizing the Appium desired capabilities autoAcceptAlerts to trueThis functionality will robotically settle for all permission popups. 

We will go this functionality within the automated assessments configuration by including the next line:  

Each of the above statements carry out the identical motion to robotically settle for the iOS app-related permission popups.

Likewise, to dismiss a popup, we are able to use the autoDismissAlert and set it to true

The above statements carry out the identical motion to dismiss the iOS app-related permission popups robotically. This functionality could be added to the automated assessments configuration utilizing the DesiredCapabilities class. 

Check Execution

There are two strategies to execute the assessments:

  • Utilizing TestNG immediately from the Built-in Growth Atmosphere (IDE).
  • Utilizing Maven from the Command Line Interface (CLI).

Utilizing TestNG

TestNG is used as a take a look at runner. Therefore, testng.xml has been created, utilizing which we’ll run the assessments by right-clicking on the file and choosing the choice Run ‘…testng.xml’.

Right here is the screenshot of the regionally executed take a look at run utilizing IntelliJ IDE. 

Android

iOS

Utilizing Maven

To run the assessments utilizing Maven, the next steps should be run:

  1. Open the Command Immediate.
  2. Navigate to the foundation folder of the undertaking.
  3. Sort the command on the terminal:
mvn clear take a look at -DLT_USERNAME= -DLT_ACCESS_KEY=

Right here is the screenshot of the take a look at outcomes after assessments had been run regionally utilizing Command Immediate: 

The identical take a look at execution particulars are on the LambdaTest App Automation Dashboard. It gives step-by-step take a look at execution particulars with the platform identify, gadget identify, and model, in addition to a video of the take a look at execution. 

Android

iOS

 

When the popup message displayed within the app will not be dealt with in that case, the popup will seem and stay as it’s. Nevertheless, the take a look at will transfer forward and execute the following line of code to search out the factor and carry out an interplay with it, and can fail. 

We have now the alert notification take a look at, so on this case, when the preliminary popup message for granting permission for the app will not be dealt with, the take a look at would fail with a NoSuchElementException as as a result of popup message, the app couldn’t transfer additional to the following display screen to find the factor. 

Following is the screenshot of the failed take a look at with exception, displayed when the popup will not be dealt with: 

This brings us to the tip of this tutorial on dealing with alerts and popups in Appium. 

Conclusion

In abstract, to deal with popup permission messages within the app utilizing Appium desired capabilities, we have to embrace the next configurations:

  • For Android: AUTO_GRANT_PERMISSIONS
  • For iOS (Settle for Alert): AUTO_ACCEPT_ALERTS
  • For iOS (Dismiss Alert): AUTO_DISMISS_ALERTS 

Concerning alert notifications displayed within the app, we are able to find the respective alert factor and assert its textual content. This may be executed immediately on the alert message or by navigating to the notification panel, studying the textual content displayed, and asserting it accordingly.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version