Securing Delay Between Events in UiAutomator Android

How can I provide a delay between clicks of events in UiAutomator Android.

The first event enters the URL into the EditText:

new UiObject(new UiSelector().text("Search here")).setText("abc.com"); getUiDevice.waitForIdle(15000); 

Here I am loading a webpage inside a webview. So when url loading ends, I need to check the second event.

The second event checks the description of the contents of the object:

 UiObject curClass = new UiObject(new UiSelector().className("android.widget.RelativeLayout")); UiObject yCur = curClass.getChild(new UiSelector().description("ye")); getCurCol(); public void getCurCol() throws UiObjectNotFoundException { try { if (yCur.getContentDescription() != null) report.append("Success"); } catch (Exception e) { System.err.println("\nCaught Exception: " + e.getMessage()); } 

But this does not seem to work.

I just want the application to wait a while before checking the second event.

I know that these three methods are provided for delay in UI Automator -

 public void waitForIdle(long timeout) public void waitForIdle() public boolean waitForWindowUpdate(String packageName, long timeout) 

But I do not know how to use these methods.

Please offer me an example of how to use them.

Any help would be appreciated.

Thanks!

+6
source share
7 answers

Just try

 sleep(5000); 

Let me know if this works. Or you can try the WaitForIdle () method.

+9
source
 public boolean waitForWindowUpdate(null, long timeout) 

Works well for me. I think that any "non-existent" String packagaName can be String packagaName , for example. "blablabla" because you can get null instead of String packageName .

I created a simple method for the delay:

 public void waitTime(int time) { getUiDevice().waitForWindowUpdate(null, time); } 

Hope this will be helpful.

+2
source

If you need to wait after pressing a button

  UiObject settingsButton = mDevice.findObject(new UiSelector().text("Settings")); settingsButton.clickAndWaitForNewWindow(); 

or

 settingsButton.waitUntilGone(1000); 

If you start a new action from an intention, this method is easiest to find in order to wait for the settings package to appear:

 mDevice.wait(Until.hasObject(By.pkg("com.android.settings").depth(0)), 2000); UiObject settingsValidation = mDevice.findObject(new UiSelector().packageName("com.android.settings").text("Settings")); assertTrue("Unable to detect Settings", settingsValidation.waitForExists(4000)); 
+2
source

UiDevice provides several methods that may work for you:

 public void waitForIdle(long timeout) public void waitForIdle() public boolean waitForWindowUpdate(String packageName, long timeout) 
+1
source

I am not an expert in UIautomator, but this can be done using Thread.sleep ().

+1
source
 webView.loadUrl("http://abc.com"); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); //Handle your code here //Toast.makeText(TableContentsWithDisplay.this, "Width " + view.getWidth() +" *** " + "Height " + view.getHeight(), Toast.LENGTH_SHORT).show(); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { // TODO Auto-generated method stub super.onReceivedSslError(view, handler, error); //Toast.makeText(TableContentsWithDisplay.this, "error "+error, Toast.LENGTH_SHORT).show(); } }); 

Download your url, then there is the onPageFinished () method, where you can process your code.

0
source

I think TimeUnit.SECONDS.sleep(val); should be useful here

eg

 import java.util.concurrent.TimeUnit; private void waitFor(long val) { try { TimeUnit.SECONDS.sleep(val); } catch (InterruptedException e) { e.printStackTrace(); } } 

This should work

0
source

Source: https://habr.com/ru/post/953973/


All Articles