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

跟風遠走 提交于 2020-01-15 01:55:05

问题


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


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/17879103/how-to-use-r-id-in-robotium-if-i-have-only-the-apk-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!