13

如何在 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)

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

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

任何帮助,将不胜感激。

谢谢!

4

9 回答 9

11

试一试

sleep(5000);

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

于 2013-10-15T12:27:47.810 回答
5

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

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

或者

settingsButton.waitUntilGone(1000);

如果以这种方式从意图启动新活动是我能找到的等待设置包出现的最简单的方法:

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));
于 2015-06-19T09:28:53.870 回答
4
public boolean waitForWindowUpdate(null, long timeout)

对我有用。例如,我认为可以带来任何 'not existing' String packagaName"blablabla", 因为你可以得到null而不是String packageName.

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

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

希望这会有所帮助。

于 2014-04-09T15:44:48.667 回答
2

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

public void waitForIdle(long timeout)

public void waitForIdle()

public boolean waitForWindowUpdate(String packageName, long timeout)
于 2013-09-18T14:50:36.993 回答
2

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

于 2013-10-20T07:03:19.110 回答
1

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

例如

import java.util.concurrent.TimeUnit;

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

这应该工作

于 2017-03-16T08:45:44.827 回答
1

我有一个案例,我不得不等待 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();
            }
        }

这将等待对话框出现,如果出现则将其关闭。

于 2018-05-23T17:53:56.510 回答
0
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() 可以在其中处理您的代码。

于 2013-09-17T09:59:47.770 回答
0

我能够在 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();
于 2021-03-17T18:24:12.230 回答