phoenixframework自動化測試平臺圖片識別示例

本例演示瞭如何使用 phoenixframework 將web頁面上的圖片自動保存到本地且讀取上面文字的方法:java

package org.phoenix.cases.lianmeng;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.interactions.Actions;
import org.phoenix.cases.plugin.SaveImgToLocal;
import org.phoenix.enums.LocatorType;
import org.phoenix.model.CaseLogBean;
import org.phoenix.model.UnitLogBean;
import org.phoenix.plugins.IImageReader;
import org.phoenix.proxy.ActionProxy;

import com.codeborne.selenide.SelenideElement;
import com.google.common.io.Files;

/**
 * 將頁面上定位到的圖片保存到本地,並使用ocr讀取圖片上的文字
 * 特別適用於無具體地址的驗證碼圖片
 * @author mengfeiyang
 */
public class SaveImg extends ActionProxy {
	private String localFile = "E:\\testcode.jpg";
	@Override
	public LinkedList<UnitLogBean> run(CaseLogBean caseLogBean) {
		init(caseLogBean);
		try {
			phoenix.webAPI().setFirefoxExePath("D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
			phoenix.webAPI().openNewWindowByFirefox("https://xxx.net/account/fpwd?action=forgotpassword&service=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn");
			// phoenix.webAPI().openNewWindowByIE("http://www.oschina.net/home/reset-pwd");
			// phoenix.webAPI().openNewWindowByChrome("http://www.oschina.net/home/reset-pwd");
			phoenix.webAPI().getCurrentDriver().manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
			phoenix.webAPI().getCurrentDriver().manage().window().maximize();
			phoenix.webAPI().sleep(2000);			
			String engine = phoenix.webAPI().getCaseLogBean().getEngineType();
			phoenix.webAPI().webElement(".user-name", null).setText("11111");
			for(int i=0;i<10;i++){
			SelenideElement element = phoenix.webAPI().webElementLinkFinder(
					"#yanzheng", null);
			phoenix.webAPI().sleep(1000);
			SaveImgToLocal.doSave(element, engine, localFile);
			IImageReader imageReader = phoenix.imageReader().configImageReader("D://Program Files (x86)//Tesseract-OCR");
			String oos = imageReader.readLocalImage(localFile, Files.getFileExtension(localFile));
			System.out.println("-----"+oos+"------");
			phoenix.webAPI().webElement(".code", null).setText(oos);
			phoenix.webAPI().webElement("//input[@type='submit']", LocatorType.XPATH).click();
				if(!phoenix.webAPI().webElement(".email-address", null).exists()){
					new Actions(phoenix.webAPI().getCurrentDriver()).doubleClick(element).perform();
				}else break;
			}
			phoenix.webAPI().sleep(2000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		phoenix.webAPI().closeWindow();
		return getUnitLog();
	}
	public static void main(String[] args) {
		SaveImg saveImg = new SaveImg();
		LinkedList<UnitLogBean> us = saveImg.run(new CaseLogBean());
		for (UnitLogBean u : us) {
			System.out.println(u.getContent());
		}
	}
}
相關文章
相關標籤/搜索