The synchronization of UiAutomator

后端 未结 2 1905
面向向阳花
面向向阳花 2021-01-14 23:55

I am writing uiautomator test case for running Android app program in emulator. Here comes the problem, assume I run the Ui test case in a different emulator machine. How co

相关标签:
2条回答
  • 2021-01-15 00:42

    Generally you use functions like UiDevice.wait(..) to pause until your test can proceed.

    For example, if you have a button which opens a screen containing some elements you want to interact with, you'll need to wait for the new content to appear before trying to interact with those elements.

    This would look something like:

    detailsButton.click();
    
    device.wait(Until.hasObject(By.desc("Details Pane")), TIMEOUT);
    
    // Do something on the details pane
    
    0 讨论(0)
  • 2021-01-15 00:49

    Allen Hair answer works just fine if you know what to expect after the click. If you don't (like in my case) you can:

    UiAutomation uiAuto = InstrumentationRegistry.getIntrumentation().getUiAutomation(); //gets the UiAutomation for this execution
    AccessibilityNodeInfo rootNode = uiAuto.getRootInActiveWindow();  //gets the root node of the currently visible screen
    //makes sure there is a rootNode and that is has children, i.e., there is a drawn screen
    while(rootNode == null || rootNode.getChildCount() == 0){
        rootNode = uiAuto.getRootInActiveWindow();
    }
    //getting here you are sure that the new screen is drawn.
    

    I do this on top of

    device.waitForWindowUpdate(packageName, timeOut)
    

    because in several occasions the wait would return and the root node of the screen could not yet be accessed, resulting in future UiObjectNotFoundException.

    0 讨论(0)
提交回复
热议问题