[Android] how to clear app data before test

Hi, Im pretty new to Appium.
I want the applications data to be cleared before each test.
How can I accomplish this?
I thought this was done by default [“no-reset”," false"];
but my app starts where it left off each time and I have to manually clear the app data from the application settings on the device.
Thanks

You can run the uninstall command line…
adb uninstall YOUR_APP_PACKAGE

Thanks for the response Asi,
I do not want to uninstall the package but clear the data before my test as to always start at the splash screen.
Thanks

‘adb shell pm clear my.wonderful.app.package’

Thanks wreed,
is there a way to implement this in a test?
I was thinking it would be a capability setting;
e.g. capabilities.setCapability(“no-reset”," false")
Thanks again.

Short answer: Yes!

Long answer: Your testing should not be dependent on Appium, but rather on your language of choice and the testing framework that you use. You don’t give either, so I’ll use mine:

I use Ruby, my testing framework is rspec, therefore I create a method in spec_helper.rb:

def clear_data
 `adb shell pm clear my.wonderful.app.package`
end

Now in my test I make sure to do this:

before(:all) do
 clear_data
end

So if I have tests that don’t need to clear data, no problem. Otherwise I’ll clear it before my tests. Note that I could also do a before(:each) if I wanted to clear data between tests in a suite.

Note that this is an oversimplified example to keep the discussion on point.

1 Like

If you have the right capabilities, Appium will install and start your application before execution and uninstall it afterwards. See http://appium.io/slate/en/master/?ruby#server-flags for more details.

In our implementation, we start up the appium server and execute all of our test cases, so each of our test cases controls whether or not they need the data cleared exactly as @wreed described.

The downside of this implementation is that you’ve created your own adb module. If you are going to use a device solution like Sauce Labs or BitBar, they may not have similar facilities to do the same.

1 Like

Thanks very much,
I am using Java with JUnit.
I could try something similar.
So you suggest to create a method that executes a shell command before the test?

Yes, JUnit also provides @BeforeClass. You can specify before all or before each in that framework.

Yes, You can Create a class called ADB In ADB class …following are the methods u can create.
1)Check whether Android Home is available on System environment.
2)Check the OS part
3)Execute the command
4)In command prompt pass Commad + Device id

public static String Get_Android(){

	if(Android_Home==null ) {
		Android_Home=System.getenv("Android_Home");
	if(Android_Home==null) throw new RuntimeErrorException("Failed to find android home","Make sure environment variable is set");
		}
	return Android_Home;
	
}


public static String getos(){
	if(os==null) os= System.getenv("os.name");
	return os;			
}

public static boolean iswindows(){
	return getos().startsWith("Windows");
	
}

//*****************************************************Run command *******************************************88

public static String runcommand(String command){
	
	String output=null;
	try{
		Scanner scan = new Scanner(Runtime.getRuntime().exec(command).getInputStream()).useDelimiter("//A");			
	}
	catch (IOException e){
		throw new RuntimeException(e.getMessage());	
	}
	return output;		

===============================

 def reset(self: T) -> T:
        """Resets the current application on the device.

        Returns:
            Union['WebDriver', 'Applications']: Self instance
        """
        self.execute(Command.RESET)
        return self