Android Robotium 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2312379/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:20:46  来源:igfitidea点击:

Examples for Robotium

androidtestinginstrumentationrobotium

提问by siri

I found a tool for Instrumentation Testing called Robotium.It is easy and simple for black box testing of android applications. We can use it as follows:

我找到了一个叫做 Robotium 的 Instrumentation Testing 工具。它对于 Android 应用程序的黑盒测试非常简单。我们可以这样使用它:

    solo.clickOnText("Other");
    solo.clickOnButton("Edit");
    assertTrue(solo.searchText("Edit Window"));
    solo.enterText(1, "Some text for testing purposes")
    solo.clickOnButton("Save");
    assertTrue(solo.searchText("Changes have been made successfully"));
    solo.clickOnButton("Ok");
    assertTrue(solo.searchText("Some text for testing purposes"));

Can any body have more idea about it? Can any one please tell how can we use it for webviews and listviews etc.

任何机构都可以对此有更多了解吗?任何人都可以告诉我们如何将它用于 webviews 和 listviews 等。

回答by Renas

Please see the QA wiki page for common question and answers on what Robotium supports: http://code.google.com/p/robotium/wiki/QuestionsAndAnswers

有关 Robotium 支持的常见问题和解答,请参阅 QA wiki 页面:http: //code.google.com/p/robotium/wiki/QuestionsAndAnswers

Also please go to the Getting Started page: http://code.google.com/p/robotium/wiki/Getting_Started

另外请转到入门页面:http: //code.google.com/p/robotium/wiki/Getting_Started

There you will find an example test project that you download and look at for ideas. You can also download the javadoc from: http://code.google.com/p/robotium/downloads/listto see what functionality there is at the moment.

在那里,您会找到一个示例测试项目,您可以下载该项目并寻找想法。您还可以从http://code.google.com/p/robotium/downloads/list下载 javadoc以查看目前有哪些功能。

For tutorials please visit: http://code.google.com/p/robotium/wiki/RobotiumTutorials

有关教程,请访问:http: //code.google.com/p/robotium/wiki/RobotiumTutorials

Sincerely, Renas

真诚的,雷纳斯

回答by user643154

I can say, what you are not able to do with Robotium :)

我可以说,你不能用 Robotium 做什么 :)

  1. Cross Activities testing, Robotium is able to work only with same certificate app, otherwise you will get inject events exception (eg you are not able to do clicks on screen keyboard)

  2. Robotium has no mechanism to handle expected/unexpected alerts/popus/dialogues. For example iOs javascript tests has very simple boolean flag and callback for handling alerts

  3. Robotium has big problem with auto scrolling methods (possibly currently it is fixed) for example if you are looking for the text, which is not shown, Robotium will stack in the end of the scroll view and make assertTrue(false) to stop scrolling

  4. Robotium has assertTrue(false) logic for reporting problems/unexpected situations instead of returning some Enum value or boolean (success/fail) so for a good stress tests which are run 24/7 you need to add your own methods which will not stop test, just handle 'method fail to click x y' result value

  5. You will need to implement some logic to click items in the scroll/list view. Because of Robotium clicks in the center of the view, you will always get exception or assertTrue(false) when try to click view with only 20% part shown

  1. 跨活动测试,Robotium 只能使用相同的证书应用程序,否则您将收到注入事件异常(例如,您无法在屏幕键盘上进行点击)

  2. Robotium 没有处理预期/意外警报/流行/对话的机制。例如,iOs javascript 测试具有非常简单的布尔标志和用于处理警报的回调

  3. Robotium 在自动滚动方法方面存在很大问题(目前可能已修复)例如,如果您正在查找未显示的文本,Robotium 将堆叠在滚动视图的末尾并使 assertTrue(false) 停止滚动

  4. Robotium 具有用于报告问题/意外情况的 assertTrue(false) 逻辑,而不是返回一些枚举值或布尔值(成功/失败),因此对于 24/7 运行的良好压力测试,您需要添加自己的方法,这些方法不会停止测试, 只处理 'method failed to click xy' 结果值

  5. 您需要实现一些逻辑来单击滚动/列表视图中的项目。由于 Robotium 在视图中心单击,因此尝试单击仅显示 20% 部分的视图时,您将始终收到异常或 assertTrue(false)

In general Robotium is very cool and helpful and I like it very much :) And I can't imagine life without this great library!

总的来说,Robotium 非常酷且乐于助人,我非常喜欢它 :) 而且我无法想象没有这个很棒的图书馆的生活!

回答by Sarp Centel

searchText method also searches ListViews. You can use it together with assertions to ensure that your ListViews contain the right content

searchText 方法也搜索 ListViews。您可以将它与断言一起使用,以确保您的 ListViews 包含正确的内容

回答by michal

In order to click List. If your activity is ListActivity type you can use clickInList with one parameter which is the index of line that should be clicked. In other cases use clickInList with two parameters – listview screen index and line number. For WebView if you load a page you should use waitForText() mathod to check content.

为了单击列表。如果您的活动是 ListActivity 类型,您可以将 clickInList 与一个参数一起使用,该参数是应单击的行的索引。在其他情况下,使用带有两个参数的 clickInList - listview 屏幕索引和行号。对于 WebView 如果你加载一个页面,你应该使用 waitForText() mathod 来检查内容。

more examples: http://bitbar.com/blog/54/automated-ui-testing-android-applications-robotium

更多示例:http: //bitbar.com/blog/54/automated-ui-testing-android-applications-robotium

回答by Gennadiy Ryabkin

  • Views
  • 观看次数

For listViews you can use following method solo.getCurrentListViews()which return a number of list views on the current screen, and then iterate through or get other object types (android widgets) from them for example you need to click image viewsfrom all lists on the screen which not redirect you to another activity and only change state of other objects:

对于 listViews,您可以使用以下方法solo.getCurrentListViews()返回当前屏幕上的多个列表视图,然后迭代或从中获取其他对象类型(android 小部件),例如您需要单击屏幕上所有列表中的图像视图,而不是将您重定向到另一个活动并且仅更改其他对象的状态:

ArrayList<ListView> lw = solo.getCurrentListViews(); // get all list views
// logging to logcat
Log.i("stats", "number of list views on the current screen: " + aLw.size());
if (aLw.size() != 0) 
for (ListView l: aLw) {
    // Take all image views from list and click each
    ArrayList <ImageView> aIw = solo.getCurrentImageViews(l);
    Log.i("stats", "list view " + l + " contains " + iw.size() + " image views.");
    if (aIw.size() != 0)
    for (int i = 0; i < aIw.size(); ) {
         // clicking
         solo.clickOnView(aIw.get(i));
         Log.i("click", "image view " + i " clicked."); 
    }
}

You can type text to editTextview or get text from textViews. You can combine Robotium with Java and Android API. For example check visibility of images on the screen using getVisibility()method and comparing it with three major states View.GONE, View.VISIBLE, View.INVISIBLE. Or you can check connection using Java method HttpURLrequestbefore execution of your tests.

您可以输入文本到editText视图或从textViews获取文本。您可以将 Robotium 与 Java 和 Android API 结合使用。例如,使用getVisibility()方法检查屏幕上图像的可见性,并将其与三个主要状态View.GONEView.VISIBLEView.INVISIBLE 进行比较。或者您可以在执行测试之前使用 Java 方法HttpURLrequest检查连接。

  • Other
  • 其他

If you have source you can take objects from any layout knowing its ID! Also exist a lot of awesome stuff like solo.waitForActivity(), solo.assertMemoryNotLow(), solo.takeScreenShot().

如果你有源,你可以从任何布局中获取对象,知道它的 ID!也存在很多类似的东西真棒solo.waitForActivity()solo.assertMemoryNotLow()solo.takeScreenShot()

More examples about Robotiumusage you can find here by joining Robotium community.

通过加入Robotium 社区,您可以在此处找到有关Robotium使用的更多示例。