UISelector Using descriptionContains - android

I'm working on the automation script, where I'm trying to click on the element based on content-desc, The code I tried is
app_element = driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains("Subway Surfers")')
app_element.click()
This works for me
But I want to know is it possible to store the 'Subway Surfers' in a string variable and assign it inside the descriptionContains("Subway Surfers")') like
content_description = 'Subway Surfers'
app_element = driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains(content_description)')
Like this is it possible can any one help me on this

Try this:
content_description = 'Subway Surfers'
app_element = driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains('+content_description+')')

Related

How to check if a string has a specified character?

I am new to android studio and kotlin. I need to find a way to check if a string contains a char, which is, in this case, "/"
I want to form a piece of code in the following manner:
if (string input contains a character "/") = true {
<code>
}
else{
<code>
}
Please tell me how to do this, and if possible, give me the code I'll need to specify as the condition.
You can use contains, like this:
val a = "hello/"
val b = a.contains("/")
When the string has the character will return true.

Json object returns with brackets

I'm trying to parse an Json object and it has this data:
"ingredients":["White bread","Bratwurst","Onions","Tomato ketchup","Mustard","Curry powder"]
When I return the value, the textview prints this :
[White bread,Bratwurst,Onions,Tomato ketchup,Mustard,Curry powder]
I want to remove the brackets but I cant. I'm doing this so far:
List<String> mIngredientArray = new ArrayList<>();
JSONArray ingredients = null;
Sandwich mSandwich = new Sandwich();
ingredients = mJsonObject.getJSONArray("ingredients");
for(int i=0; i<ingredients.length();i++){
mIngredientArray.add(ingredients.getString(i));
}
mSandwich.setIngredients(mIngredientArray);
The textview code:
ingredientsTV.setText(sandwich.getIngredients().toString());
But doesnt works. Any idea about how to solve it?
REQUIRED OUTPUT:-
White bread, Bratwurst, Onions, Tomato ketchup, Mustard, Curry powder
If you just want to remove the brackets, you can use regex like this
strIngredients = sandwich.getIngredients().toString();
strIngredients = strIngredients.replaceAll("\\[", "").replaceAll("\\]","");
ingredientsTV.setText(strIngredients);
You could just replace the bracket characters as suggested, but that doesn’t strike me as the most direct solution. You need to specify how the List object is converted to a string — something like:
String.join(“, “, mIngredientArray)
Finally, I used this line of code to solve my problem:
ingredientsTv.setText(sandwich.getIngredients().toString().replace("[","").replace("]",""));
When I tried to use ReplaceAll, the IDE said that I need to use 24 as minimunSdkVersion but I was using 16 so that's the reason I use replace instead of ReplaceAll. And it works finally.

Android TextView not showing multiple lines, even though String has newlines [duplicate]

For the input text:
<p>Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?
I run the following code:
Whitelist list = Whitelist.simpleText().addTags("br");
// Some other code...
// plaintext is the string shown above
retVal = Jsoup.clean(plaintext, StringUtils.EMPTY, list,
new Document.OutputSettings().prettyPrint(false));
I get the output:
Arbit string <b>of</b>
text. <em>What</em> to <strong>do</strong> with it?
I don't want Jsoup to convert the <br> tags to line breaks, I want to keep them as-is. How can I do that?
Try this:
Document doc2deal = Jsoup.parse(inputText);
doc2deal.select("br").append("br"); //or append("<br>")
This is not reproducible for me. Using Jsoup 1.8.3 and this code:
String html = "<p>Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?";
String cleaned = Jsoup.clean(html,
"",
Whitelist.simpleText().addTags("br"),
new Document.OutputSettings().prettyPrint(false));
System.out.println(cleaned);
I get the following output:
Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?
Your problem must be somewhere else I guess.

Android, how to replace slashes in path

i have an image path : "UploadFile\\/UserProfile\\/Female.jpg". How to replace "\\/" with "/"?
i have tried :
replaceAll("\\/","/")
replaceAll("\\\\/","/")
Code:
adapter_profilepic = objUser.getProfilePicture().replaceAll("\\/","/");
This works :
String src = "UploadFile\\/UserProfile\\/Female.jpg";
src = src.replaceAll("\\\\/","/");
System.out.println(src);
output:
UploadFile/UserProfile/Female.jpg
You said you tried it, but check it again.
According documentation your string is used as regular expression. Why you don't using replace:
String adapter_profilepic = objUser.getProfilePicture().replace("\\\\/","/");

android dynamical binding

I want to work dynamically therefore I want to bind text views dynamically I think an example would explain me the best
assuming I want to bind 7 image views i can do it like this :
Country = (EditText)findViewById(R.id.CountryEditText);
City = (EditText)findViewById(R.id.CityEditText);
LivinigCreture = (EditText)findViewById(R.id.LivingCretureE);
Nature =(EditText)findViewById(R.id.NatureEditText);
Inanimate = (EditText)findViewById(R.id.InanimateEditText);
KnowenPersonality = (EditText)findViewById(R.id.KnowenPersonalityEditText);
Occupation = (EditText)findViewById(R.id.OccupationEditText);
but lets change 7 with NUMOFFILEDS as a final where i want to do the previous ?
myImages = new ImageView [7];
for (int i = 0; i<7;i++,????)
myImages[i] = (ImageView)findViewById(R.id.initialImageView01);
notice : in my R file the R.id.initialImageView01 - R.id.initialImageView07 are not generate in a cont gap between them therefore I don't know how to make this architecture possible .
and if there's a way can someone show me an example how to work dynmiclly (like using jsp on android combined way or something ?)
id its possiable to do so constant times is it possible to build an the same xml constant num of times like jsp does
thank u pep:)
You can store the IDs themselves in an array at the beginning of your Activity; that way you'll only need to write them once and you can index them afterwards.
Something like:
int[] initialImageViewIds = {
R.id.CountryEditText,
R.id.CityEditText,
R.id.LivingCretureE,
R.id.NatureEditText,
R.id.InanimateEditText,
R.id.KnowenPersonalityEditText,
R.id.OccupationEditText
};
Then you can access them with:
myImages = new ImageView [7];
for (int i = 0; i<7;i++) {
myImages[i] = (ImageView)findViewById(initialImageViewIds[i]);
}
If that's not enough and you really want to get the IDs dynamically, I suppose you can use reflection on the R.id class, possibly with something like R.id.getClass().getFields() and iterate on the fields to check if their names interest you. Check reference for the Class class, too.

Categories

Resources