Not able to capture the screen shot from the browserstack device

Hi
I am using browser stack to test one application but on click of force quit button on the application, the device is not responding to getscreenshot() call.
Getting the below error in logs captured from the browser stack:

2025-3-3 9:29:33:995 REQUEST [2025-3-3 9:29:33:995] POST /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/actions {"actions":[{"type":"pointer","id":"TapTouch","parameters":{"pointerType":"touch"},"actions":[{"type":"pointerMove","duration":0,"origin":"viewport","x":215,"y":543},{"type":"pointerDown","button":0},{"type":"pause","duration":0},{"type":"pointerUp","button":0}]}]}
2025-3-3 9:29:34:433 RESPONSE {"value":null}
2025-3-3 9:29:34:436 REQUEST [2025-3-3 9:29:34:436] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot {}
2025-3-3 9:29:35:408 RESPONSE {"value":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-selenium-ba148c1b14.png"}
2025-3-3 9:29:37:932 REQUEST [2025-3-3 9:29:37:932] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/source {}
2025-3-3 9:29:38:55 RESPONSE {"value":{"error":"invalid element state","message":"The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed","stacktrace":"InvalidElementStateError: The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed\n    at errorFromW3CJsonCode (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)\n    at ProxyRequestError.getActualError (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)\n    at JWProxy.command (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:273:19)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at XCUITestDriver.proxyCommand (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/proxy-helper.js:96:12)\n    at XCUITestDriver.getNativePageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:25:17)\n    at XCUITestDriver.getPageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:21:10)","screen":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-93df8c0ac0.jpeg"}}
2025-3-3 9:29:41:236 REQUEST [2025-3-3 9:29:41:236] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/source {}
2025-3-3 9:29:41:301 RESPONSE {"value":{"error":"invalid element state","message":"The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed","stacktrace":"InvalidElementStateError: The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed\n    at errorFromW3CJsonCode (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)\n    at ProxyRequestError.getActualError (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)\n    at JWProxy.command (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:273:19)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at XCUITestDriver.proxyCommand (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/proxy-helper.js:96:12)\n    at XCUITestDriver.getNativePageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:25:17)\n    at XCUITestDriver.getPageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:21:10)","screen":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-d81e567690.jpeg"}}
2025-3-3 9:29:41:995 REQUEST [2025-3-3 9:29:41:995] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot {}
2025-3-3 9:29:42:996 RESPONSE {"value":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-selenium-aef41de4d3.png"}
2025-3-3 9:29:44:476 REQUEST [2025-3-3 9:29:44:476] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/source {}
2025-3-3 9:29:44:563 RESPONSE {"value":{"error":"invalid element state","message":"The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed","stacktrace":"InvalidElementStateError: The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed\n    at errorFromW3CJsonCode (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)\n    at ProxyRequestError.getActualError (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)\n    at JWProxy.command (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:273:19)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at XCUITestDriver.proxyCommand (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/proxy-helper.js:96:12)\n    at XCUITestDriver.getNativePageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:25:17)\n    at XCUITestDriver.getPageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:21:10)","screen":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-6772329e90.jpeg"}}
2025-3-3 9:29:47:915 REQUEST [2025-3-3 9:29:47:915] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/source {}
2025-3-3 9:29:48:610 RESPONSE {"value":{"error":"invalid element state","message":"The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed","stacktrace":"InvalidElementStateError: The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed\n    at errorFromW3CJsonCode (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)\n    at ProxyRequestError.getActualError (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)\n    at JWProxy.command (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:273:19)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at XCUITestDriver.proxyCommand (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/proxy-helper.js:96:12)\n    at XCUITestDriver.getNativePageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:25:17)\n    at XCUITestDriver.getPageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:21:10)","screen":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-b09c72208e.jpeg"}}
2025-3-3 9:29:47:690 REQUEST [2025-3-3 9:29:47:690] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot {}
2025-3-3 9:29:48:735 RESPONSE {"value":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-selenium-ff03dadb6d.png"}
2025-3-3 9:29:51:757 REQUEST [2025-3-3 9:29:51:757] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/source {}
2025-3-3 9:29:51:799 RESPONSE {"value":{"error":"invalid element state","message":"The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed","stacktrace":"InvalidElementStateError: The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed\n    at errorFromW3CJsonCode (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)\n    at ProxyRequestError.getActualError (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)\n    at JWProxy.command (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:273:19)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at XCUITestDriver.proxyCommand (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/proxy-helper.js:96:12)\n    at XCUITestDriver.getNativePageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:25:17)\n    at XCUITestDriver.getPageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:21:10)","screen":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-ebda9a14b9.jpeg"}}
2025-3-3 9:29:52:635 REQUEST [2025-3-3 9:29:52:635] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot {}
2025-3-3 9:29:53:660 RESPONSE {"value":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-selenium-273e452364.png"}
2025-3-3 9:29:55:285 REQUEST [2025-3-3 9:29:55:285] GET /session/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/source {}
2025-3-3 9:29:55:347 RESPONSE {"value":{"error":"invalid element state","message":"The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed","stacktrace":"InvalidElementStateError: The application under test with bundle id 'Application 'com.mobily.mobilyapp'' is not running, possibly crashed\n    at errorFromW3CJsonCode (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)\n    at ProxyRequestError.getActualError (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)\n    at JWProxy.command (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:273:19)\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at XCUITestDriver.proxyCommand (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/proxy-helper.js:96:12)\n    at XCUITestDriver.getNativePageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:25:17)\n    at XCUITestDriver.getPageSource (/nix/store/h83064gqicshqjg2pmw64xcj7rm7aqgw-appium-1.21.0/node_modules/appium-xcuitest-driver/lib/commands/source.js:21:10)","screen":"https://app-automate.browserstack.com/s3-debug/browserstack-debug-screenshots-prod-aps1/4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019/screenshot-757165b95b.jpeg"}}
2025-3-3 9:29:56:867 STOP_SESSION {"status":0,"sessionId":"4ed4b21867f41adab4c6acabe3f9a8ad7c3e4019","value":{"message":"CLIENT_STOPPED_SESSION"},"errorStack":{}}

After performing this click i could not interect wiht the app any more.
Please suggest.

With this screenshot we see your app NOT running any more. Enable logging to see your crash e.g. showXcodeLog or maybe crash possibly caught with some framework that uses app for reporting crashes (ask developers to check).

@Aleksei - Its confirmed that application is closed on click of button (Force quit now) but once application is closed is it possible to start interacting with the device on browser stack again in the same session ?
Note - InvalidElementStateException is happening during any interaction with the screen after force quit.

I am navigating on same screen without problem.

In one of mine test I am terminating app under test and swiping and tapping on home screen without issues. Both iOS + Android real devices. BrowserStack never tried.

Example with iOS →
211abb59d1a76096

1 Like

@Aleksei - Is it possible to get the Driver.PageSource only when app is running,
In my code what I am observing, If app is closed Driver.PageSource throws
InvalidElementStateException and even if I try to get the page source again i could not find any element while running application on browser stack.

Do not have any issue with iOS. Got response as from main screen

<?xml version="1.0" encoding="UTF-8"?>
<AppiumAUT>
  <XCUIElementTypeApplication type="XCUIElementTypeApplication" name=" " label=" " visible="true" x="0" y="0" width="414" height="896">
    <XCUIElementTypeWindow type="XCUIElementTypeWindow" visible="true" x="0" y="0" width="414" height="896">
      <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" x="0" y="0" width="414" height="896">
        <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="0" y="0" width="414" height="896">
          <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="0" y="0" width="414" height="100"/>
        </XCUIElementTypeOther>
        <XCUIElementTypeOther type="XCUIElementTypeOther" name="Home screen icons" visible="true" x="0" y="0" width="414" height="896">
          <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" x="0" y="0" width="414" height="896">
            <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" x="0" y="0" width="414" height="896">
              <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="828" y="0" width="414" height="896">
                <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="828" y="0" width="414" height="896">
                  <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="828" y="0" width="414" height="896">
                    <XCUIElementTypeOther type="XCUIElementTypeOther" name="dewey-search-bar" visible="false" x="828" y="0" width="414" height="142">
                      <XCUIElementTypeSearchField type="XCUIElementTypeSearchField" name="dewey-search-field" visible="false" x="855" y="70" width="360" height="48">
                        <XCUIElementTypeImage type="XCUIElementTypeImage" name="magnifyingglass" label="Search" visible="false" x="967" y="83" width="23" height="22"/>
                      </XCUIElementTypeSearchField>
                    </XCUIElementTypeOther>
                    <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="828" y="0" width="414" height="896">
                      <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="828" y="0" width="414" height="896">
                        <XCUIElementTypeOther type="XCUIElementTypeOther" name="dewey-pod-view" visible="false" x="828" y="0" width="414" height="896">
                          <XCUIElementTypeOther type="XCUIElementTypeOther" name="dewey-pod-expanded" visible="false" x="828" y="0" width="414" height="896"/>
                        </XCUIElementTypeOther>
                      </XCUIElementTypeOther>
                    </XCUIElementTypeOther>
                  </XCUIElementTypeOther>
                  <XCUIElementTypeOther type="XCUIElementTypeOther" visible="false" x="828" y="0" width="414" height="896"/>
                </XCUIElementTypeOther>
              </XCUIElementTypeOther>
              <XCUIElementTypeIcon type="XCUIElementTypeIcon" visible="true" x="0" y="0" width="0" height="0">
                <XCUIElementTypeIcon type="XCUIElementTypeIcon" value="Widget, Stack" name="Weather" label="Weather" visible="true" x="25" y="68" width="173" height="197">
                  <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" x="27" y="70" width="169" height="183">
                    <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" x="27" y="70" width="169" height="169">
                      <XCUIElementTypeOther type="XCUIElementTypeOther" name="widget-stack:&lt;Weather,Maps,Notes&gt;" visible="true" x="27" y="70" width="169" height="169">
                        <XCUIElementTypeScrollView type="XCUIElementTypeScrollView" visible="true" x="27" y="70" width="169" height="169">
                          <XCUIElementTypeOther type="XCUIElementTypeOther" visible="true" x="27" y="70" width="169" height="169">
......

Mine code as example what i did (it only high level as functions code hidden)

        new AppDriver(getDriver()).terminateApp();
        sleep(10000);
        Logger.log(getDriver().getPageSource());
        sleep(10000);

I suspect it might be working for the real device but on browser stack we create the session using the bundleID of app under test. If app is closed then we are not able to get page source using Driver.GetPagesource() and it always throws InvalidElementStateException, Because of this even though app looks like active on browser stack portal, but appium inspector could not interact with the app anymore and it could not fetch the screen shots , no tap and swipe works, etc.
Can you try using appium inspector and creating the cloud connection on browser stack ?

WebDriverAgent has a concept of application under test similar to what’s described in GitHub - appium/appium-mac2-driver: Next-gen Appium macOS driver, backed by Apple XCTest

In order to avoid the above exception to be thrown the application under test must be explicitly terminated by appium-xcuitest-driver/docs/reference/execute-methods.md at master · appium/appium-xcuitest-driver · GitHub
The above API must also be called explicitly even if the app was closed in some other way to notify WDA about the application state change.

1 Like