【问题标题】:Providing delay between events in UiAutomator Android在 UiAutomator Android 中提供事件之间的延迟
【发布时间】:2013-09-17 09:49:45
【问题描述】:

如何在 UiAutomator Android 中提供事件点击之间的延迟。

第一个事件是在 EditText 中输入一个 url:

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

在这里,我在 webview 中加载网页。因此,当 url 加载完成时,我需要检查第二个事件。

第二个事件是检查对象的内容描述:

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());
}

但这似乎不起作用。

我只是希望应用在检查第二个事件之前等待一段时间。

我知道 UI Automator 中提供了这三种方法用于延迟 -

public void waitForIdle(long timeout)

public void waitForIdle()

public boolean waitForWindowUpdate(String packageName, long timeout)

但我不知道如何使用这些方法。

请给我一个如何使用这些的例子。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: java android ui-automation android-uiautomator


    【解决方案1】:

    试试吧

    sleep(5000);
    

    让我知道它是否有效。或者你可以试试 WaitForIdle( ) 方法。

    【讨论】:

    • Wait for idle 将等待屏幕关闭。
    【解决方案2】:

    如果您在按下按钮后需要等待

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

    settingsButton.waitUntilGone(1000);
    

    如果从 intent 启动新活动,这种方式是我发现等待 设置包 出现的最简单方法:

    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));
    

    【讨论】:

      【解决方案3】:
      public boolean waitForWindowUpdate(null, long timeout)
      

      对我有好处。我认为可以带上任何“不存在的”String packagaName,例如。 "blablabla",因为你可以得到null 而不是String packageName

      我创建了一个简单的延迟方法:

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

      希望这有帮助。

      【讨论】:

        【解决方案4】:

        我不是 UIautomator 方面的专家,但这可以通过 Thread.sleep() 来完成。

        【讨论】:

        • Uiautomator 不适用于线程。这可能在某些情况下有效,但不能暂停。
        • 不是我不同意,但接受的答案也使用了Thread.sleep()
        【解决方案5】:

        UiDevice 提供了一些可能对您有用的方法:

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

        【讨论】:

        • 这没有成功。我提供了足够的超时时间,但它并没有在两次点击事件之间造成任何延迟。
        【解决方案6】:

        我认为TimeUnit.SECONDS.sleep(val); 在这里应该会有所帮助

        例如

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

        这应该可以工作

        【讨论】:

          【解决方案7】:

          我有一个案例,我不得不等待 Smart Lock 对话框并想办法将其关闭。

          我最终做了以下事情

          final UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
                  // Wait for the Smart Lock dialog to popup
                  if(mDevice.waitForWindowUpdate(null, 5000)) {
                      if("com.google.android.gms".equals(mDevice.getCurrentPackageName())) {
                          // Dismiss dialog
                          mDevice.pressBack();
                      }
                  }
          

          这将等待对话框出现,如果它会被关闭。

          【讨论】:

            【解决方案8】:
            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();
            
                                }
                            });
            

            加载您的网址,然后有方法 onPageFinished() 可以处理您的代码。

            【讨论】:

            • 嘿,我正在作为黑盒测试人员在应用程序代码之外运行测试代码。所以我想要一些 UiAutomator 方法来提供 [1] 和 [2] 之间的延迟 -> setText 和函数调用 - [1] new UiObject(new UiSelector().text("Search here")) .setText("www.hotels .com"); [2] check_currency();你能在这里提供一些建议吗?
            • 我像这样使用它,但似乎仍然无法正常工作。我想在我的点击和函数调用之间提供延迟 - new UiObject(new UiSelector().text("搜索或输入 URL ")) .setText("ftd"); getUiDevice().click(308 , 439); getUiDevice().waitForIdle(50); check_abc();
            • 请有人建议我在这里提供任何延迟的方法。这很重要。谢谢!
            【解决方案9】:

            我能够在 UIAutomator 代码中添加延迟的唯一方法是与 slott 的答案类似:

            我基本上吃了一个小应用程序(android 示例中的基本应用程序),然后按下关闭它。这让所有元素都有时间正确加载。

                Context context = getApplicationContext();
                final Intent intent = context.getPackageManager()
                        .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
                context.startActivity(intent);
            
                // Wait for the app to appear
                mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
                //then close this small app
                mDevice.pressBack();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-09-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-03-22
              • 2012-11-05
              • 1970-01-01
              相关资源
              最近更新 更多