xiaofans / robotium

Automatically exported from code.google.com/p/robotium
0 stars 0 forks source link

junit.framework.AssertionFailedError: 2131230724 Buttons are not found! #451

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

I'm new to Robotium so i'm trying to perform a simple test on an android 
calculator app.
i have 2 editTexts, a textView for result and a button "Addition":

<EditText
        android:id="@+id/EDtxt1"
        android:inputType="numberDecimal"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:hint="Enter First Number"/>

    <EditText
        android:id="@+id/EDtxt2"
        android:inputType="numberDecimal"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:hint="Enter Second Number"/>

    <TextView
        android:id="@+id/resultTvID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="No Result "
        android:textSize="25dp" />

    <Button 
        android:id="@+id/addBtnID"
        android:text="Addition"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_gravity="center"/>

In my robotium coding i want to enter 2 numbers & click on the "Addition" 
button.
But, i keep getting the error "junit.framework.AssertionFailedError: 2131230724 
Buttons are not found!" on button click.

this is the robotium coding:
@Test public void testAddBtn(){

        //solo.assertCurrentActivity("Check on startup", MainActivity.class);

        String appName = solo.getCurrentActivity().getClass().getSimpleName();
        solo.assertCurrentActivity("Check on startup", appName); 
        solo.waitForActivity(appName);

        EditText editText1 = (EditText) solo.getView(com.example.simplecalculator.R.id.EDtxt1);
        EditText editText2 = (EditText) solo.getView(com.example.simplecalculator.R.id.EDtxt2);
        //Button button = (Button) solo.getView(com.example.simplecalculator.R.id.addBtnID);

        solo.enterText(editText1, String.valueOf("10"));
        solo.enterText(editText2, String.valueOf("5"));
        solo.goBack();
        solo.clickOnButton(com.example.simplecalculator.R.id.addBtnID);
}

I'm using robotium-solo-4.1.jar with sdk17.

Thank you in advance.
:)

Original issue reported on code.google.com by khelifa....@gmail.com on 9 May 2013 at 2:28

GoogleCodeExporter commented 9 years ago
ClickOnButton() is used with index. What you want to do is clickOnView(button); 
Please keep this tracker for issues only.

Original comment by renasr...@gmail.com on 9 May 2013 at 5:37

GoogleCodeExporter commented 9 years ago
this didn't work too:
        Button msButton = (Button) solo.getCurrentActivity().findViewById(
                com.example.simplecalculator.R.id.addBtnID);

        solo.clickOnView(msButton);

in the manifest xml, i also added
<uses-sdk android:targetSdkVersion="YOUR_VERSION" /> 
<supports-screens android:anyDensity="true"/> 

i still get the same error.

Original comment by khelifa....@gmail.com on 10 May 2013 at 9:34

GoogleCodeExporter commented 9 years ago
Please paste your test case. 

Original comment by renasr...@gmail.com on 10 May 2013 at 9:37

GoogleCodeExporter commented 9 years ago
package com.example.simplecalculator.test;

import org.junit.Test;

import com.example.simplecalculator.MainActivity;
import com.jayway.android.robotium.solo.Solo;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;

public class testAddButton extends
        ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo;

    public testAddButton() {
        super(MainActivity.class);
    }

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

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    @Test public void testAddBtn(){

        Button msButton = (Button) solo.getCurrentActivity().findViewById(
                com.example.simplecalculator.R.id.addBtnID);

        solo.clickOnView(msButton);

    }

}

Original comment by khelifa....@gmail.com on 10 May 2013 at 9:46

GoogleCodeExporter commented 9 years ago
Do this:

Button msButton = (Button) solo.getView(
                com.example.simplecalculator.R.id.addBtnID);
solo.clickOnView(msButton);

Original comment by renasr...@gmail.com on 10 May 2013 at 9:48

GoogleCodeExporter commented 9 years ago
now i'm getting:
junit.framework.AssertionFailedError: Click can not be completed!
at com.jayway.android.robotium.solo.Clicker.clickOnScreen(Clicker.java:85)
at com.jayway.android.robotium.solo.Clicker.clickOnScreen(Clicker.java:157)
at com.jayway.android.robotium.solo.Clicker.clickOnScreen(Clicker.java:131)
at com.jayway.android.robotium.solo.Solo.clickOnView(Solo.java:879)
at 
com.example.simplecalculator.test.testAddButton.testAddBtn(testAddButton.java:36
)
at java.lang.reflect.Method.invokeNative(Native Method)
at 
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at 
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at 
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTes
tCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at 
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:55
5)
at 
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

does that mean the the button is beeing clicked on & there is another problem.

the expected result when i click on the button is a TextView takes "Hello". 
What exactly can go wrong.

Original comment by khelifa....@gmail.com on 10 May 2013 at 10:14

GoogleCodeExporter commented 9 years ago
You need to follow the tutorial "Test Android Calculator Application with 
Robotium" :

http://code.google.com/p/robotium/wiki/RobotiumTutorials

Original comment by renasr...@gmail.com on 10 May 2013 at 10:16

GoogleCodeExporter commented 9 years ago
Also see: "Why do text and button clicks get wrong?" 

http://code.google.com/p/robotium/wiki/QuestionsAndAnswers 

/Renas 

Original comment by renasr...@gmail.com on 10 May 2013 at 10:31

GoogleCodeExporter commented 9 years ago
by the way i'm using 4.2.2, API17

i downloaded the 2 projects AndroidCalsulator & AndroidCalculatorTest.
when u run the AndroidCalculatorTest.

i get the error:
junit.framework.AssertionFailedError: EditText is not found!
at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:492)
at com.jayway.android.robotium.solo.Solo.enterText(Solo.java:1488)
at com.calculator.test.TestMain.testDisplayBlackBox(TestMain.java:26)
at java.lang.reflect.Method.invokeNative(Native Method)
at 
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at 
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at 
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTes
tCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at 
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:55
5)
at 
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

the problem happens in 1st
        EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);
        solo.enterText(FirsteditText, String.valueOf(firstNumber));

i think the problem is more than how to create the test case
could it be a framework issue?
should i use an older API?

Original comment by khelifa....@gmail.com on 10 May 2013 at 11:30

GoogleCodeExporter commented 9 years ago
Hi i've got couple of things

1. In case of your main problem 

//solo.assertCurrentActivity("Check on startup", MainActivity.class);
String appName = solo.getCurrentActivity().getClass().getSimpleName();
   solo.assertCurrentActivity("Check on startup", appName); 
    solo.waitForActivity(appName);

   EditText editText1 = (EditText) solo.getView(com.example.simplecalculator.R.id.EDtxt1);
    EditText editText2 = (EditText) solo.getView(com.example.simplecalculator.R.id.EDtxt2);
        //Button button = (Button) solo.getView(com.example.simplecalculator.R.id.addBtnID);

        solo.enterText(editText1, String.valueOf("10"));
        solo.enterText(editText2, String.valueOf("5"));
        solo.goBack();
        solo.clickOnButton(com.example.simplecalculator.R.id.addBtnID);
}

why are you using "solo.goBack()" ? , it is the one cause of issue. 

Original comment by luckynav...@gmail.com on 10 May 2013 at 12:15

GoogleCodeExporter commented 9 years ago
2. Secondly 

solo.clickOnButton() need index not the id of view. 

Original comment by luckynav...@gmail.com on 10 May 2013 at 12:19

GoogleCodeExporter commented 9 years ago
About 

junit.framework.AssertionFailedError: EditText is not found!

Issues with AndroidCalculatorTest, I've just run it on Galaxy S4 with 4.2.2, 
its working fine here. 

Can you please tell me are you using on real device ? what is the device info?

Original comment by luckynav...@gmail.com on 10 May 2013 at 12:24

GoogleCodeExporter commented 9 years ago
Thank you both so much for your help,
i figure out the issue & it's not related to the code itself

when i change the android device to Nexus S it worked.
on other devices, lower performnce, it didn't work. it didn't find the button.
which could be understandable because maybe the button is too big for the 
screen itself or something like that.
thank you so much again.
my problem is officially solved.
kais.

Original comment by khelifa....@gmail.com on 10 May 2013 at 12:25

GoogleCodeExporter commented 9 years ago
yes luckynav, that was the problem exactly

Original comment by khelifa....@gmail.com on 10 May 2013 at 12:32