Java 在 UiAutomator Android 中提供事件之间的延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18846579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Providing delay between events in UiAutomator Android
提问by Smriti
How can I provide delay between event clicks in UiAutomator Android.
如何在 UiAutomator Android 中提供事件点击之间的延迟。
First event is entering a url in EditText :
第一个事件是在 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 finishes , then I need to check for second event.
在这里,我在 webview 中加载一个网页。因此,当 url 加载完成时,我需要检查第二个事件。
Second event is checking content description of a 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 doesn't seem to be working.
但这似乎不起作用。
I just want the app to wait for some time before checking for the second event.
我只希望应用程序在检查第二个事件之前等待一段时间。
I know these three methods are provided for delay in UI Automator -
我知道这三种方法是为 UI Automator 中的延迟提供的 -
public void waitForIdle(long timeout)
public void waitForIdle()
public boolean waitForWindowUpdate(String packageName, long timeout)
But I don't know how to use these methods.
但我不知道如何使用这些方法。
Please suggest me a example how to use these.
请给我一个如何使用这些的例子。
Any help would be appreciated.
任何帮助,将不胜感激。
Thanks!
谢谢!
采纳答案by intrepidkarthi
Try just
试试吧
sleep(5000);
Let me know whether it works. Or you can try WaitForIdle( ) method.
让我知道它是否有效。或者您可以尝试 WaitForIdle() 方法。
回答by Nikhil Virupakshi
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();
}
});
Load your url then there is method onPageFinished() where you can handle your code.
加载您的网址,然后有方法 onPageFinished() 可以在其中处理您的代码。
回答by Anders
UiDevice
provides a few methods that may work for you:
UiDevice
提供了一些可能对您有用的方法:
public void waitForIdle(long timeout)
public void waitForIdle()
public boolean waitForWindowUpdate(String packageName, long timeout)
回答by Dnavir
I am no expert in UIautomator, but this can be done with Thread.sleep().
我不是 UIautomator 方面的专家,但这可以通过 Thread.sleep() 来完成。
回答by m.zhelieznov
public boolean waitForWindowUpdate(null, long timeout)
Works good for me. I think it's possible to bring any 'not existing' String packagaName
, for ex. "blablabla"
, because you can get null
instead of String packageName
.
对我有用。我认为可以带来任何“不存在” String packagaName
,例如。"blablabla"
,因为你可以得到null
而不是String packageName
.
I've create a simply method to make a delay:
我创建了一个简单的方法来延迟:
public void waitTime(int time) {
getUiDevice().waitForWindowUpdate(null, time);
}
Hope this is helpful.
希望这是有帮助的。
回答by Dmitry Gr
If you need a wait after pressing a button
如果您在按下按钮后需要等待
UiObject settingsButton = mDevice.findObject(new UiSelector().text("Settings"));
settingsButton.clickAndWaitForNewWindow();
or
或者
settingsButton.waitUntilGone(1000);
In case of launching new activity from intentthis way is the easiest I could find to wait for a settings packageappear:
如果以这种方式从意图启动新活动是我能找到的最简单的等待设置包出现的方法:
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));
回答by Steven
i think TimeUnit.SECONDS.sleep(val);
should be helpful here
我认为TimeUnit.SECONDS.sleep(val);
在这里应该会有所帮助
for instance
例如
import java.util.concurrent.TimeUnit;
private void waitFor(long val)
{
try
{
TimeUnit.SECONDS.sleep(val);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
This should work
这应该工作
回答by slott
I had a case where I had to wait for the Smart Lock dialog and find a way to dismiss it.
我有一个案例,我不得不等待 Smart Lock 对话框并找到一种方法来关闭它。
I ended up doing the following
我最终做了以下事情
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();
}
}
This will wait for the dialog to appear and ifit does it gets dismissed.
这将等待对话框出现,如果出现,它将被关闭。