Best Galen code snippet using com.galenframework.specs.SpecOcr
Source:SpecValidationOcr.java
...23import com.galenframework.ocr.OcrResult;24import com.galenframework.ocr.OcrService;25import com.galenframework.page.PageElement;26import com.galenframework.page.Rect;27import com.galenframework.specs.SpecOcr;28import com.galenframework.validation.PageValidation;29import com.galenframework.validation.SpecValidation;30import com.galenframework.validation.ValidationErrorException;31import com.galenframework.validation.ValidationObject;32import com.galenframework.validation.ValidationResult;33public class SpecValidationOcr extends SpecValidation<SpecOcr> {34 private final OcrService ocrService;35 public SpecValidationOcr(OcrService ocrService) {36 this.ocrService = ocrService;37 }38 @Override39 public ValidationResult check(PageValidation pageValidation, String objectName, SpecOcr spec) throws ValidationErrorException {40 PageElement mainObject = pageValidation.findPageElement(objectName);41 42 checkAvailability(mainObject, objectName);43 44 Rect area = mainObject.getArea();45 BufferedImage img = pageValidation.getPage().getScreenshotImage();46 OcrResult ocrResult = ocrService.findOcrText(img, area);47 if (ocrResult.getText() == null) {48 ocrResult.setText("");49 }50 String realText = applyOperationsTo(ocrResult.getText().trim(), spec.getOperations());51 checkValue(spec, objectName, realText, "text", ocrResult.getRect());52 return new ValidationResult(spec, asList(new ValidationObject(ocrResult.getRect(), objectName)));53 }54 private String applyOperationsTo(String text, List<String> operations) {55 if (operations != null) {56 for (String operation : operations) {57 text = TextOperation.find(operation).apply(text);58 }59 }60 return text;61 }62 protected void checkValue(SpecOcr spec, String objectName, String realText, String checkEntity, Rect area) throws ValidationErrorException {63 if (spec.getType() == SpecOcr.Type.IS) {64 checkIs(objectName, area, realText, spec.getText(), checkEntity);65 }66 if (spec.getType() == SpecOcr.Type.CONTAINS) {67 checkContains(objectName, area, realText, spec.getText(), checkEntity);68 }69 else if (spec.getType() == SpecOcr.Type.STARTS) {70 checkStarts(objectName, area, realText, spec.getText(), checkEntity);71 }72 else if (spec.getType() == SpecOcr.Type.ENDS) {73 checkEnds(objectName, area, realText, spec.getText(), checkEntity);74 }75 else if (spec.getType() == SpecOcr.Type.MATCHES) {76 checkMatches(objectName, area, realText, spec.getText(), checkEntity);77 }78 }79 protected void checkIs(String objectName, Rect area, String realText, String text, String checkEntity) throws ValidationErrorException {80 if(realText.equals(text)){81 return;82 }83 throw new ValidationErrorException(asList(new ValidationObject(area, objectName)), asList(format("\"%s\" %s is \"%s\" but should be \"%s\"", objectName, checkEntity, realText, text)));84 }85 protected void checkStarts(String objectName, Rect area, String realText, String text, String checkEntity) throws ValidationErrorException {86 if (!realText.startsWith(text)) {87 throw new ValidationErrorException(asList(new ValidationObject(area, objectName)), asList(format("\"%s\" %s is \"%s\" but should start with \"%s\"", objectName, checkEntity, realText, text)));88 }89 }...
Source:OcrValidationTest.java
...20import com.galenframework.ocr.OcrService;21import com.galenframework.page.PageElement;22import com.galenframework.page.Rect;23import com.galenframework.rainbow4j.Rainbow4J;24import com.galenframework.specs.SpecOcr;25import com.galenframework.specs.page.Locator;26import com.galenframework.specs.page.PageSpec;27import com.galenframework.validation.PageValidation;28import com.galenframework.validation.ValidationErrorException;29import com.galenframework.validation.ValidationResult;30import com.galenframework.validation.specs.SpecValidationOcr;31import org.testng.annotations.Test;32import java.awt.image.BufferedImage;33import java.io.IOException;34import java.util.HashMap;35import static java.util.Arrays.asList;36import static org.hamcrest.MatcherAssert.assertThat;37import static org.hamcrest.Matchers.is;38import static org.hamcrest.Matchers.notNullValue;39import static org.mockito.Matchers.any;40import static org.mockito.Matchers.anyObject;41import static org.mockito.Matchers.eq;42import static org.mockito.Mockito.mock;43import static org.mockito.Mockito.verify;44import static org.mockito.Mockito.when;45public class OcrValidationTest {46 private final OcrService ocrService = mock(OcrService.class);47 private final SpecValidationOcr ocrValidation = new SpecValidationOcr(ocrService);48 private BufferedImage fakeScreenshot = loadTestImage("/imgs/page-screenshot.png");49 @Test50 public void should_fail_check_when_text_is_different() throws Exception {51 when(ocrService.findOcrText(any(), any())).thenReturn(52 new OcrResult(" Real text \n ", new Rect(0,0, 100, 50))53 );54 SpecOcr spec = new SpecOcr(SpecOcr.Type.IS, "Expected text");55 MockedPage page = createMockedPage();56 PageSpec pageSpec = createMockedPageSpec(page);57 PageValidation pageValidation = new PageValidation(null, page, pageSpec, null, null);58 try {59 ocrValidation.check(pageValidation, "button", spec);60 throw new RuntimeException("It didn't throw exception but should");61 } catch(ValidationErrorException ex) {62 assertThat(ex.getErrorMessages(), is(asList("\"button\" text is \"Real text\" but should be \"Expected text\"")));63 }64 verify(ocrService).findOcrText(anyObject(), eq(new Rect(0, 0, 100, 50)));65 }66 private PageSpec createMockedPageSpec(MockedPage page) {67 PageSpec pageSpec = new PageSpec();68 for (String objectName : page.getElements().keySet()) {...
Source:SpecOcrProcessor.java
...19import com.galenframework.parser.Expectations;20import com.galenframework.parser.StringCharReader;21import com.galenframework.parser.SyntaxException;22import com.galenframework.specs.Spec;23import com.galenframework.specs.SpecOcr;24public class SpecOcrProcessor implements SpecProcessor {25 @Override26 public Spec process(StringCharReader reader, String contextPath) {27 /* first building up a list of text operations */28 List<String> textOperations = new LinkedList<>();29 verifyValidationEntity(reader);30 SpecOcr.Type textCheckType = null;31 while(textCheckType == null && reader.hasMoreNormalSymbols()) {32 String word = reader.readWord();33 if (word.isEmpty()) {34 throw new SyntaxException("Expected text check type, but got nothing");35 }36 if (SpecOcr.Type.isValid(word)) {37 textCheckType = SpecOcr.Type.fromString(word);38 } else {39 textOperations.add(word);40 }41 }42 String expectedText = Expectations.doubleQuotedText().read(reader);43 if (reader.hasMoreNormalSymbols()) {44 throw new SyntaxException("Too many arguments for spec: " + reader.getTheRest().trim());45 }46 return new SpecOcr(textCheckType, expectedText, textOperations);47 }48 private void verifyValidationEntity(StringCharReader reader) {49 String validationEntity = reader.readWord();50 if (!"text".equals(validationEntity)) {51 throw new SyntaxException(String.format("Unknown entity: %s, expected to see \"text\"", validationEntity));52 }53 }54}...
SpecOcr
Using AI Code Generation
1import com.galenframework.specs.SpecOcr;2import com.galenframework.browser.SeleniumBrowser;3import com.galenframework.browser.Browser;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.firefox.FirefoxDriver;6public class 1 {7 public static void main(String[] args) throws Exception {8 WebDriver driver = new FirefoxDriver();9 Browser browser = new SeleniumBrowser(driver);10 SpecOcr specOcr = new SpecOcr("text", "search", "search", "search", "search");11 specOcr.check(browser, null);12 driver.close();13 }14}
SpecOcr
Using AI Code Generation
1package com.galenframework.java.sample.tests;2import com.galenframework.java.sample.components.SpecOcr;3import com.galenframework.java.sample.components.SpecOcr;4import java.io.IOException;5import static com.galenframework.java.sample.components.SpecOcr.specOcr;6public class OcrTest {7 public static void main(String[] args) throws IOException {8 specOcr("C:\\Users\\Shreya\\Desktop\\ocr.png", "Shreya", 90);9 }10}11package com.galenframework.java.sample.tests;12import com.galenframework.java.sample.components.SpecOcr;13import java.io.IOException;14import static com.galenframework.java.sample.components.SpecOcr.specOcr;15public class OcrTest {16 public static void main(String[] args) throws IOException {17 specOcr("C:\\Users\\Shreya\\Desktop\\ocr.png", "Shreya", 90);18 }19}
SpecOcr
Using AI Code Generation
1package com.galenframework.specs;2import com.galenframework.specs.SpecOcr;3import java.io.File;4import java.io.IOException;5import java.util.List;6import net.sourceforge.tess4j.Tesseract;7import net.sourceforge.tess4j.TesseractException;8import org.apache.commons.io.FileUtils;9import org.openqa.selenium.OutputType;10import org.openqa.selenium.TakesScreenshot;11import org.openqa.selenium.WebDriver;12import org.openqa.selenium.WebElement;13public class SpecOcr {14public static void main(String[] args) throws IOException, TesseractException {15Tesseract tesseract = new Tesseract();16tesseract.setDatapath("C:\\Users\\Administrator\\Downloads\\tessdata-master\\tessdata-master");17String text = tesseract.doOCR(new File("C:\\Users\\Administrator\\Downloads\\tessdata-master\\tessdata-master\\image.png"));18System.out.println(text);19}20}
SpecOcr
Using AI Code Generation
1import com.galenframework.specs.Spec;2import com.galenframework.specs.SpecOcr;3import com.galenframework.specs.page.PageSection;4import com.galenframework.validation.ValidationError;5import com.galenframework.validation.ValidationObject;6import com.galenframework.validation.ValidationResult;7import com.galenframework.validation.Validator;8import org.openqa.selenium.*;9import org.openqa.selenium.firefox.FirefoxDriver;10import java.io.File;11import java.io.IOException;12import java.util.ArrayList;13import java.util.List;14import java.util.concurrent.TimeUnit;15public class 1 {16 public static void main(String[] args) throws IOException {17 WebDriver driver = new FirefoxDriver();18 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);19 File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);20 PageSection section = new PageSection(0, 0, 200, 100);21 SpecOcr specOcr = new SpecOcr(screenshot, section, "sample text");22 Spec spec = new Spec("ocr", specOcr);23 Validator validator = new Validator();24 ValidationObject validationObject = new ValidationObject("object1", spec);25 List<ValidationObject> validationObjects = new ArrayList<ValidationObject>();26 validationObjects.add(validationObject);27 ValidationResult validationResult = new ValidationResult();28 validator.validate(validationObjects, validationResult);29 List<ValidationError> validationErrors = validationResult.getErrors();30 for (ValidationError error : validationErrors) {31 System.out.println(error.getMessage());32 }33 driver.quit();34 }35}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!