Search code examples
pythonseleniumselenium-chromedriverappiumpython-appium

How to stay logged in between multiple tests in Appium?


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'

Solution

  • 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:

    1. 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.

    2. Find out your chromedriver version:

       $ .\chromedriver.exe --version
       ChromeDriver 91.0.4472.19
      
    3. Update Chromium source code accordingly:

       $ git checkout -b fix_clear_app_data 91.0.4472.19
       $ gclient sync
      
    4. 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;
      
    5. Make sure to build a release version or you won't be able to move chromedriver.exe on its own from the build directory.

    6. Build chromedriver:

       $ autoninja -C out/Default chromedriver
      
    7. Copy out\Default\chromedriver.exe to wherever is appropriate and pass its location to appium:

       $ appium --chromedriver-executable /path/to/chromedriver.exe