How to write test case to call next activity using Robotium.
Below Code i was tried but have an issue comparison error,
public void testCaseValidLogin() throws Exception {
solo.enterText(loginUserName,Username);
solo.enterText(loginPassword, Password);
solo.clickOnView(solo.getView(R.id.loginsuccess));
solo.assertCurrentActivity("Launch FileList Class", FileListActivityTest.class);}
Once i click login button, its intent to next activity test case class but i get comparison error in that line
[solo.assertCurrentActivity("Launch FileList Class",
FileListActivityTest.class);]
please help me... thanks
Try this:
solo = new Solo(Instrumentation instrumentation)
getActivity(), this will start the 1st Activity.
Reference: Can't get my assert currrent activity to work
Assertion error is because the code solo.assertCurrentActivity("Launch FileList Class",
FileListActivityTest.class); is executed before new Activity is launched.
Instead solo.waitForActivity(FileListActivityTest.class); should be the right way to verify is FileListActivityTest activity is launched.
Related
THis is the result i get after i login to the server. the main things i want to do is, after it login and will open another page which will list out all the data to another activity named MainActivity.
public void showJSON(String json){
ParseJson pj = new ParseJson(json);
pj.parseJson();
EventList cl = new EventList(this, ParseJson.ids,ParseJson.titles,ParseJson.descriptions);
listViewMain.setAdapter(cl);
}
I try to put this code inside my MainActivity, and called it from LoginActivity:
public void onResponse(String response) {
if(response.trim().equals("Login failed. Please enter the correct information.")){
Toast.makeText(Login.this, response, Toast.LENGTH_LONG).show();
}else{
MainActivity test = new MainActivity();
test.showJSON(response);
}
}
But it got the error which is no responding. The main purpose is i want to get the data and put into my MainActivity listview. What method should i try after login and get the data in my another activity.
What you are doing is correct, get the data in login activity and then pass it to Main activity using intent
With intent.putextra, you can set your data and call your next activity.
For details see below link.
http://developer.android.com/reference/android/content/Intent.html
I am trying to move from one activity to another activity. But some times seems it is hanging. On screen off and On it is working fine.
eg:
I am in Activity 1
Moved from Activity 1 to Activity 2
Seems it is hanged.
But if I do screen off and on it is on Activity 1.
Below is my Code sample:
Intent i;
i = new Intent(AdvSeatAvail.this, TrainsBetweenTwoStationsList.class);
i.putExtra("anim id in", R.anim.fragment_slide_right_enter);
i.putExtra("anim id out", R.anim.fragment_slide_left_exit);
i.putExtra("jsonvalue", jsonvalue);
i.putExtra("dateval", dateval);
AdvSeatAvail.this.finish();
AdvSeatAvail.this.startActivity(i);
overridePendingTransition(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit);
Some one please help me to resolve this Issue.
--
thanks,
Kiran
Use below lines of code for it..
Firstly call another activity than finish the class
Intent i;
i = new Intent(AdvSeatAvail.this, TrainsBetweenTwoStationsList.class);
i.putExtra("anim id in", R.anim.fragment_slide_right_enter);
i.putExtra("anim id out", R.anim.fragment_slide_left_exit);
i.putExtra("jsonvalue", jsonvalue);
i.putExtra("dateval", dateval);
AdvSeatAvail.this.startActivity(i); //// FIRTLY START THE CLASS AND THAN FINISH THE CLASS
AdvSeatAvail.this.finish();
overridePendingTransition(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit);
Try this:
finish();
Intent i = new Intent(AdvSeatAvail.this, TrainsBetweenTwoStationsList.class);
/* putExtra stuff */
startActivity(i);
Hopefuly it will help.
I've a method which is making a huge calculation and then calls an intent as follows
public void sampleMethod(final Context cont)
{
.
.
(huge calculation [50-80 lines])
.
.
Intent intent = new Intent(cont, TimesheetMain.class);
finish();
startActivity(intent);
}
This is present in Activity 'SampleActivity'. When I'm trying to access it through on object of Activity 'SampleActivity' from Activity 'B' as follows:
Context context = this;
SampleActivity sa = new SampleActivity();
sa.sampleMethod(context);
I'm getting a NullPointerException at the startActivity line of code while accessing it from Activity 'B'. I can't figure out where am i going wrong in here. Please help me out
EDIT 2
This seem to work when i added context to it like cont.startActivity(intent), but i need to know why shouldn't i use another class or another activity's function in a secondary class? Is the android framework is the reason? I've been doing this (without the intent part) for the past two months or so, i never faced any sudden force close issues in either emulator or in device(Nextbook professional 7 SE); Please explain it with a legit example
You're not supposed to create explicit instances of activities by yourself as you're doing like this:
SampleActivity sa = new SampleActivity();
Please provide a better description for your problem and what you want to achieve with the outcome of this issue.
try the follwng updated code:
public void sampleMethod(final Activity cont)
{
Intent intent = new Intent(cont, TimesheetMain.class);
cont.finish();
cont.startActivity(intent);
}
also move this method to a util class and call it from activity and pass the activity reference as follows
class ActivityB extends Activity
{
.
.
.
Util.sampleMethod(this);
}
I managed to write two test cases in my XXTest.java with robotium-solo-3.2.1.jar included, luckily in JUnit view it shows the first one is done, which the device exactly worked (on emulator too).
Then it proceed to the second one, but it just hanging there forever! sorry I can't attach screen shot with my account.
here are my code:
public class XXTest extends ActivityInstrumentationTestCase2<SignInActivity> {
private Solo solo;
private Activity mActivity;
private static final String account = "someone";
private static final String pwd = "123456";
#SuppressWarnings("deprecation")
public XXTest() {
super("com.acompany.android", SignInActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
this.mActivity = getActivity();
solo = new Solo(getInstrumentation(), mActivity);
}
#Smoke
public void testLogIn() throws Exception {
EditText accountInput = (EditText) solo.getView(R.id.edit_account);
EditText pwdInput = (EditText) solo.getView(R.id.edit_password);
solo.clearEditText(accountInput);
solo.clearEditText(pwdInput);
solo.enterText(accountInput, account);
solo.enterText(pwdInput, pwd);
solo.clickOnButton(mActivity.getResources()
.getString(R.string.text_sign_in));
solo.waitForActivity("MainActivity");
solo.assertCurrentActivity("Expect MainActivity shown...", "MainActivity");
boolean expected = true;
boolean actual = solo.searchButton(mActivity.getResources().getString(
R.string.welcome_dialog_start));
assertEquals("find friend dialog not shown", expected, actual);
}
#Smoke
public void testOpenPref() throws Exception {
solo.goBack();
solo.clickOnMenuItem(mActivity.getResources().getString(
R.string.text_preferences));
solo.assertCurrentActivity("Expected PrefActivity..", "PrefActivity");
solo.goBackToActivity("MainActivity");
solo.assertCurrentActivity("Expected MainActivity..", "MainActivity");
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
I've searched the sample of NotePadTest.java from Robotium tutorial, those 3 test cases in it are just work fine!
Please tell me where goes wrong?? Am I missing something somewhere? why the second test case not running?
btw. Can there be more than one class extends ActivityInstrumentationTestCase2 in a test project? curious!
You need to use solo.finishOpenedActivities() in your tearDown().
#Robert - this is the issue with Activity testing itself , not to robotium specific .
For the first test method:
the basic flow you is like below:
1>in the setUp() method load the main activity (say MainActivity) > do some ops in your testMethod1() - that results to land you in another activity ( say AnotherActivity) > and in tearDown() you kill the launched activity in setUp() method which is MainActivity
note: but AnotherActivity remains live
For the second test method:
the basic flow you is like below:
2>in the setUp() method try loading the main activity (say MainActivity) again ,although the previously launched AnotherActivity is not yet got killed, so it hangs there , It doesnt even enter the testMethod2() fr execution -
note: the eclipse graphical runner shows it hangs while the execution marker points to the testMethod2() , people thinks that it got stuck in testMethod2() - but the problem lies in setUp() for testMethod2() not in testMethod2() itself.
Solution:
1>Just keep a look on your cross activity navigation - at the end of each testMethod() use solo.goBack() to come back to the same main activity MainActivity from your current Activity ( you got landed in because of your actions)
results - at the end of testMethod1() only the main activity which was opened in setUP() remains. So it gets killed successfully .No Activity remains alive and for the testMethod2().The setUP() is able to load the MainActivity again without a hang - and testMethod2() gets executed .
hope it clarifies :)
I have 2 activities, the first one is the startup one which in it's create method causes the second one to be launched, always.
My Robolectric tests pass fine
Activity
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginview);
Intent intent = new Intent(this,MainActivity.class);
startActivity( intent );
}
I know my activity works fine cause it launches in my device and on the emulator
My Robolectric tests
public void testLoginFirstTime() throws Exception
{
LoginActivity activity = new LoginActivity();
activity.onCreate(null);
assertThat(activity, new StartedMatcher(MainActivity.class));
}
My Robotium test
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testLoginFirstTime() throws Exception
{
solo.assertCurrentActivity("Expected MainActivity to launch", MainActivity.class);
}
What is wrong with my robotium assertion? it always thinks the current activity is login one even though as I watch the emulator/device I can see that Robotium does actually launch the MainActivity but it doesn't appear to know that the new activity has been launched.
Edit: Meant to say if I add a button to my login view and launch the new activty via a button click then Robotium performs the click and detects the new activity has been launched ok.
Edit:
Looks like its a Robotium limitation
http://groups.google.com/group/robotium-developers/browse_thread/thread/79a70038c16e35e6
However it still leaves me with the issue of how to test my app with robotium the same way as a user will use it, ie, not cheating and starting in a different activity :(
You need to use the constructor solo = new Solo(Instrumentation instrumentation) and then after you have created the Solo object you call getActivity() in order to start the first Activity. Then it will work.