简体   繁体   中英

How to use R.id in robotium if I have only the apk file

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 cannot be resolved to a variable

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?

my 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();

              }
            }

and manifest

<?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>

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

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

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"));

Use getView(String id) which was introduced in robotium 4.2 . It should look like this:

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

The first step is to resign the apk you are going to test.

Then I use scripts like below:

View view = solo.getView(com.test.app.R.id.resourceId);
solo.clickOnView(view);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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