简体   繁体   English

使用Robotium .getString()方法从R.string返回错误

[英]Wrong return from R.string using Robotium .getString() method

I try to get Android settings from R.string using Robotium 我尝试使用Robotium从R.string获取Android设置

String loginButton = solo.getString(test_project_package.R.string.login_button);

string.xml file contain (located in the test project): string.xml文件包含(位于测试项目中):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="login_button">Sign In to App.</string>
</resources>

Problem is in the returning value. 问题在于返回值。 Instead of "Sign In to App." 而不是“登录到应用程序”。 , Java return "res/layout/bottom.xml" . ,Java返回“ res / layout / bottom.xml” It seems that it is a path from source code of app. 看来这是来自应用程序源代码的路径。 that is in the same workspace). 在同一工作区中)。

I tried not to use solo: 我试着不单独使用:

Resources resources = getInstrumentation().getTargetContext().getResources();
String loginButton = res.getString(test_project_package.R.string.login_button);

Same problem. 同样的问题。

What is wrong? 怎么了? Why Java returns an incorrect value? 为什么Java返回错误的值?

Partially solved this problem :) 部分解决了这个问题:)

Add the following import in the test project: 在测试项目中添加以下导入:

import project_package.R;

After that it is possible to use settings from source code. 之后,可以使用源代码中的设置。 Java returns right parameters. Java返回正确的参数。

solo.clickOnButton(solo.getString(R.string.add_account_button));

But! 但! If you want to get parameters from test project and set import like this: 如果要从测试项目中获取参数并设置导入,如下所示:

import project_package.test.R;

or just without import Java will return wrong parameter. 或仅不导入Java就会返回错误的参数。 Don't know why 不知道为什么

I found this tip in article Android Testing with the Android Test framework, Robotium, Monkey and Robolectric 我在使用Android测试框架Robotium,Monkey和Robolectric的Android测试文章中找到了这个技巧

I think solo.getString() is not to fetch the strings from String.xml It is to get the strings associated with the views in current activity. 我认为solo.getString()并不是要从String.xml中获取字符串,而是要获取与当前活动中的视图关联的字符串。

To get the strings from String.xml use the following : 要从String.xml获取字符串,请使用以下命令:

solo.getContext().getString( resId );

in your case - 就您而言-

solo.getContext().getString( R.string.login_button );

should work fine. 应该工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM