Java 在UiAutomator Android中提供事件之间的延迟

Java 在UiAutomator Android中提供事件之间的延迟,java,android,ui-automation,android-uiautomator,Java,Android,Ui Automation,Android Uiautomator,如何在UiAutomator Android中提供事件点击之间的延迟 第一个事件是在EditText中输入url: new UiObject(new UiSelector().text("Search here")).setText("abc.com"); getUiDevice.waitForIdle(15000); 在这里,我在webview中加载一个网页。所以当url加载完成时,我需要检查第二个事件 第二个事件是检查对象的内容描述: UiObject curClass = new Ui

如何在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自动机中的延迟提供的-

public void waitForIdle(long timeout)

public void waitForIdle()

public boolean waitForWindowUpdate(String packageName, long timeout)
但我不知道如何使用这些方法

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

任何帮助都将不胜感激

谢谢

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

                    }
                });

加载您的url,然后在PageFinished()方法中可以处理您的代码。

UiDevice
提供了一些可能适用于您的方法:

public void waitForIdle(long timeout)

public void waitForIdle()

public boolean waitForWindowUpdate(String packageName, long timeout)
试试看

sleep(5000);

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

我不擅长UIautomator,但这可以通过Thread.sleep()完成

对我有好处。我认为可以将任何“不存在的”
String packageName
,例如
“blablabla”
,因为您可以获得
null
而不是
String packageName

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

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

希望这对您有所帮助。

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

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

如果从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));

我认为
TimeUnit.SECONDS.sleep(val)在这里应该很有用

比如说

import java.util.concurrent.TimeUnit;

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

这应该行得通

我曾经遇到过这样的情况,我必须等待智能锁对话框,然后找到一种方法来关闭它

我最后做了以下几件事

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

这将等待对话框出现,如果出现,它将被取消。

我能够在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();

嘿,我作为黑盒测试仪在应用程序代码之外运行测试代码。所以我需要一些UiAutomator方法来提供[1]和[2]之间的延迟->setText和函数调用-[1]新UiObject(new-UiSelector().text(“Search here”)).setText(“www.hotels.com”);[2] 检查货币();您能在这里提供一些建议吗?我这样使用它,但似乎仍然不起作用。我想提供单击和函数调用之间的延迟-新建UiObject(新建UiSelector().text(“搜索或键入URL”)).setText(“ftd”);getUiDevice()。单击(308439);getUiDevice().waitForIdle(50);检查abc();请有人能在这里给我建议任何延迟的方法吗。这很重要。谢谢这没有成功。我提供了足够的超时时间,但它不会在两次单击事件之间造成任何延迟。Uiautomator不能很好地处理线程。这在某些情况下可能会起作用,但不会暂停。并非我不同意,但公认的答案也使用了
Thread.sleep()
Wait for idle将等待屏幕关闭。
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();
            }
        }
    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();