Testing app upgrades is an important scenario since new versions of your apps are shipped to customers on a frequent basis. Users upgrade to the latest version of your app and not install it from scratch. In such cases, there might be existing user data that needs to be migrated for the app to work correctly. As a result, testing of app-upgrade is very important to ensure that the app works as expected even after the upgrade.
To test app upgrades, ensure that the upgraded app version has the same signatures and bundle ID as the application under test.
Before starting test execution, you first need to upload the app to be upgraded (newer version) using REST API similar to your app under test. The REST API response will return an app_url for each successful app upload. Use the app_url value (returned on uploading the app) to set the browserstack.midSessionInstallApps capability in your Appium test script.
If you are using BrowserStack SDK, you can set the following capability within the browserstack.yml file:
Capability
Description
Values
midSessionInstallApps
Set this capability if you want to install app(s) in between a test session. Upload your apps to BrowserStack servers using REST API. Use the app_url value returned as a result of the upload request to set this capability.
The app_url returned on successful upload. Example:["bs://<hashed app-id>", "bs://<hashed app-id>"]
BrowserStack SDK is a plug-n-play solution that takes care of all the integration steps for you. Using the BrowserStack SDK is the recommended integration method for your project. To know more, visit the SDK core concepts page.
Set this capability if you want to install app(s) in between a test session. Upload your apps to BrowserStack servers using REST API. Use the app_url value returned as a result of the upload request to set this capability.
The app_url returned on successful upload. Example:["bs://<hashed app-id>", "bs://<hashed app-id>"]
Capability
Description
Values
browserstack.midSessionInstallApps
Set this capability if you want to install app(s) in between a test session. Upload your apps to BrowserStack servers using REST API. Use the app_url value returned as a result of the upload request to set this capability.
The app_url returned on successful upload. Example:["bs://<hashed app-id>", "bs://<hashed app-id>"]
Test app upgrades
You can now make use of Appium’s install app command. It will allow you to install and launch the upgraded version of your app in between your Appium test session and continue with your test flow with this newer version of the app.
The detailed steps to achieve this are:
Upload the app version say v1.0 and set the app_url value (returned on uploading the app) in app capability. This will be the application under test(AUT) for your Appium test.
Now, upload the upgrade version of app (say v1.1). You need to set the app_url value (returned on uploading the app) in midSessionInstallApps capability in your test script prior to running the test as mentioned above.
If you are using the JSON Wire Protocol, make sure you set the app_url value in browserstack.midSessionInstallApps capability in your test script prior to running the test as mentioned above.
Start your Appium test execution.
In the middle of the test you can install and upgrade the app version using Appium’s install app command. You can launch the installed app using the Start Activity command.
// Use app_url value passed in the "browserstack.midSessionInstallApps" capability
driver.executeScript("mobile:installApp",ImmutableMap.of("appPath","bs://<hashed app-id>"));HashMap<String,String> startActivityArgs =newHashMap<>();
startActivityArgs.put("intent","com.browserstack.sample.MainActivity");
startActivityArgs.put("package","com.browserstack.sample");
driver.executeScript("mobile:startActivity", startActivityArgs);
// Use app_url value passed in the "browserstack.midSessionInstallApps" capability
driver.execute("mobile: installApp",{appPath:"bs://<hashed app-id>"});
driver.execute("mobile: startActivity",{intent:"com.browserstack.sample.MainActivity",package:"com.browserstack.sample"});
// Use app_url value passed in the "browserstack.midSessionInstallApps" capabilityDictionary<string,string> installAppArgs =newDictionary<string,string>();
installAppArgs.Add("appPath","bs://9fg0rt302be241e6524ebc67253ecdee266343");((IJavaScriptExecutor)driver).ExecuteScript("mobile:installApp", installAppArgs);Dictionary<string,string> startActivityArgs =newDictionary<string,string>();
startActivityArgs.Add("intent","com.browserstack.sample.MainActivity");
startActivityArgs.Add("package","com.browserstack.sample");((IJavaScriptExecutor)driver).ExecuteScript("mobile: startActivity", startActivityArgs);
// Use app_url value passed in the 'browserstack.midSessionInstallApps' capability$driver->installApp('bs://<hashed app-id>');$driver->startActivity(array('appPackage'=>'com.browserstack.sample','appActivity'=>'com.browserstack.sample.MainActivity'));
# Use app_url value passed in the 'browserstack.midSessionInstallApps' capability
driver.execute_script("mobile: installApp",{"appPath":"bs://<hashed app-id>"});
driver.execute_script("mobile: startActivity",{"intent":"com.browserstack.sample.MainActivity","package":"com.browserstack.sample"});
# Use app_url value passed in the 'browserstack.midSessionInstallApps' capability
execute_script("mobile: installApp",{"appPath"=>"bs://<hashed app-id>"})
execute_script("mobile: startActivity",{"intent"=>"com.browserstack.sample.MainActivity","package"=>"com.browserstack.sample"})
Sample Android code snippet
The following code samples explain an app-upgrade test using Appium. A test begins with the app version v1.0 but later gets updated to v1.1 by installing the latest version through the app_url.
packageandroid;importio.appium.java_client.MobileBy;importio.appium.java_client.android.Activity;importio.appium.java_client.android.AndroidDriver;importjava.io.IOException;importjava.net.URL;importorg.junit.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.remote.DesiredCapabilities;importorg.openqa.selenium.support.ui.ExpectedConditions;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassAppUpgradeBS{privateString APP_PKG ="com.browserstack.sample";privateString APP_ACT ="com.browserstack.sample.MainActivity";@Testpublicvoid testAppUpgrade ()throwsIOException{DesiredCapabilities capabilities =newDesiredCapabilities();HashMap<String,Object> browserstackOptions =newHashMap<String,Object>();// Set your access credentials
capabilities.setCapability("bstack:options", browserstackOptions);
capabilities.setCapability("platformName","android");
capabilities.setCapability("platformVersion","9.0");
capabilities.setCapability("deviceName","Google Pixel 3");// Set app_url of the application under test(AUT) which is version v1.0 of app
capabilities.setCapability("app","bs://<hashed app-id>");// Set app_url obtained after uploading the upgrade version v1.1 of app
browserstackOptions.put("midSessionInstallApps",newString[]{"bs://<hashed app-id>"});AndroidDriver driver =newAndroidDriver(newURL("https://hub.browserstack.com/wd/hub"), capabilities);WebDriverWait wait =newWebDriverWait(driver,10);try{//Your test goes with v1.0 goes here// Close the app under test
driver.terminateApp(APP_PKG);
driver.executeScript("mobile:terminateApp",ImmutableMap.of("appId", APP_PKG));// Install the upgrade version of the app// Specify the app_url of the upgrade version(v1.1) of the app that was passed in "browserstack.midSessionInstallApps" capability
driver.executeScript("mobile:installApp",ImmutableMap.of("appPath","bs://<hashed app-id>"));// Launch the app using the package and launcher activity of the appHashMap<String,String> startActivityArgs =newHashMap<>();
startActivityArgs.put("intent", APP_ACT);
startActivityArgs.put("package", APP_PKG);
driver.executeScript("mobile:startActivity", startActivityArgs);//Your test with upgrade version v1.1 goes}finally{
driver.quit();}}}
const{ remote }=require('webdriverio');asyncfunctiontestAppUpgrade(){const capabilities ={
platformName:'android',
platformVersion:'9.0',
deviceName:'Google Pixel 3',
app:'bs://<hashed app-id>','bstack:options':{
midSessionInstallApps:['bs://<hashed app-id>']}};const driver =awaitremote({
capabilities,
hostname:'hub.browserstack.com',
port:443,
path:'/wd/hub',
user:'YOUR_BROWSERSTACK_USERNAME',
key:'YOUR_BROWSERSTACK_ACCESS_KEY'});try{// Your test with v1.0 goes here// Close the app under testawait driver.terminateApp('com.browserstack.sample');await driver.executeScript('mobile:terminateApp',{ appId:'com.browserstack.sample'});// Install the upgrade version of the appawait driver.executeScript('mobile:installApp',{ appPath:'bs://<hashed app-id>'});// Launch the app using the package and launcher activity of the appawait driver.executeScript('mobile:startActivity',{ intent:'com.browserstack.sample.MainActivity',package:'com.browserstack.sample'});// Your test with upgrade version v1.1 goes here}finally{await driver.deleteSession();}}testAppUpgrade();
usingSystem;usingOpenQA.Selenium.Appium;usingOpenQA.Selenium.Appium.Enums;usingOpenQA.Selenium.Appium.Service;usingOpenQA.Selenium.Remote;classProgram{staticvoidMain(string[] args){TestAppUpgrade();}staticvoidTestAppUpgrade(){var capabilities =newAppiumOptions();
capabilities.AddAdditionalCapability(MobileCapabilityType.PlatformName,"Android");
capabilities.AddAdditionalCapability(MobileCapabilityType.PlatformVersion,"9.0");
capabilities.AddAdditionalCapability(MobileCapabilityType.DeviceName,"Google Pixel 3");
capabilities.AddAdditionalCapability(MobileCapabilityType.App,"bs://<hashed app-id>");
capabilities.AddAdditionalCapability("bstack:options",newDictionary<string,object>{{"midSessionInstallApps",newstring[]{"bs://<hashed app-id>"}}});var serviceUri =newUri("https://hub.browserstack.com/wd/hub");var driver =newAndroidDriver<AppiumWebElement>(serviceUri, capabilities);try{// Your test with v1.0 goes here// Close the app under test
driver.CloseApp();// Install the upgrade version of the app
driver.InstallApp("<hashed app-id>");// Launch the app using the package and launcher activity of the app
driver.ExecuteScript("mobile:startActivity",newDictionary<string,object>{{"intent","com.browserstack.sample.MainActivity"},{"package","com.browserstack.sample"}});// Your test with upgrade version v1.1 goes here}finally{
driver.Quit();}}}
<?phprequire_once('vendor/autoload.php');useFacebook\WebDriver\Remote\RemoteWebDriver;useFacebook\WebDriver\Remote\DesiredCapabilities;functiontestAppUpgrade(){$capabilities=DesiredCapabilities::android();$capabilities->setCapability('platformVersion','9.0');$capabilities->setCapability('deviceName','Google Pixel 3');$capabilities->setCapability('app','bs://<hashed app-id>');$capabilities->setCapability('bstack:options',['midSessionInstallApps'=>['bs://<hashed app-id>']]);$driver=RemoteWebDriver::create('https://hub.browserstack.com/wd/hub',$capabilities,'YOUR_BROWSERSTACK_USERNAME','YOUR_BROWSERSTACK_ACCESS_KEY');try{// Your test with v1.0 goes here// Close the app under test$driver->terminateApp('com.browserstack.sample');$driver->executeScript('mobile:terminateApp',['appId'=>'com.browserstack.sample']);// Install the upgrade version of the app$driver->executeScript('mobile:installApp',['appPath'=>'bs://<hashed app-id>']);// Launch the app using the package and launcher activity of the app$driver->executeScript('mobile:startActivity',['intent'=>'com.browserstack.sample.MainActivity','package'=>'com.browserstack.sample']);// Your test with upgrade version v1.1 goes here}finally{$driver->quit();}}testAppUpgrade();?>
from appium import webdriver
deftest_app_upgrade():
desired_caps ={'platformName':'Android','platformVersion':'9.0','deviceName':'Google Pixel 3','app':'bs://<hashed app-id>','bstack:options':{'midSessionInstallApps':['bs://<hashed app-id>']}}
driver = webdriver.Remote(
command_executor='https://hub.browserstack.com/wd/hub',
desired_capabilities=desired_caps,
username='YOUR_BROWSERSTACK_USERNAME',
access_key='YOUR_BROWSERSTACK_ACCESS_KEY')try:# Your test with v1.0 goes here# Close the app under test
driver.terminate_app('com.browserstack.sample')
driver.execute_script('mobile:terminateApp',{'appId':'com.browserstack.sample'})# Install the upgrade version of the app
driver.execute_script('mobile:installApp',{'appPath':'bs://<hashed app-id>'})# Launch the app using the package and launcher activity of the app
driver.execute_script('mobile:startActivity',{'intent':'com.browserstack.sample.MainActivity','package':'com.browserstack.sample'})# Your test with upgrade version v1.1 goes herefinally:
driver.quit()
test_app_upgrade()
require'appium_lib'deftest_app_upgrade
caps ={
platformName:'Android',
platformVersion:'9.0',
deviceName:'Google Pixel 3',
app:'bs://<hashed app-id>','bstack:options':{ midSessionInstallApps:['bs://<hashed app-id>']}}
opts ={
caps: caps,
appium_lib:{
server_url:'https://hub.browserstack.com/wd/hub',
wait_timeout:30}}
driver =Appium::Driver.new(opts,true).start_driver
begin# Your test with v1.0 goes here# Close the app under test
driver.terminate_app('com.browserstack.sample')
driver.execute_script('mobile:terminateApp',{ appId:'com.browserstack.sample'})# Install the upgrade version of the app
driver.execute_script('mobile:installApp',{ appPath:'bs://<hashed app-id>'})# Launch the app using the package and launcher activity of the app
driver.execute_script('mobile:startActivity',{ intent:'com.browserstack.sample.MainActivity', package:'com.browserstack.sample'})# Your test with upgrade version v1.1 goes hereensure
driver.quit
endend
test_app_upgrade
In case of Android, you can also pass publicly accessible URL as a value in the driver.installApp command. The public URL should have a downloadable Android app (.apk/.aab) file.
packageandroid;importio.appium.java_client.MobileBy;importio.appium.java_client.android.Activity;importio.appium.java_client.android.AndroidDriver;importjava.io.IOException;importjava.net.URL;importorg.junit.Test;importorg.openqa.selenium.By;importorg.openqa.selenium.remote.DesiredCapabilities;importorg.openqa.selenium.support.ui.ExpectedConditions;importorg.openqa.selenium.support.ui.WebDriverWait;publicclassAppUpgradeBS{privateString APP_PKG ="com.browserstack.sample";privateString APP_ACT ="com.browserstack.sample.MainActivity";@Testpublicvoid testAppUpgrade ()throwsIOException{DesiredCapabilities capabilities =newDesiredCapabilities();// Set your access credentials
capabilities.setCapability("browserstack.user","YOUR_USERNAME");
capabilities.setCapability("browserstack.key","YOUR_ACCESS_KEY");
capabilities.setCapability("device","Samsung Galaxy S8");
capabilities.setCapability("os_version","7.0");
capabilities.setCapability("name","Test_app_upgrade");// Set app_url of the application under test(AUT) which is version v1.0 of app
capabilities.setCapability("app","bs://<hashed app-id>");// Set app_url obtained after uploading the upgrade version v1.1 of app
capabilities.setCapability("browserstack.midSessionInstallApps",newString[]{"bs://<hashed app-id>"});AndroidDriver driver =newAndroidDriver(newURL("https://hub.browserstack.com/wd/hub"), capabilities);WebDriverWait wait =newWebDriverWait(driver,10);try{//Your test goes with v1.0 goes here// Close the app under test
driver.terminateApp(APP_PKG);// Install the upgrade version of the app // Specify the app_url of the upgrade version(v1.1) of the app that was passed in "browserstack.midSessionInstallApps" capability
driver.installApp("bs://<hashed app-id>");// Launch the app using the package and launcher activity of the app
driver.activateApp(APP_PKG);//Your test with upgrade version v1.1 goes}finally{
driver.quit();}}}
const{ remote }=require('webdriverio');asyncfunctiontestAppUpgrade(){const capabilities ={
browserstack:{
user:'YOUR_USERNAME',
key:'YOUR_ACCESS_KEY',
device:'Samsung Galaxy S8',
os_version:'7.0',
name:'Test_app_upgrade','browserstack.appium_version':'1.20.2'},
app:'bs://<hashed app-id>','browserstack.midSessionInstallApps':['bs://<hashed app-id>']};const browserstackOptions ={
hostname:'hub.browserstack.com',
port:443,
path:'/wd/hub',
protocol:'https'};const driver =awaitremote({
capabilities,...browserstackOptions
});try{// Your test with v1.0 goes here// Close the app under testawait driver.terminateApp('com.browserstack.sample');// Install the upgrade version of the appawait driver.installApp('bs://<hashed app-id>');// Launch the app using the package and launcher activity of the appawait driver.activateApp('com.browserstack.sample');// Your test with upgrade version v1.1 goes here}finally{await driver.deleteSession();}}testAppUpgrade();
usingSystem;usingOpenQA.Selenium.Appium;usingOpenQA.Selenium.Appium.Android;classProgram{staticvoidMain(string[] args){TestAppUpgrade();}staticvoidTestAppUpgrade(){var options =newAppiumOptions();
options.AddAdditionalCapability("browserstack.user","YOUR_USERNAME");
options.AddAdditionalCapability("browserstack.key","YOUR_ACCESS_KEY");
options.AddAdditionalCapability("device","Samsung Galaxy S8");
options.AddAdditionalCapability("os_version","7.0");
options.AddAdditionalCapability("name","Test_app_upgrade");
options.AddAdditionalCapability("app","bs://<hashed app-id>");
options.AddAdditionalCapability("browserstack.appium_version","1.20.2");
options.AddAdditionalCapability("browserstack.midSessionInstallApps",newstring[]{"bs://<hashed app-id>"});var driver =newAndroidDriver<AndroidElement>(newUri("https://hub.browserstack.com/wd/hub"), options);try{// Your test with v1.0 goes here// Close the app under test
driver.CloseApp();// Install the upgrade version of the app
driver.InstallApp("bs://<hashed app-id>");// Launch the app using the package and launcher activity of the app
driver.ActivateApp("com.browserstack.sample");// Your test with upgrade version v1.1 goes here}finally{
driver.Quit();}}}
<?phprequire_once('vendor/autoload.php');// Assuming you have webdriverio installed via ComposeruseFacebook\WebDriver\Remote\RemoteWebDriver;useFacebook\WebDriver\Remote\DesiredCapabilities;
async functiontestAppUpgrade(){$capabilities=['browserstack'=>['user'=>'YOUR_USERNAME','key'=>'YOUR_ACCESS_KEY','device'=>'Samsung Galaxy S8','os_version'=>'7.0','name'=>'Test_app_upgrade','browserstack.appium_version'=>'1.20.2'],'app'=>'bs://<hashed app-id>','browserstack.midSessionInstallApps'=>['bs://<hashed app-id>']];$browserstackOptions=['hostname'=>'hub.browserstack.com','port'=>443,'path'=>'/wd/hub','protocol'=>'https'];$webDriver=RemoteWebDriver::create('https://hub.browserstack.com/wd/hub',$capabilities,'YOUR_USERNAME','YOUR_ACCESS_KEY');try{// Your test with v1.0 goes here// Close the app under test$webDriver->terminateApp('com.browserstack.sample');// Install the upgrade version of the app$webDriver->installApp('bs://<hashed app-id>');// Launch the app using the package and launcher activity of the app$webDriver->activateApp('com.browserstack.sample');// Your test with upgrade version v1.1 goes here}finally{$webDriver->quit();}}testAppUpgrade();?>
from appium import webdriver
deftest_app_upgrade():
desired_caps ={'browserstack.user':'YOUR_USERNAME','browserstack.key':'YOUR_ACCESS_KEY','device':'Samsung Galaxy S8','os_version':'7.0','name':'Test_app_upgrade','app':'bs://<hashed app-id>','browserstack.appium_version':'1.20.2','browserstack.midSessionInstallApps':['bs://<hashed app-id>']}
driver = webdriver.Remote(
command_executor='https://hub.browserstack.com/wd/hub',
desired_capabilities=desired_caps
)try:# Your test with v1.0 goes here# Close the app under test
driver.terminate_app('com.browserstack.sample')# Install the upgrade version of the app
driver.install_app('bs://<hashed app-id>')# Launch the app using the package and launcher activity of the app
driver.activate_app('com.browserstack.sample')# Your test with upgrade version v1.1 goes herefinally:
driver.quit()
test_app_upgrade()
require'appium_lib'deftest_app_upgrade
caps ={'browserstack.user':'YOUR_USERNAME','browserstack.key':'YOUR_ACCESS_KEY','device':'Samsung Galaxy S8','os_version':'7.0','name':'Test_app_upgrade','app':'bs://<hashed app-id>','browserstack.appium_version':'1.20.2','browserstack.midSessionInstallApps':['bs://<hashed app-id>']}
opts ={
caps: caps,
appium_lib:{
server_url:'https://hub.browserstack.com/wd/hub'}}
driver =Appium::Driver.new(opts,true).start_driver
begin# Your test with v1.0 goes here# Close the app under test
driver.terminate_app('com.browserstack.sample')# Install the upgrade version of the app
driver.install_app('bs://<hashed app-id>')# Launch the app using the package and launcher activity of the app
driver.activate_app('com.browserstack.sample')# Your test with upgrade version v1.1 goes hereensure
driver.quit
endend
test_app_upgrade
In case of Android, you can also pass publicly accessible URL as a value in the driver.installApp command. The public URL should have a downloadable Android app (.apk/.aab) file.
For JSONwire browserstack.midSessionInstallApps and W3C midSessionInstallApps capabilities can also be used to install any other app in between the test session.
Sample iOS code snippet
```csharp
public class BrowserStackSample {
public static void main(String[] args)
throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
// Passing browserstack capabilities inside bstack:options
caps.setCapability("bstack:options", browserstackOptions);
// Set URL of the application under test
caps.setCapability("app", "bs://your_initial_app_url");
browserstackOptions.put("midSessionInstallApps", new String[] { "bs://your_upgraded_app_url" });
// Specify device and os_version for testing
caps.setCapability("deviceName", "iPhone 14");
caps.setCapability("platformName", "ios");
caps.setCapability("platformVersion", "16");
IOSDriver driver = new IOSDriver(new URL("http://hub.browserstack.com/wd/hub"), caps);
driver.terminateApp("bundleID");
driver.installApp("bs://your_upgraded_app_url");
driver.activateApp("bundleID");
driver.quit();
}
```