如何使用R.id在robotium如果我只有apk文件如何使用、文件、robotium、id

2023-09-08 10:18:07 作者:酒蚀离心人

我想测试从游戏市场的应用程序。我有当我真的试图用一个问题

I want to test an app from play market. I have a problem when I`m trying to use

solo.clickOnView(solo.getView(cn.wps.moffice_eng.R.id.writer_edittoolbar_saveBtn));

CN - CN不能被解析为一个变量

cn - cn cannot be resolved to a variable

我怎样才能解决这个poblem?据我了解,robotium不能使用R.id,因为我没有从我的测试应用程序有R.id文件?

How can i solve this poblem? As I understand, robotium can't use R.id because I don't have R.id file from my tested app?

我的code

    package com.example.android.apis.test;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;

import com.jayway.android.robotium.solo.Solo;



            @SuppressWarnings("unchecked")
            public class Test extends ActivityInstrumentationTestCase2 {

                    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "cn.wps.moffice.documentmanager.PreStartActivity";

                    private static Class<?> launcherActivityClass;
                    static{
                            try {
                                    launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
                            } catch (ClassNotFoundException e) {
                                    throw new RuntimeException(e);
                            }
                    }

                    @SuppressWarnings("unchecked")
                    public Test() throws ClassNotFoundException {
                            super(launcherActivityClass);
                    }

                    private Solo solo;

                    @Override
                    protected void setUp() throws Exception {
                            solo = new Solo(getInstrumentation(), getActivity());
                    }

                    public void testSimple() {

                        solo.sleep(2000);                   
                        solo.clickOnButton(1);
                        solo.sleep(2000);
                        solo.clickOnImage(6);
                        solo.sleep(2000);
                        solo.clickInList(0);
                        solo.sleep(5000);

                        solo.sendKey(KeyEvent.KEYCODE_P);
                        solo.sendKey(KeyEvent.KEYCODE_R);
                        solo.sendKey(KeyEvent.KEYCODE_O);

                        solo.sendKey(Solo.ENTER);
                        solo.sleep(2000);


                        solo.clickOnView(solo.getView(cn.wps.moffice_eng.R.id.writer_edittoolbar_saveBtn));

                    }


                    @Override
                    public void tearDown() throws Exception {
                            solo.finishOpenedActivities();

              }
            }

和清单

<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.example.android.apis.test">

    <uses-sdk android:minSdkVersion="13"/>

    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.wps.moffice_eng"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <uses-library android:name="android.test.runner"/>
    </application>

</manifest>

我的解决办法:我找到了解决办法 - 我反编译apk文件末尾拉R.id文件到robotium项目 - 这是我的问题的关键时刻

MY SOLUTION: I found the solution - I decompiled apk file end pull R.id file to the robotium project - that was the key moment of my question

推荐答案

作为@Renas建议, getView(字符串ID)是你所需要的。

As a @Renas suggested, getView(String id) is what you need.

在 robotium 5.0.1 您应该只使用 ID字符串,而不是整个名字。它应该是这样的:

In robotium 5.0.1 you should use just id string, not the whole name. It should look like this:

solo.clickOnView(solo.getView("resourceId"));