Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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
Java Android测试:当没有id时该怎么办?_Java_Android_Robotium_Android Testing - Fatal编程技术网

Java Android测试:当没有id时该怎么办?

Java Android测试:当没有id时该怎么办?,java,android,robotium,android-testing,Java,Android,Robotium,Android Testing,我正在尝试为我的应用程序编写一些自动化脚本。我已经在Robotium的网站上完成了教程,对如何实现自动化有了基本的了解。然而,通过使用android hierarchy viewer,我发现所有视图都没有明确定义的ID 从屏幕截图中可以看到,嵌套视图上有视图。它们的ID读起来像0x17e0或0x17de。我如何引用这些,特别是在robotium脚本中?最终的结果是,我试图让它在其中一个文本切换器视图上点击一下。到目前为止,我只能让它工作,如果我给它一个像素点去,或者如果我给它的文本,显示在按钮上

我正在尝试为我的应用程序编写一些自动化脚本。我已经在Robotium的网站上完成了教程,对如何实现自动化有了基本的了解。然而,通过使用android hierarchy viewer,我发现所有视图都没有明确定义的ID

从屏幕截图中可以看到,嵌套视图上有视图。它们的ID读起来像0x17e0或0x17de。我如何引用这些,特别是在robotium脚本中?最终的结果是,我试图让它在其中一个文本切换器视图上点击一下。到目前为止,我只能让它工作,如果我给它一个像素点去,或者如果我给它的文本,显示在按钮上(但文本是动态的,会使一个糟糕的测试)

我是否必须使用GetCurrentView()向下过滤到文本切换器?或者我必须想出一种方法从FrameLayout>RelativeLayout>FrameLayout>LinearLayout>TextSwitcher遍历整个树吗


如果我必须遍历整棵树,如何一次又一次地查看?

我会投票说,您可能真的想在层次结构中添加一些ID,这是一个单行更改,它将使您的生活变得更加轻松

但我不是没有帮助,如果出于某种原因,你不能做到这一点,要做到这一点,你将不得不走整个过程,以达到你想要的看法


获取顶级视图后,您将能够将其强制转换为视图组。ViewGroups有一个名为getChildAt()的方法,您可以使用该方法获取给定索引处的子级,索引是基于0的,并且将与您在层次结构查看器中看到的内容相匹配,因此您可以将这些命令链接在一起,以获得您想要与之交互的视图。

我想说,您可能真的希望在层次结构中添加一些ID。这是一个单行更改,将使您的生活变得更加轻松

但我不是没有帮助,如果出于某种原因,你不能做到这一点,要做到这一点,你将不得不走整个过程,以达到你想要的看法


获取顶级视图后,您将能够将其强制转换为视图组。ViewGroups有一个名为getChildAt()的方法,您可以使用该方法在给定的索引中获取一个子项,该索引基于0,并将与您在层次结构查看器中看到的内容相匹配,因此您可以将这些命令链接在一起,以获得要与之交互的视图。

我很少使用Robodtium,但我知道这正是你想要的。以下是转储主视图Id的代码段:

ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
以下是结果转储:

com.android.launcher2.CellLayout id/cell3 None
     com.android.launcher2.ShortcutAndWidgetContainer NO_ID None
         com.android.launcher2.BubbleTextView NO_ID Email
         com.android.launcher2.LauncherAppWidgetHostView NO_ID None
             android.widget.AnalogClock id/0x7f0f0014 None
         com.android.launcher2.BubbleTextView NO_ID Camera
         com.android.launcher2.BubbleTextView NO_ID Settings
         com.android.launcher2.BubbleTextView NO_ID Gallery
         com.android.launcher2.LauncherAppWidgetHostView NO_ID None
             android.widget.LinearLayout id/0x7f080167 None

我没怎么用过Robodtium,但我知道这正是你想要的。以下是转储主视图Id的代码段:

ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
以下是结果转储:

com.android.launcher2.CellLayout id/cell3 None
     com.android.launcher2.ShortcutAndWidgetContainer NO_ID None
         com.android.launcher2.BubbleTextView NO_ID Email
         com.android.launcher2.LauncherAppWidgetHostView NO_ID None
             android.widget.AnalogClock id/0x7f0f0014 None
         com.android.launcher2.BubbleTextView NO_ID Camera
         com.android.launcher2.BubbleTextView NO_ID Settings
         com.android.launcher2.BubbleTextView NO_ID Gallery
         com.android.launcher2.LauncherAppWidgetHostView NO_ID None
             android.widget.LinearLayout id/0x7f080167 None

虽然我无法让ViewGroup()和getChildAt()方法为我工作,但我最终做了一些不同的事情:

// Grabbing all the LinearLayout views found under view with with id of 'content' 
ArrayList<LinearLayout> linearLayouts = solo.getCurrentViews(LinearLayout.class, solo.getView(R.id.content));

// The 4th item in the linearLayouts list is the one I need
View pickerList = linearLayouts.get(3);

// Grabbing the buttons in the pickerList
ArrayList<TextSwitcher> buttons = solo.getCurrentViews(TextSwitcher.class,pickerList);

// Now I can click on the buttons
solo.clickOnView(buttons.get(0));
//获取在id为“content”的视图下找到的所有LinearLayout视图
ArrayList linearLayouts=solo.getCurrentView(LinearLayout.class,solo.getView(R.id.content));
//linearLayouts列表中的第四项是我需要的
View pickerList=linearLayouts.get(3);
//抓住选择器列表中的按钮
ArrayList buttons=solo.GetCurrentView(TextSwitcher.class,pickerList);
//现在我可以点击按钮了
solo.clickOnView(buttons.get(0));

我会说这很慢。单击第一个按钮大约需要10秒钟才能启动。但是一旦它运行起来,它就会飞起来。

虽然我无法让ViewGroup()和getChildAt()方法为我工作,但我最终做了一些不同的事情:

// Grabbing all the LinearLayout views found under view with with id of 'content' 
ArrayList<LinearLayout> linearLayouts = solo.getCurrentViews(LinearLayout.class, solo.getView(R.id.content));

// The 4th item in the linearLayouts list is the one I need
View pickerList = linearLayouts.get(3);

// Grabbing the buttons in the pickerList
ArrayList<TextSwitcher> buttons = solo.getCurrentViews(TextSwitcher.class,pickerList);

// Now I can click on the buttons
solo.clickOnView(buttons.get(0));
//获取在id为“content”的视图下找到的所有LinearLayout视图
ArrayList linearLayouts=solo.getCurrentView(LinearLayout.class,solo.getView(R.id.content));
//linearLayouts列表中的第四项是我需要的
View pickerList=linearLayouts.get(3);
//抓住选择器列表中的按钮
ArrayList buttons=solo.GetCurrentView(TextSwitcher.class,pickerList);
//现在我可以点击按钮了
solo.clickOnView(buttons.get(0));

我会说这很慢。单击第一个按钮大约需要10秒钟才能启动。但一旦它走了,它就会飞起来。

很有趣。我会试一试,如果我没有_ID,或者我在哪里得到ID/0x7f080167之类的东西,我怎么引用它呢?getView(R.id.0x7f080167)?这些id不是一个好东西,它们将在将来某个时候重新生成,因此不能依赖。android中的ID在构建时自动生成并存储在R文件中。我会试一试,如果我没有_ID,或者我在哪里得到ID/0x7f080167之类的东西,我怎么引用它呢?getView(R.id.0x7f080167)?这些id不是一个好东西,它们将在将来某个时候重新生成,因此不能依赖。android中的ID是在构建时自动生成的,并存储在一个R文件中。在我上面的例子中,使用该视图组,我可以强制转换LinearLayout(在右侧),然后将文本切换器作为chid组,还是必须从左侧的LinearLayout开始一直到它?您可以获得具有id(内容)的FrameLayout它可能是一个安卓内置id,其名称类似于安卓。R。id。内容,而不是你自己的应用程序包。记住,一旦你到达线性布局,你可以保存对该视图的引用,然后在将来从那里遍历到文本切换器;我肯定是用错了。我最终使用GetCurrentView并在LinearLayout.class上进行过滤,将所有这些内容发送到ArrayList<代码>ArrayList linearLayouts=solo.getCurrentView(LinearLayout.class,solo.getView(R.id.content))然后我想到了o