Reputation: 53
I'm testing a website in Chrome using Python, Appium, Android Emulator and trying to figure out how to stay logged in between multiple tests. The most common answer to similar questions I found is to add --user-data-dir option so I wrote the following:
options.add_argument('--user-data-dir=/data/data/com.android.chrome/app_chrome/Profile')
According to chrome://version it changed profile path to /data/data/com.android.chrome/app_chrome/Profile/Default but still each time a driver instance is created that directory is restored to its default state.
Answering Appium - running browser tests without clearing browser data one person states that "Chromedriver always starts totally fresh, nothing is keeping" while another person from Using selenium: How to keep logged in after closing Driver in Python confirms that in case of OSX "it worked perfectly fine, I don't need to login again".
I also encountered Appium Reset Strategies and tried to add the following to desired capabilities with no luck:
desired_caps['noReset'] = 'true'
desired_caps['fullReset'] = 'false'
Upvotes: 1
Views: 1028
Reputation: 1
Just to add to the above, around 4 months ago a flag was added to chromedriver which allows you to prevent the clearing of the app data directory called "androidKeepAppDataDir".
When setting up your desired caps this worked for me and it stops chromedriver from running the adb shell pm clear command:
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
appCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
appCapabilities.setCapability(MobileCapabilityType.NO_RESET, true);
appCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "chrome");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidKeepAppDataDir", true);
appCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
Upvotes: 0
Reputation: 53
The reason UserDataDir is getting wiped out on Android each time we initialize a driver is because chromedriver.exe runs adb shell pm clear com.android.chrome
a few statements before it launches the browser.
So if you also need this functionality desperately, here's how to fix it:
Follow these instructions to meet all the requirements for building Chromium. Since we will only build chromedriver and not Chromium the building process won't take to too long or require too much RAM, CPU cores or disk space.
Find out your chromedriver version:
$ .\chromedriver.exe --version
ChromeDriver 91.0.4472.19
Update Chromium source code accordingly:
$ git checkout -b fix_clear_app_data 91.0.4472.19
$ gclient sync
Comment out the following lines in chromedriver/chrome/device_manager.cc's Device::SetUp method:
// status = adb_->ClearAppData(serial_, package);
// if (status.IsError())
// return status;
Make sure to build a release version or you won't be able to move chromedriver.exe on its own from the build directory.
Build chromedriver:
$ autoninja -C out/Default chromedriver
Copy out\Default\chromedriver.exe to wherever is appropriate and pass its location to appium:
$ appium --chromedriver-executable /path/to/chromedriver.exe
Upvotes: 2
Reputation: 21
Since you are dealing with browsers, everytime you invoke chrome a new browser will be launched. Appium reset strategies will only work for Native apps.
As a solution you can use the same driver for all of your test rather than closing them.
Upvotes: 0