Search code examples
androidaws-device-farm

Unable to open and close notifications panel with AWS Device farm


My test needs to open and close the notifications panel and go back to the app from an Android device (Moto G4 Android 7 - AWS device farm). So I'm calling the following method in my test

androidActivity.openAndCloseNotifications();

where

public void openAndCloseNotifications() throws InterruptedException{ 
    TimeUnit.SECONDS.sleep(3);
    driver.openNotifications();
    TimeUnit.SECONDS.sleep(3);
    driver.pressKeyCode(AndroidKeyCode.BACK);          
} 

The test passes locally with the device but when I run it on AWS device farm I can see that the notifications panel is never open therefore the test fails. At this point does AWS device farm allow to interact with notifications panel? if not, how could I deal with it? Because this would be a problem for most of our tests. Thanks in advance!

This is the test that it's failing, failing at line androidActivity.openAndCloseNotifications(); :

    public boolean run() throws Exception {
    MainActivity mainActivity = new MainActivity(driver);
    AndroidActivity androidActivity = new AndroidActivity(driver);
    LoginActivity loginActivity = new LoginActivity(driver);



    try {
    if(device.getCapability("manufacturer")=="Huawei")
    { 
        driver.closeApp();
        driver.launchApp();
        loginActivity.registerApp("whateverhere");
        androidActivity.openAndCloseNotifications();
        mainActivity.verifyAppIsReady();
        assertTrue(mainActivity.check());
        return true;

    }

    else {

        driver.closeApp();
        driver.launchApp();
        loginActivity.registerApp("whateverhere");
        androidActivity.openAndCloseNotifications();
        mainActivity.verifyAppIsReady();
        System.out.println("Verified Stop button is displayed");
        assertTrue(mainActivity.check());
        System.out.println("Verified title is displayed");
        return true;
        }
    }

    catch(Exception e) {
        System.out.println(e);
        return false;
    }

}

Logs From AWS Device farm (there are no failures locally) java.lang.AssertionError: expected [true] but found [false]\n\tat org.testng.Assert.fail(Assert.java:94)\n\tat org.testng.Assert.failNotEquals(Assert.java:494)\n\tat org.testng.Assert.assertTrue(Assert.java:42)\n\tat org.testng.Assert.assertTrue(Assert.java:52)\n\tat com.OneMeter.Robirdo.TestSequence1.test1(TestSequence1.java:48)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)\n\tat org.testng.internal.Invoker.invokeMethod(Invoker.java:714)\n\tat org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)\n\tat org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)\n\tat org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)\n\tat org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)\n\tat org.testng.TestRunner.privateRun(TestRunner.java:767)\n\tat org.testng.TestRunner.run(TestRunner.java:617)\n\tat org.testng.SuiteRunner.runTest(SuiteRunner.java:348)\n\tat org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)\n\tat org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)\n\tat org.testng.SuiteRunner.run(SuiteRunner.java:254)\n\tat org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)\n\tat org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)\n\tat org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)\n\tat org.testng.TestNG.runSuitesLocally(TestNG.java:1149)\n\tat org.testng.TestNG.run(TestNG.java:1057)\n\tat org.testng.TestNG.privateMain(TestNG.java:1364)\n\tat org.testng.TestNG.main(TestNG.java:1333)\n\n


Solution

  • Tested this locally with a OnePlus 3T and it was successful

    Capabilities:

    capabilities.setCapability("app", "C:\\Users\\james\\Desktop\\stackoverflow\\question_50172058_unable-to-open-and-close-notifications-panel-with-aws-device-farm\\testofopenNotifications\\app-debug.apk");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("deviceName", "OnePlus");
    

    Appium version: 1.7.1

    Appium server output:

    [info] [HTTP] --> POST /wd/hub/session/51cf9f0e-2106-4e34-b0d5-7df3caebc223/appium/device/open_notifications {}
    [debug] [MJSONWP] Calling AppiumDriver.openNotifications() with args: ["51cf9f0e-2106-4e34-b0d5-7df3caebc223"]
    [debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"openNotification","params":{}}[debug] [AndroidBootstrap] Received command result from bootstrap
    [debug] [MJSONWP] Responding to client with driver.openNotifications() result: true
    [info] [HTTP] <-- POST /wd/hub/session/51cf9f0e-2106-4e34-b0d5-7df3caebc223/appium/device/open_notifications 200 328 ms - 76 
    

    After packaging the tests using the assembly and pom from the example tests using command mvn clean package -DskipTests=true this is the result from device farm using the Moto G4 Android 7:

    Appium server version: 1.7.1 Used an empty desired capabilities object as described here

    is successful and opens the notifications. See video

    Appium server logs:

    2018-05-05 16:55:06:839 - [HTTP] --> POST /wd/hub/session/c70b3b24-b7d6-4c6b-a7e2-d4ff5e91aa22/appium/device/open_notifications {}
    2018-05-05 16:55:06:868 - [debug] [MJSONWP] Calling AppiumDriver.openNotifications() with args: ["c70b3b24-b7d6-4c6b-a7e2-d4ff5e91aa22"]
    2018-05-05 16:55:06:872 - [debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"openNotification","params":{}}
    2018-05-05 16:55:06:888 - [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"openNotification","params":{}}
    2018-05-05 16:55:06:889 - [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
    2018-05-05 16:55:06:890 - [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: openNotification
    2018-05-05 16:55:06:891 - [debug] [AndroidBootstrap] Received command result from bootstrap
    2018-05-05 16:55:06:893 - [debug] [MJSONWP] Responding to client with driver.openNotifications() result: true
    2018-05-05 16:55:06:907 - [HTTP] <-- POST /wd/hub/session/c70b3b24-b7d6-4c6b-a7e2-d4ff5e91aa22/appium/device/open_notifications 200 55 ms - 76 
    

    Here is the test method I executed:

    @Test public void testOpenNotifications(){
            //docs: https://github.com/appium/appium/blob/62700d3b1dc0edd985502cc1279747a782f3ee74/docs/en/commands/device/system/open-notifications.md
            driver.openNotifications();
        }
    

    I thought that the code calling sleep might be causing an issue but that appears to be working as well. see video

    code:

    @Test public void testOpenNotificationsWithSleep() throws InterruptedException{
            TimeUnit.SECONDS.sleep(3);
            driver.openNotifications();
            TimeUnit.SECONDS.sleep(3);
        }
    

    Would you please be able to share your appium server logs from the execution in Device Farm? It will show the response from UIAutomator or UIAutomator2 and we can go from there.

    Here is my full test class for comparison:

    package Tests;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.Test;
    
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.android.AndroidDriver;
    
    public class OpenNotificationsTest{
    
         /**
         * Make the driver static. This allows it to be created only once
         * and used across all of the test classes.
         */
        public static AndroidDriver<MobileElement> driver;
    
        /**
         * This method runs before any other method.
         *
         * Appium follows a client - server model:
         * We are setting up our appium client in order to connect to Device Farm's appium server.
         *
         * We do not need to and SHOULD NOT set our own DesiredCapabilities
         * Device Farm creates custom settings at the server level. Setting your own DesiredCapabilities
         * will result in unexpected results and failures.
         *
         * @throws MalformedURLException An exception that occurs when the URL is wrong
         */
        @BeforeSuite
        public void setUpAppium() throws MalformedURLException {
    
            final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
    
            URL url = new URL(URL_STRING);
    
            //Use a empty DesiredCapabilities object fro device farm
            DesiredCapabilities capabilities = new DesiredCapabilities();
    
            //local capabilities
            capabilities.setCapability("app", "C:\\Users\\james\\Desktop\\stackoverflow\\question_50172058_unable-to-open-and-close-notifications-panel-with-aws-device-farm\\testofopenNotifications\\app-debug.apk");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("deviceName", "OnePlus");
    
            driver = new AndroidDriver<MobileElement>(url, capabilities);
    
            //Use a higher value if your mobile elements take time to show up
            driver.manage().timeouts().implicitlyWait(35, TimeUnit.SECONDS);
        }
    
        @Test public void testOpenNotifications(){
            //docs: https://github.com/appium/appium/blob/62700d3b1dc0edd985502cc1279747a782f3ee74/docs/en/commands/device/system/open-notifications.md
            driver.openNotifications();
        }
    
        @Test public void testOpenNotificationsWithSleep() throws InterruptedException{
            TimeUnit.SECONDS.sleep(3);
            driver.openNotifications();
            TimeUnit.SECONDS.sleep(3);
        }
    
        /**
         * Always remember to quit
         */
        @AfterSuite
        public void tearDownAppium() {
            driver.quit();
        }
    
        /**
         * Restart the app after every test class to go back to the main
         * screen and to reset the behavior
         */
        @AfterClass
        public void restartApp() {
            driver.resetApp();
        }
    }
    

    Hope to hear from you soon.

    -James