title: How to check application state under swift UI Test
body: <h3>Some background</h3>
  
  <p>I am currently writing a UI Test for a settings pane, and click on buttons to enable certain permissions such as push notifications and location services.</p>
  
  <p>However, if the alert for the permission has been displayed before (regardless of the user allowing or denying access to the permission), the alert will <strong>not</strong> display again, and will just take the user to the settings app. Unfortunately, these settings do not reset, meaning the first time I run the UI tests, alerts will show; and on all subsequent UI test runs, the buttons will take me to the settings app unless I reset the device before the tests begin.</p>
  
  <h3>My issue</h3>
  
  <p>Thus, my test needs to know if the app went into the background, and attempt to foreground it to continue the testing:</p>
  
  <pre><code>if app.state == background {
      foregroundApp()
  }
  
  // continue with other tests 
  </code></pre>
  
  <p>Is there any way to determine if the app is in the background?</p>
  
  <h3>What I tried</h3>
  
  <p>I researched methods to determine the state of the application <em>(running/background/etc)</em> from a UI test, and was not able to find much. I tried to check whether certain elements exist:</p>
  
  <pre><code>if (app.navigationBars.element.exists) ...
  </code></pre>
  
  <p>but this gives me runtime errors<sup>[1]</sup> if the user is taken to the settings page because the app under test is in the background, and the test cannot lookup the navigationBars (or other elements).</p>
  
  <p>I tried using some of the methods from Facebook's private headers for <code>XCUIApplication()</code> and <code>XCUIElement()</code>. </p>
  
  <p><code>XCUIApplication().state</code> always returns <code>3</code> no matter what state the app is currently in, and any attempts to call <code>XCUIApplication().resolve()</code> to foreground the app give me the same errors as before<sup>[1]</sup></p>
  
  <p>I tried to rewrite the logic to foreground the app before resuming the tests, but methods such as <code>XCUIApplication().launch()</code> kill the app before restarting, which I <strong>cannot</strong> do. Only siri service seems to work for me, but I cannot access the siri service through the corporate proxy, and modifying proxy permissions is <strong>not</strong> possible.</p>
  
  <p>Is there any other way to check the app state?</p>
  
  <h3>Errors</h3>
  
  <p>[1] This error is printed every time I try to do something involving state. I do <strong>not</strong> call <code>snapshotView</code> anywhere, and thus the suggestion to use <code>afterScreenUpdates</code> is useless.</p>
  
  <pre><code>Failure to get snapshot within 15.0s
  Cannot snapshot view (&lt;UIKeyboardImpl: 0x7febcc75d000; frame = (0 0; 
    414 226); layer = &lt;CALayer: 0x608000625720&gt;&gt;) with 
    afterScreenUpdates:NO, because the view is not in a window. Use 
    afterScreenUpdates:YES.`
  </code></pre>
  
  <h3>tl;dr</h3>
  
  <p>I need to check whether the app I am UI testing has entered the background (i.e. user pressed the home button). Checking for existence of particular elements such as navigation bars doesn't work, neither do most methods from Facebook's private headers for XCUIApplication/XCUIElement. Foregrounding the app also causes issues, and relaunching the app is not an option; neither is siri service.</p>