I would like to compare content of .txt file that i have in my assets folder with some text on the screen.
Usually when I assert text on the screen I use:
onView(withId(R.id.someId)).check(matches(withText("String")));
is ther any easy way so i can assert it from file?
Also, if you want to shorten your assertions and actions when using Expresso, check this library: https://github.com/SchibstedSpain/Barista (disclaimer: I'm a contributor).
It contains a set of quick actions and assertions that make the tests much more readable.
Here is a code to read text from text file.
StringBuilder buf=new StringBuilder();
InputStream json=getAssets().open("book/contents.json");
BufferedReader in=
new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
now compare your string from assets to you screen text.
buf.toString().equals("your text here");
Related
Is it possible to read a textfile on sdcard during runtime of an UIautomator test? As in an android application, using getExternalDirectory() etc. to create a File-object pointing to the actual file. Is it possible to send a command using getRuntime().exec("cmd"), if so, how? Or is there an easier way to simply access the device:s sdcard and read a file into the test?
The goal is to throughout the test send parameters to the test. So the test will perform certain actions, then continously look for a change on a file on the devices sdcard, and if so, read that line, and continue to perform actions. So therefor a way to read a file, and check certain things, is needed.
Or is there perhaps another way to pass information into the test during runtime? I know it can be done at the start of the testrun, but not during testrun.
I use below code inside uiautomator code to read text files.instead of
public void FileRead(String file_location) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file_location));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
//you can do whatever you want here or return String
} finally {
br.close();
}
}
This should be straight forward but for some reason when I try to count words in a file after I download it to my SD Card, the number seems to be off. Also the more occurrences there are, the further my result seems to be off. I use Microsoft Word to verify the number of occurrences (using ignore case and whole word only). To test the number of occurrences, I use the "the_counter" variable below. I also verified there is nothing wrong with download & the FULL file is downloaded to my SD card. This is driving me nuts -- I'm thinking Word cannot be wrong here so what could possibly be wrong with my code below?
Could it be white space or special chars in the file causing the problem --is there a way to clean the file to verify this?
//Find the directory for the SD Card using the API
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,TEMP_FILE);
//Read text from file
//StringBuilder text = new StringBuilder();
m_tree = new Tree();
int i=0;
BufferedReader br = null;
long the_counter=0;
try {
br = new BufferedReader(new FileReader(file));
String line;
String []arLine;
while ((line = br.readLine()) != null) {
//get each word in line
if(line.length()==0)
continue;
arLine = line.split("\\s+");
//now add each word to search tree
for(i=0;i< arLine.length;++i){
m_tree.insert(arLine[i]);
if(arLine[i].equalsIgnoreCase("a"))
++the_counter;
}
}
m_sTest = Long.toString(the_counter) ;
br.close();
I edited my code to read in each character per line and create words manually. and I STILL GET THE SAME RESULT.
br = new BufferedReader(new FileReader(file));
String line;
String []arLine;
StringBuilder word = new StringBuilder();
while ((line = br.readLine()) != null) {
//check for word at end of last line
if(word.length()>0){
m_tree.insert(word.toString());
word.setLength(0);
}
char[] lineChars = new char [line.length()];
line.getChars(0,line.length(),lineChars,0);
for(char c: lineChars){
if(c== ' '){
//if we have a word then store and clear then move on
if(word.length()>0){
m_tree.insert(word.toString());
word.setLength(0);
}
}
else{
word.append(c);
}
}
This is issue was that I was not accounting for special characters in between words: i.e:
this-is-four-words and not one . I'm not even sure that is proper grammar or writing but it was in this file and it certainly threw off my count.
I have a text file in res/raw/stringtest.txt. On each line of this there are different lines of text that need to be put into individual TextViews.
Where can I find the java code to read the text file and then put it into textViews?
I believe you can use a FileInputStream to output the text to the Logcat but that is as far as I know so far. I know you put some code into the class files but I am unsure what it is to make it place the Text into the TextViews by id. Any information or code will be very appreciated.
I would use a BufferedReader, for its readLine method. Something like:
BufferedReader br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.id.my_raw_file)));
String line = null;
while((line = br.readLine()) != null) {
// update TextView with string
}
I'm in the process of translating one of my apps to Spanish, and I'm having a character encoding problem with a raw HTML file I'm sticking into a WebView. I have the spanish translation of the file in my raw-es folder, and I'm reading it in with the following function:
private CharSequence getHtmlText(Activity activity) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.help), "utf-8"));
String line;
StringBuilder buffer = new StringBuilder();
while ((line = in.readLine()) != null) buffer.append(line).append('\n');
return buffer;
} catch (IOException e) {
return "";
} finally {
closeStream(in);
}
}
But everywhere there is a spanish character in the file, there is a diamond with a question mark inside of it when I run the app, and look at the activity that displays the HTML. I'm using the following to load the text into the WebView:
mWebView.loadData(text, "text/html", "utf-8");
I originally created the file in Microsoft Word, so I'm sure there is some sort of character encoding issue going on, but I'm not really sure how to fix it, and a Google search isn't helping. Any ideas?
Don't use loadData. Use loadDataWithBaseURL instead. You would say:
mWebView.loadDataWithBaseURL( null, text, "text/html", "utf-8", null );
I had a similar issue with a French translation where diamond symbols with question marks were appearing in place of certain characters, including those which I had escaped. I got around it by opening file properties in Eclipse and changing the encoding to "ISO-8859-1". Don't know if this would work for Spanish though.
i have a txt file which contains many chinese characters, and the txt file is in the directory res/raw/test.txt. I want to read the file but somehow i can't make the chinese characters display correctly. Here is my code:
try {
InputStream inputstream = getResources().openRawResource(R.raw.test);
BufferedReader bReader = new BufferedReader(
new InputStreamReader(inputstream,Charset.forName("UTF-8")));
String line = null;
while ((line= bReader.readLine())!= null) {
Log.i("lolo", line);
System.out.println("here is some chinese character 这是一些中文字");
}
} catch (IOException e) {
e.printStackTrace();
}
Both Log.i("lolo", line); and System.out.println("here is some chinese character 这是一些中文字") don't show characters correctly, i can not even see the chinese characters in the println() method.
What can i do to fix this problem? Can anybody help me?
In order to correctly handle non-ASCII characters such as UTF-8 multi-byte characters, it's important to understand how these characters are encoded and displayed.
Your console (output screen) may not support the display of non-ASCII characters. If that's the case, your UTF-8 characters will be displayed as garbage. Sometimes, you will be able to change the character encoding on the console. Sometimes not.
Even if the console correctly displayed UTF-8 characters, it's possible that your string does not correctly store the Chinese characters. You may think that it's correct because your editor displays them, but ensure that the character encoding of your editor also supports UTF-8.
I also was trying to figure out that. First you need to open the .txt file with the notepad and then click on File->Save as, there you will see a dropdown menu that says Enconding, so change it to UTF-8. After saving the file you should remove the .txt extension to the file and then add the file to the path res/raw and then you can refer to it from the code as R.raw.txtFileName.
That's all, i will put my code where I used the chinese characters and I could show them in the emulator.
If you have any other question, let me know because i am also developing something related with characters. Here is the code:
public List<String> getWords() {
List<String> contents = new ArrayList<String>();
try {
InputStream inputStream = getResources().openRawResource(R.raw.chardb);
BufferedReader input = new BufferedReader(new InputStreamReader(inputStream,Charset.forName("UTF-8")));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.add(line);
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents;
}