Has anyone have a clue to how to use Toast on Appium 1.6.3?
Android - Uiautomator2
Add ability to verify TOAST messages (these can't be interacted with, only
text retrieval allowed)
It looks like it's supported but there's very few threads out there that are helpful.
Hi Dear Hamza
I have follow your suggestion:
1-pip install tesseract
2-pip install pytesseract
3-pip install pillow
4-pip install tesseract-ocr
then I do not know how to start the codes to take capture of the elements inside the toast?
I might need your help on the detailed steps for that!!!
thanks very much and appreciated.
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;
public class BasicExample {
public static void main(String[] args) {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(null, "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
// Open input image with leptonica library
PIX image = pixRead(args.length > 0 ? args[0] : "/usr/src/tesseract/testing/phototest.tif");
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
System.out.println("OCR output:\n" + outText.getString());
// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}
}
Appium Directly does not give any API to read toast message we need to do it using tess4j jar. First we need to take screen shot and then we need to read the text from screen shot using tess4j API.
static String scrShotDir = “screenshots”;
File scrFile;
static File scrShotDirPath = new java.io.File("./"+ scrShotDir+ “//”);
String destFile;
static AndroidDriver driver = null;
public String readToastMessage() throws TesseractException {
String imgName = takeScreenShot();
String result = null;
File imageFile = new File(scrShotDirPath, imgName);
System.out.println(“Image name is :” + imageFile.toString());
ITesseract instance = new Tesseract();
File tessDataFolder = LoadLibs.extractTessResources(“tessdata”); // Extracts
// Tessdata
// folder
// from
// referenced
// tess4j
// jar
// for
// language
// support
instance.setDatapath(tessDataFolder.getAbsolutePath()); // sets tessData
// path
result = instance.doOCR(imageFile);
System.out.println(result);
return result;
}
/**
- Takes screenshot of active screen
-
@return ImageFileName
*/
public String takeScreenShot() {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat dateFormat = new SimpleDateFormat(“dd-MMM-yyyy__hh_mm_ssaa”);
new File(scrShotDir).mkdirs(); // Create folder under project with name
// “screenshots” if doesn’t exist
destFile = dateFormat.format(new Date()) + “.png”; // Set file name
// using current
// date time.
try {
FileUtils.copyFile(scrFile, new File(scrShotDir + “/” + destFile)); // Copy
// paste
// file
// at
// destination
// folder
// location
} catch (IOException e) {
System.out.println(“Image not transfered to screenshot folder”);
e.printStackTrace();
}
return destFile;
}
For more details Refer this video - https://www.youtube.com/watch?v=lM6-ZFXiSls20
I’m using:
io.appium 6.1.0
selenium-java 3.11.0
guava 24.0-jre.
Try this:
String xmlFormat = driver.getPageSource();
if(xmlFormat.contains(“Your Toast Message Here”)){
System.out.println("Toast message displayed: "+yourToastMessage);
}
- The first line of instruction will give the xml format of the app page. In this, I got the toast message in one of the tags. It works for me.
You can put the getPageSource instruction after the desired activity, and since toast messages generally have 2 seconds of timeOut, so the above method will work.
Hi @Zuzeac,
We need to check if the xmlFormat string contains the toast message. Whether getPageSource() method is able to capture the exact XML DOM at the time of the toast message getting displayed is dependent on how you tweak wait times to ensure the exact toast message is captured.
You need to give some TimeOut.MILLISECONDS.sleep(200) or 500 before the page source is captured.
Also, the capture has to be done before the toast message vanishes away.
I think the standard of any toast message time out is 2 seconds.
Please let me know after doing it if you are still facing any problems.
More than this, I need to see your code what you are exactly trying to achieve.
Thanks … Cheers
Try this:
long startTime = System.currentTimeMillis(); //fetch starting time
boolean neededStatus;
do{
xmlFormat = driver.getPageSource();
neededStatus = xmlFormat.contains(“Needed”);
}while(!(neededStatus) && (((System.currentTimeMillis()-startTime) <= (5*1000))));
‘5’ is for 5 seconds to wait till the toast message is displayed. Generally standard timeout of a toast message is 2 seconds. You can keep this value around 2 to 5 seconds.
Let me know if this works !
@sam69
This looks like best approach for me. Can you please let me know how to achieve the same thing using ruby_lib