<small id='IxBYh'></small><noframes id='IxBYh'>

      <bdo id='IxBYh'></bdo><ul id='IxBYh'></ul>

        <tfoot id='IxBYh'></tfoot>
      1. <i id='IxBYh'><tr id='IxBYh'><dt id='IxBYh'><q id='IxBYh'><span id='IxBYh'><b id='IxBYh'><form id='IxBYh'><ins id='IxBYh'></ins><ul id='IxBYh'></ul><sub id='IxBYh'></sub></form><legend id='IxBYh'></legend><bdo id='IxBYh'><pre id='IxBYh'><center id='IxBYh'></center></pre></bdo></b><th id='IxBYh'></th></span></q></dt></tr></i><div id='IxBYh'><tfoot id='IxBYh'></tfoot><dl id='IxBYh'><fieldset id='IxBYh'></fieldset></dl></div>
        <legend id='IxBYh'><style id='IxBYh'><dir id='IxBYh'><q id='IxBYh'></q></dir></style></legend>

      2. 如何使用 Google UiAutomator 按两次按钮?

        时间:2023-05-28
        <legend id='ZWEcM'><style id='ZWEcM'><dir id='ZWEcM'><q id='ZWEcM'></q></dir></style></legend>
          <tbody id='ZWEcM'></tbody>

                <bdo id='ZWEcM'></bdo><ul id='ZWEcM'></ul>

                  <tfoot id='ZWEcM'></tfoot>

                  <small id='ZWEcM'></small><noframes id='ZWEcM'>

                1. <i id='ZWEcM'><tr id='ZWEcM'><dt id='ZWEcM'><q id='ZWEcM'><span id='ZWEcM'><b id='ZWEcM'><form id='ZWEcM'><ins id='ZWEcM'></ins><ul id='ZWEcM'></ul><sub id='ZWEcM'></sub></form><legend id='ZWEcM'></legend><bdo id='ZWEcM'><pre id='ZWEcM'><center id='ZWEcM'></center></pre></bdo></b><th id='ZWEcM'></th></span></q></dt></tr></i><div id='ZWEcM'><tfoot id='ZWEcM'></tfoot><dl id='ZWEcM'><fieldset id='ZWEcM'></fieldset></dl></div>
                2. 本文介绍了如何使用 Google UiAutomator 按两次按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下脚本,用于在 Android 中使用 UiAutomator 在计算器中输入33".但是,只接受第一个 '3',第二次按下完全被忽略.

                  I have the following script for typing '33' into the Calculator, in Android, using UiAutomator. However, only the first '3' is accepted, the second press is entirely ignored.

                  import com.android.uiautomator.core.*;
                  import com.android.uiautomator.testrunner.UiAutomatorTestCase;
                  
                  public class MyFirstUiAutomatorTest extends UiAutomatorTestCase {
                      UiObject getByDescription(String description) {
                          return new UiObject(new UiSelector().description(description));
                      }
                  
                      UiObject getByText(String description) {
                          return new UiObject(new UiSelector().text(description));
                      }
                  
                      UiObject scrollableGetByText(String text ) throws UiObjectNotFoundException {
                              UiScrollable uiScrollable = new UiScrollable(new UiSelector().scrollable(true));
                              uiScrollable.setAsHorizontalList();
                              return uiScrollable.getChildByText(new UiSelector().className(
                                      android.widget.TextView.class.getName()),
                                      text);      
                      }
                  
                      public void testStuff() throws UiObjectNotFoundException {
                          getUiDevice().pressHome();
                          getByDescription("Apps").clickAndWaitForNewWindow();
                          getByText("Apps").click();
                          scrollableGetByText("Calculator").clickAndWaitForNewWindow();
                  
                          // pressing '+' and '=' effectively clears the previous input
                          getByText("+").click();
                          getByText("=").click();
                          getByText("3").click();
                          // this second '3' is ignored
                          getByText("3").click();
                      }
                  }
                  

                  我尝试在第一次点击后添加睡眠 2 秒,方法是:

                  I've tried adding a sleep for 2 seconds after the first click, by doing:

                          try {
                              Thread.sleep(2000);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
                  

                  ...但这并没有改变任何东西.

                  ... but that didn't change anything.

                  我还尝试在 2 个 '3' 之间单击另一个按钮,即:

                  I also tried clicking on a different button, in between the 2 '3's, ie:

                          new UiObject(new UiSelector().text("3")).click();
                          new UiObject(new UiSelector().className("android.widget.EditText")).click();
                          new UiObject(new UiSelector().text("3")).click();
                  

                  ...但这也没有用.

                  想法?

                  (注意:在 AVD 中使用 Android 4.1.2;在 Ubuntu linux 12.04 上运行)

                  (Note: using Android 4.1.2, in an AVD; running on Ubuntu linux 12.04)

                  编辑,根据 Rami 的观察,我尝试了以下方法,以重复使用相同的 UiObject 对象来获得相同描述的第二次请求:

                  Edit, following Rami's observations, I tried the following, to reuse the same UiObject object for a second request for the same description:

                  HashMap<String,UiObject> objectByText = new HashMap<String,UiObject>(); 
                  UiObject getByText(String description) {
                      if( objectByText.containsKey(description)) {
                          System.out.println("" + objectByText.get(description) );
                          return objectByText.get(description);
                      }
                      System.out.println("Created new object for [" + description + "]");
                      UiObject object = new UiObject(new UiSelector().text(description));
                      objectByText.put(description, object );
                      System.out.println("" + object );
                      return object;
                  }
                  

                  ...但它不起作用,即使它每次都清楚地重用同一个 UiObject,因为它只说一次为 [3] 创建新对象".

                  ... but it didn't work, even though it is clearly reusing the same UiObject each time, because it only says 'Created new object for [3]' once.

                  然后我尝试了 UiDevice.click() 'trick',通过创建一个函数 'click' 如下,再次遵循 Rami 的观察:

                  Then I tried the UiDevice.click() 'trick', by creating a function 'click' as follows, again, following Rami's observations:

                  void click(UiObject target ) throws UiObjectNotFoundException {
                      Rect rect = target.getBounds();
                      System.out.println("rect: " + rect );
                      getUiDevice().click(rect.centerX(), rect.centerY());
                  }
                  

                  但是,这对我也不起作用:只出现第一个3",第二个被忽略,即使两次点击都明显在同一个地方,因为 rect:输出位置相同.如果我使用自己的桌面鼠标手动单击3"两次,则两个 3 都显示正常.

                  However, this didn't work for me either: only the first '3' appears, and the second is ignored, even though both clicks are clearly in the same place, because the rect: output locations are identical. If I click '3' twice manually, using my own desktop mouse, then both 3s appear ok.

                  我还尝试在两次点击之间添加两秒 Thread.sleep(),但我仍然只出现了一个3".

                  I also tried adding a two second Thread.sleep() between the clicks, and still only a single '3' appeared for me.

                  推荐答案

                  不要只按文本搜索,而是尝试按文本和类搜索.这是执行此操作的示例方法.

                  Instead of just searching by text, try searching by the text and the class. Here is a sample method for doing so.

                  Uiobject getByTextAndClass(String text, String className) {
                      return new UiObject(new UiSelector().text(text).className(className));
                  }
                  

                  然后,如果您尝试为带有数字 3 的计算器按钮调用此方法:

                  And then if you are trying to call this method for the Calculator button with number 3 on it:

                  getByTextAndClass("3", android.widget.Button.class.getName()).click();
                  

                  您可以使用 UiAutomatorViewer 工具:{android-sdk}/tools/uiautomator.bat 查看不同 UiObject 的类名和其他属性.

                  You can use the UiAutomatorViewer tool: {android-sdk}/tools/uiautomator.bat to check the classnames and other attributes of different UiObjects.

                  这适用于我的 4.2.2 设备,但我正在下载 4.1.2 以在那里进行测试.

                  This works on my 4.2.2 devices, but I am downloading 4.1.2 to test it on there as well.

                  我在 4.1.2 AVD 上尝试过,它可以在我的 Windows 机器上运行.

                  I tried it on a 4.1.2 AVD and it works on my Windows machine.

                  这篇关于如何使用 Google UiAutomator 按两次按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android - 在用户输入之前和之后检查editText是否> 3 下一篇:“Chrome Legacy Window"的自动化(铬)使用 Winium

                  相关文章

                  <small id='Ekdgs'></small><noframes id='Ekdgs'>

                  <legend id='Ekdgs'><style id='Ekdgs'><dir id='Ekdgs'><q id='Ekdgs'></q></dir></style></legend>

                    • <bdo id='Ekdgs'></bdo><ul id='Ekdgs'></ul>

                  1. <tfoot id='Ekdgs'></tfoot>
                  2. <i id='Ekdgs'><tr id='Ekdgs'><dt id='Ekdgs'><q id='Ekdgs'><span id='Ekdgs'><b id='Ekdgs'><form id='Ekdgs'><ins id='Ekdgs'></ins><ul id='Ekdgs'></ul><sub id='Ekdgs'></sub></form><legend id='Ekdgs'></legend><bdo id='Ekdgs'><pre id='Ekdgs'><center id='Ekdgs'></center></pre></bdo></b><th id='Ekdgs'></th></span></q></dt></tr></i><div id='Ekdgs'><tfoot id='Ekdgs'></tfoot><dl id='Ekdgs'><fieldset id='Ekdgs'></fieldset></dl></div>