简体   繁体   中英

Appium: onDestroy() never called after tests

I am a beginner to test automation and using Appium to automate some E2E tests for a native Android application. My test files are written in Java.

I noticed that after test methods within a test class the app does not properly close, ie call onDestroy() or onPause() . I noticed that because I store some data in the SharedPreferences (in onDestroy() & onPause()) and the file stays untouched or does not get created at all.

I tried various combinations of setting the Desired Capability noReset to true/false and invoking driver.quit() or driver.closeApp in the @AfterMethod.

I run the test on an AVD (Pixel 2). onResume() and onCreate() seem to get called properly.

My only solution as of now would be writing the data into the file at another point but I feel like it is really hacky and only serves the purpose of making the test work.

Therefore I would really appreciate some help!

THANKS

This should be the relevant code:

@BeforeMethod
public void setup () throws MalformedURLException {
    DesiredCapabilities caps caps = new DesiredCapabilities();
    caps.setCapability("deviceName", "device");
    caps.setCapability("udid", "emulator-5554");
    caps.setCapability("platformName", "Android");
    caps.setCapability("platformVersion", "8.0");
    caps.setCapability("skipUnlock","true");
    caps.setCapability("noReset",false);
    caps.setCapability("fullReset",true);
    caps.setCapability("app", "/path/to/my/app-debug.apk");
    driver = new AndroidDriver<MobileElement>(new 
    URL("http://0.0.0.0:4723/wd/hub"),caps);
    wait = new WebDriverWait(driver, 10);
}

@Test 
public void someTest() {
}
@Test 
public void anotherTest() {
}

@AfterMethod
public void teardown(){

    driver.closeApp();
} 

EDIT:

I assumed that maybe the storing of the SharedPreferences via commit() is somehow intervened by Appium and changed it to the asynchronous call apply() . However that did not turn out to be the solution and led to the same outcome.

I'd want to recommend you nice free SDK, but first here's example of code how it works with correct closing driver:

Here's the runner class:

public class AndroidExampleRunner {
Runner runner;

    @Before
    public void setup() throws InstantiationException{
        runner = Runner.createAndroid("devtoken","udid","package","activity");
    }

    @After
    public void tearDown() throws IOException{
        runner.close();
    }

    @Test
    public void runTest() throws Exception{
        AndroidExample androidExample = new AndroidExample();
        runner.run(androidExample);

    }
}

It's a test example. ExecutionResult can be also shown in reports if you upload this test to platfrom.

public class AndroidExample implements AndroidTest {
    @Override
    public ExecutionResult execute(AndroidTestHelper helper) throws FailureException {
        AndroidDriver driver = helper.getDriver();
        boolean success = false;
        ExecutionResult executionResult;
        //your test here

        if(success){
            executionResult = ExecutionResult.PASSED;
        }else{
            executionResult = ExecutionResult.FAILED;
        }

        return executionResult;
    }
}

You can find the SDK here You can also upload the test to platform and run it remotely on different machines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM