我使用Selenium和Java在Android上运行脚本(通过Appium服务器)。 我看到它是不可能通过使用定位举杯硒在Appium如何在Appium中测试Android Toast消息(selenium Java)
driver.findElement(By.LinkText("User not logged in")
但可以Selendroid被用来捕捉敬酒消息。
我有一种方法可以在同一个脚本中同时使用Selendroid和Appium?
我使用Selenium和Java在Android上运行脚本(通过Appium服务器)。 我看到它是不可能通过使用定位举杯硒在Appium如何在Appium中测试Android Toast消息(selenium Java)
driver.findElement(By.LinkText("User not logged in")
但可以Selendroid被用来捕捉敬酒消息。
我有一种方法可以在同一个脚本中同时使用Selendroid和Appium?
看起来你不能在同一个会话中切换驱动程序类型。 如果您尝试仅使用Selendroid进行烤面包验证 - 您可以使用OSR图像识别引擎。
检查这个答案w/ Ruby bindings
想法很简单:出现
下面是Ja中OCR用法的一个很好和简单的例子VA:tess4j example(请确保安装Tesseract引擎)
Step 1:
File scrFile=null;
String path1 = null;
BufferedImage originalImage=null;
BufferedImage resizedImage=null;
System.out.println("Starting\n\n\n\n");
scrFile = ((TakesScreenshot) appiumDriver).getScreenshotAs(OutputType.FILE);
System.out.println("after scrfile\n\n\n\n");
originalImage = ImageIO.read(scrFile);
System.out.println("after originalFile\n\n\n");
BufferedImage.TYPE_INT_ARGB : originalImage.getType();
resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
Step 2:
BufferedImage pathforToast= original image;
Step 3:
System.setProperty("jna.library.path","C:/Users/Dell/workspace/MOBILEFRAMEWORK/dlls/x64/");
Tesseract instance = Tesseract.getInstance();
`enter code here`ImageIO.scanForPlugins();
String result=null;
result = instance.doOCR(pathforToast);`enter code here`
System.out.println(result);`enter code here`
吐司信息页面采取截屏,并尝试将图像文件转换中使用下面的代码文本并验证文本。
public void imageconversion(String filePath) throws IOException,
{
ITesseract instance = new Tesseract();
//file path is the image which you need to convert to text
File imageFile = new File(filePath);
BufferedImage img = null;
img = ImageIO.read(imageFile);
BufferedImage blackNWhite = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackNWhite.createGraphics();
graphics.drawImage(img, 0, 0, null);
//path where your downloaded tessdata exists
instance.setDatapath("E://ocr//data");
//What language you required to convert,(e.g. English)
instance.setLanguage("eng");
String result = instance.doOCR(blackNWhite);
System.out.println(result);
}
Appium直接不给任何API来读,我们需要使用tess4j罐子做敬酒消息。首先,我们需要拍摄屏幕截图,然后我们需要使用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;
}
欲了解更多详情,请参阅此视频 - https://www.youtube.com/watch?v=lM6-ZFXiSls
谢谢!所以我从http://tess4j.sourceforge.net/下载了Tess4J 2.0,我在eclipse中将所有jar文件添加到了我的项目中。我还给出了下面的命令,将dll文件gsdll32,libtesseract303和liblept170存储在文件夹中dllfiles在代码System.setProperty(“jna.library.path”,“C:\\ Tess4J \\ dllfiles);在运行示例代码。它抛出一个错误”线程中的异常“主”java.lang.UnsatisfiedLinkError:找不到指定的模块“ – RJX
我能够解决这个错误,代码使用了doOCR函数,它不工作,它是Microsoft VC++,它创建了这个错误,我安装了VC++ 2013并且代码运行了,但现在问题出现了tesseract并没有正确地转换图像,它缺少大量文本,有些文本不正确。是否还有其他好的OCR可供使用? – RJX
@RJX实际上,您应该首先使用图像以使其“可读” 'img.c ontrast.normalize.negate.posterize(3).adaptive_resize(3)' 您可以看到我使用ImageMagick来对比,标准化,否定,折后和调整它的大小x3。多数民众赞成我发现在我的项目工作很好 – brbrr