Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 需要UI观察者示例代码吗_Android_Automation_Android Uiautomator_Ui Testing - Fatal编程技术网

Android 需要UI观察者示例代码吗

Android 需要UI观察者示例代码吗,android,automation,android-uiautomator,ui-testing,Android,Automation,Android Uiautomator,Ui Testing,嗨,自动化专家们 网址: 如何实现此目标:您可以使用此方法处理阻止测试继续进行的已知阻塞问题。例如,您可以检查是否出现阻止测试的对话框,然后关闭该对话框或执行其他适当的操作以允许测试继续 在尝试播放视频并继续我们的测试时,请提供一个示例代码,说明“在弹出窗口上按OK”无法播放此视频“?Anvesh, 下面的网站有一个关于如何使用UI Watcher的完整示例 仅供参考,这是我在Google上搜索uiwatcher uiautomator时的第一个结果。您的观察者由UiDevice类管理。实现观察

嗨,自动化专家们

网址:

如何实现此目标:您可以使用此方法处理阻止测试继续进行的已知阻塞问题。例如,您可以检查是否出现阻止测试的对话框,然后关闭该对话框或执行其他适当的操作以允许测试继续

在尝试播放视频并继续我们的测试时,请提供一个示例代码,说明“在弹出窗口上按OK”无法播放此视频“?

Anvesh, 下面的网站有一个关于如何使用UI Watcher的完整示例


仅供参考,这是我在Google上搜索
uiwatcher uiautomator

时的第一个结果。您的观察者由
UiDevice
类管理。实现观察者的典型逻辑流程如下:

  • 定义新的观察者
  • 注册你的观察者
  • 运行你的观察者
  • 代码:


    以下网站提供了如何使用UI Watcher的完整示例

    非常感谢JulianHarty。。我不知道我怎么会错过这个(很乐意提供帮助。如果答案对你有帮助,你能接受吗?这有助于其他人识别答案可能会对他们有帮助。为了避免只链接的答案,请重新发布你的答案!@Anveshylamarthy,作为重新发布我的答案并将其标记为已接受的副作用,StackOverflow从我的声誉中减去15。我感谢你提供答案的目的代码,但不是副作用。。。
    package com.watcherDemoTests;
    
    import android.util.Log;
    
    import com.android.uiautomator.core.UiCollection;
    import com.android.uiautomator.core.UiDevice;
    import com.android.uiautomator.core.UiObject;
    import com.android.uiautomator.core.UiObjectNotFoundException;
    import com.android.uiautomator.core.UiScrollable;
    import com.android.uiautomator.core.UiSelector;
    import com.android.uiautomator.core.UiWatcher;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    public class WatcherDemoTestExample1 extends UiAutomatorTestCase {
    
    private static final String LOG_TAG = "WatcherDemoEx1";
    private static final String MYOKCANCELDIALOGWATCHER_STRING = "OkCancelDialogWatcher";
    
    public void testWatcherDemoTestExample1() throws UiObjectNotFoundException {
    Log.w(LOG_TAG, "Starting our test!");
    
    // Simulate a short press on the HOME button based on the sample test case:
    //
    // http://developer.android.com/tools/testing/testing_ui.html#sample
    //
    getUiDevice().pressHome();
    // We're now on the home screen. Launch the All Apps screen.
    UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
    // Simulate a click to bring up the All Apps screen.
    allAppsButton.clickAndWaitForNewWindow();
    // In the All Apps screen, the Snow Report app should be in the Apps tab
    // assuming it is installed prior to starting this test
    UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
    // Simulate a click to enter the Apps tab.
    appsTab.click();
    // Next, in the apps tabs, we can simulate a user swiping until they
    // come across the Snow Report app icon. Again, swiped from the
    // sample test case.
    UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
    // Set the swiping mode to horizontal (the default is vertical)
    // This is only compatible with API lvl 17. Short of that it will crash
    // with a "method not found" failure.
    appViews.setAsHorizontalList();
    // Create a UiSelector to find the Snow Report app and simulate
    // a user click to launch the app.
    UiObject apiDemoApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "API Demos");
    apiDemoApp.clickAndWaitForNewWindow();
    /////////////////////////////////////////////////////////////////////
    // This concludes the section devoted to simply launching the app. //
    /////////////////////////////////////////////////////////////////////
    
    // Define watcher
    UiWatcher okCancelDialogWatcher = new UiWatcher() {
    @Override
    public boolean checkForCondition() {
    UiObject okCancelDialog = new UiObject(new UiSelector().textStartsWith("Lorem ipsum"));
    if(okCancelDialog.exists()){
    Log.w(LOG_TAG, "Found the example OK/Cancel dialog");
    UiObject okButton = new UiObject(new UiSelector().className("android.widget.Button").text("OK"));
    try {
    okButton.click();
    } catch (UiObjectNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return (okCancelDialog.waitUntilGone(25000));
    }
    return false;
    }
    };
    // Register watcher
    UiDevice.getInstance().registerWatcher(MYOKCANCELDIALOGWATCHER_STRING, okCancelDialogWatcher);
    
    // Run watcher
    UiDevice.getInstance().runWatchers();
    
    /*
    * This test demonstrates UiAutomator Watchers using the Emulator's
    * API Demos App/Alert Dialogs
    *
    * With this, the watcher will be set in advance to identify when
    * an alert dialog is present and cancel it. The test will click
    * on at least one item in the list.
    */
    // Get Api Demos list
    UiCollection apiDemoList = new UiCollection(new UiSelector().className("android.widget.ListView"));
    // Click on App
    UiObject appTextView = apiDemoList.getChildByText(new UiSelector()
    .className(android.widget.TextView.class.getName()),
    "App");
    appTextView.clickAndWaitForNewWindow();
    // Get App demo list
    UiCollection appDemoList = new UiCollection(new UiSelector().className("android.widget.ListView"));
    // Click on Alert Dialogs
    UiObject alertDialogTextView = appDemoList.getChildByText(new UiSelector()
    .className(android.widget.TextView.class.getName()), "Alert Dialogs");
    alertDialogTextView.clickAndWaitForNewWindow();
    
    // Click on button with text "OK Cancel dialog with a message"
    UiObject okCancelDialogButton1 = new UiObject(new UiSelector().text("OK Cancel dialog with a message"));
    okCancelDialogButton1.click();
    // Click on button with text "OK Cancel dialog with a long message"
    // This is where the watcher should save our bacon. Yes, this is a poorly written test case, I know.
    // Just go with me here, I'm making a point about Watchers. If the dialog from the previous button
    // press is still in place, this new selector will fail to find an object containing that text.
    // Because the watcher is there, the dialog gets closed and we're all good.
    UiObject okCancelDialogButton2 = new UiObject(new UiSelector().text("OK Cancel dialog with a long message"));
    okCancelDialogButton2.click();
    }
    
    }