Passing String Array to A Robotium Class - android

I am automating a product using Robotium. In a module I have to validate the data derived.
For that I am using this code:
class sharefirstlevel {
public void A {
for(int k=1;k<=4;k+=2) {
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++) {
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d("xyz","Values of the Strings are"+Actualvalues[i].toString());
}
}
}
}
Its extracting the values selected to an array.
Now when its derived, to validate I am using this code:
class sharedLevel {
public void B {
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++) {
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
}
}
}
But the thing is in the first part of code proper values are extracted to the Array.
In the second part of code when I am passing the array values, I am not getting the values but the memory allocation object. Hence the test case is failing.
Please help me. Am at a fix !!

How about using
assertTrue(Arrays.equals(expected, actual));
EDIT: This answer is for the question for which the bounty was raised and not the edited question (https://stackoverflow.com/posts/18334315/revisions)

I solved the Problem. The change which I had to make in Class B was
for(int i=1;i<sharefirstlevel.Actualvalues.length;i++){
Log.d("Pratibha","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("TAG","Values are:"+sharefirstlevel.Actualvalues[i]);
}
And hence I am getting the values of the array in class B. Because the index started from 0, and I din't have any text values at 0th position, It was returning garbage values.
Thanks all for the responses.

Related

unable to retrieve values from two different arraylist

i am having two different arraylist of diffrent size,my problem is that i have to retrieve values from both the list and show that on to a textview,when i am using two loops it is first completing the inner loop and than executing outer loop and the vales are getting printed two times and if using break to break the inner loop it is completely ignoring inner loop after 1st loop.
ArrayList<LocationDto>location = new ArrayList<>();
education<EducationDto> = new ArrayList<>();
if (education.size() != 0) {
for (int j = 0; j < education.size(); j++) {
for (int k = 0; k < location.size(); k++) {
if (!education.get(j).getSpecializationTitle().equalsIgnoreCase("") && !location.get(k).getLocationName().equalsIgnoreCase("")) {
tvEducation.append(education.get(j).getEducationTitle() + "(" + education.get(j).getSpecializationTitle() + ")" + " Located at " + location.get(k).getLocationName());
} else if (education.get(j).getSpecializationTitle().equalsIgnoreCase("") && !location.get(k).getLocationName().equalsIgnoreCase("")) {
tvEducation.append(education.get(j).getEducationTitle() + " Located at " + location.get(j).getLocationName());
} else if (!education.get(j).getSpecializationTitle().equalsIgnoreCase("") && location.get(k).getLocationName().equalsIgnoreCase("")) {
tvEducation.append(education.get(j).getEducationTitle() + "(" + education.get(j).getSpecializationTitle() + ")");
} else {
tvEducation.append(education.get(j).getEducationTitle());
}
if (j != education.size() - 1) {
tvEducation.append(" , ");
}
break;
}
}
} else {
tvEducation.setText("Not Specified");
tvEducation.setTextColor(getResources().getColor(R.color.color_three));
}
what should i do now?
The break statement terminates a loop regardless of whether it has completed or not, so your problem is that you've put a break directly inside your inner for loop without some sort of condition. Therefore the first time the loop runs it will reach the break which is why it's terminating on the first iteration!
You probably mean to put the break inside the last if statement?

Multiple OR statements in ORMLite

According to this post :Multiple, combined OR conditions in ORMLite
I want to make queries that can depend on ArrayList of condition and Strings in function
List<String> cat = new ArrayList<String>();
List<Person> line = new ArrayList<>();
Dao<Person, Integer> personDAO = getHelper().getPersonDAO();
cat.add("category1");
cat.add("category2");
QueryBuilder<Person, Integer> queryBuilder = personDAO.queryBuilder();
Where<Person, Integer> where = queryBuilder.where();
if(filterOn)
for (int i = 0; i < cat.size(); ++i)
where.eq("category", cat.get(i)).or().eq("name", cat.get(i));
where.or(cat.size());
if(searchBar.size()!=0){
for (int i = 0; i < cat.size(); ++i)
where.eq("category", cat.get(i)).or().eq("name", cat.get(i));
where.like("category", constrainName).or().like("name", constrainName);
where.or(cat.size()+1);
}
line = personDAO.queryBuilder().query();
But Ive got throw out of application without any exception whenever the first iteration of loop is finished
UPDATE: I solve my problem. solution was:
for (int i = 0; i < cat.size(); ++i)
where.eq("category",cat.get(i)).or().eq("name",cat.get(i));
where.or(cat.size());
if(data){
where.like("category", t).or().like("name", t);
l = personDAO.query(where.and(2).prepare());
} else
l = personDAO.query(where.or(1).prepare());
But I've got throw out of application without any exception whenever the first iteration of loop is finished
Ok, first off, unless the JVM quit on you there was an exception, just that it was not properly caught or logged somehow. If you enable logging I suspect the exception would have explained the problem.
There are number of problems with your code. I'm not sure which one (or something else) is causing the exception to be thrown. The first problem is:
if (filterOn) {
for (String entry : cat) {
where.eq("category", entry);
where.eq("name", entry);
}
where.or(cat.size() * 2);
}
The fix here is to only add the ORs if the filterOn is set. Otherwise you would have generated an invalid query. I recommend always using {} so you can see these sorts of issues. I do one call to where.or(...) with the size * 2 for the category and name.
if(searchBar.size()!=0){
So this seems to be adding the category and name equals comparisons in as well. If filterOn and !searchBar.isEmpty() can be true at the same time then you are going to get duplicate WHERE entries which is inefficient. However, it might also generate an incomplete query because if you add a number of OR entries that match the category/name and then do it again, you need one more OR to link the two sets. For example:
if (filterOn) {
where.eq("category", entry);
where.eq("name", entry);
where.or(2);
}
if (!searchBar.isEmtpy()) {
where.eq("category", entry);
where.eq("name", entry);
where.like("category", entry);
where.like("name", entry);
where.or(4);
}
// MISSING LAST OR HERE if both conditions are true
This code would generate an invalid query exception because you are missing the last OR which ties the two sets of comparisons together.
What I would recommend is to count the number of ORs added to the query:
int orCount = 0;
if (filterOn) {
// loop here
for loop {
where.eq("category", entry);
where.eq("name", entry);
orCount += 2;
}
}
if (!searchBar.isEmtpy()) {
for loop {
where.eq("category", entry);
where.eq("name", entry);
orCount += 2;
}
where.like("category", entry);
where.like("name", entry);
orCount += 2;
}
where.or(orCount);
This will put the appropriate number of ORs into your query at the end.
Couple other comments:
cat should be cats at least to show that it is storing multiple. Same for line.
Consider using the for (String cat : cats) type of for instead of using for (int i = 0; ...) { ... cats.get(i) ... }. The first type is less prone to errors with the counter.

Which Json structure suitable for this work?

This function generate dummy data for this Structure .
private void createDummyData() {
for (int i = 1; i <= 10; i++) {
VerticalDataModel dm = new VerticalDataModel();
dm.setSectionTitle("Section " + i);
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 10; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setSingleItemModelha(singleItem);
allSampleData.add(dm);
}
Know for generating this data from internet , i need json structor for this work .(I use volley library)
Thanks <3
{
data : [
{
"title":"section1",
"items":[{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]
},
{
"title":"section2",
"items":[{"name":"item3","url":"url3"},{"name":"item4","url":"url4"}]
}
]
}
so basically you have array of objects which contains title and items as its property. Again items is another array of objects which contains name and url as its property.

Get first argument in live-template

Recently i was creating my own live-template, i was customizing for loop, here is how default for loop is given in live-template.
for(int $INDEX$ = 0; $INDEX$ < $LIMIT$; $INDEX$++) {
$END$
}
but i want my method's first argument inplace of $LIMIT$, how can i do that?
public void getList(ArrayList<String> list)
{
}
then my for loop shoulde be
for(int i = 0; i < list.size; i++) {
...
}
I have seen template of logm, but it is printing all arguments of method
groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
You can add the following live template:
for(int $INDEX$ = 0; $INDEX$ < $VAR$.size(); $INDEX$++){
$END$
}
Then go to right button "Edit variables" and put the following predefined methods in the expressions fields:
You can find all predefined methods in Jetbrains documentation

Null pointer exception on consecutive execution of the test script

I am automating a product using Robotium. In a module I have to validate the data derived.
For that I am using this code:
class sharefirstlevel {
public void A {
for(int k=1;k<=4;k+=2) {
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++) {
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d("xyz","Values of the Strings are"+Actualvalues[i].toString());
}
}
}
}
Its extracting the values selected to an array.
Now when its derived, to validate I am using this code:
class sharedLevel {
public void B {
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++) {
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
}
}
}
The values are getting extracted to the array and when printed, the proper values are getting inseted to the array and are even getting passed to another class.
But the execution of the junit test case will be successful only for 1 or 2 times. But from then i am getting a null pointer exception. Please help me. Am at a fix!!
The first value while getting inserted to the ArrayList itself is null. Here is the Code:
for(int k=1;k<=4;k+=2)
{
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++)
{
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d(" ","Values of the Strings are"+Actualvalues[i].toString());
}
}
The value getting inserted at the first line within the for loop itself is null. Hence the problem.
But I am not getting, why null value is being inserted at 0th position. Please help me out for solutions.
In your sharedLevel class, change:
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++)
to:
for(int i=0; i < sharefirstlevel.Actualvalues.length; i++)
Since your counter starts at 0, it should less than the length of your array.

Categories

Resources