3

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

Laura
  • 63
  • 1
  • 8
  • 1
    Could you provide the appium server logs and the tests output from print the page source? – jmp May 04 '18 at 12:22
  • 2
    I've added everything you asked for. Locally everything is fine. – Laura May 04 '18 at 13:30
  • There should also be a appum server output log from Device Farm. Would you be able to update this with that log as well? It will show what exactly is happening when the openNotifications method is called. – jmp May 04 '18 at 16:20

2 Answers2

1

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

jmp
  • 2,175
  • 2
  • 17
  • 16
0

It seems that there are no appium server logs in Device farm for the driver.openNotifications(); (Going to attach them anyway).

Using appium 1.7.2 not sure if this is the issue, I slightly modify my code according to your example and I can still reproduce the same issue, no notifications are being opened.

Find my code below (note that there are 2 tests and they are done this way in order to force AWS to run them sequentially as it ignores some of the testNG annotations)

public class TestSequence1 {
public int global1;
public DesiredCapabilities capabilities;
public static AndroidDriver<MobileElement> driver;
private Factory1 testfactory;

@BeforeSuite
public void setUp() throws MalformedURLException {
    capabilities = new DesiredCapabilities();
    global1 = 0;
    URL appiumURL = new URL("http://127.0.0.1:4723/wd/hub");
    capabilities.setCapability("appPackage", "MyAppPackage");
    capabilities.setCapability("appActivity", "MyAppActivity");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("deviceName", "MotoG4");
    //capabilities.setCapability("manufacturer", manufacturer);
    //capabilities.setCapability("noReset", true);
    driver = new AndroidDriver<MobileElement>(appiumURL, capabilities);
    testfactory = new Factory1(capabilities, driver);

}

@Test
public void test1() throws Exception {

    Tests.Test test=testfactory.getNextTest();
    boolean returnvalue = test.run();
    assertTrue(returnvalue);

}

@Test
public void test2() throws Exception {

    Tests.Test test=testfactory.getNextTest();
    boolean returnvalue = test.run();
    assertTrue(returnvalue);

}



@AfterSuite
public void tearDown() {
    driver.quit();
}

@AfterClass
public void restartApp() {
    driver.resetApp();
}

where:

public Factory1(DesiredCapabilities Capabilities, AndroidDriver<MobileElement> Driver) {

    device = Capabilities;
    driver =Driver;
    testNumber=0;

}

public Factory1() {
    // TODO Auto-generated constructor stub
}

public Tests.Test getNextTest() {

    if(testNumber==0)
    {
        test = new BasicTest1(device, driver);
    }
    else if(testNumber==1)
    {
        test = new BasicTest2(device, driver);
    }

    testNumber++;
    return test;
}

and the test that is failing is the following:

public class BasicTest2 extends Tests.Test{
public DesiredCapabilities device;
public AndroidDriver<MobileElement> driver;



public BasicTest2(DesiredCapabilities capabilities, AndroidDriver<MobileElement> driver) {
    device = capabilities;
    this.driver = driver;

}

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

 try{   driver.closeApp();
        driver.launchApp();
        loginActivity.registerApp("password");
        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;
    }

}`

enter image description here

I don't really know what's going wrong as the test is passing locally in few devices with different android versions. Thanks for your help James :)

Laura
  • 63
  • 1
  • 8
  • If you scroll down more on that page there should be a link for the appium server logs. When you can, please attach them. – jmp May 07 '18 at 16:31