Best Galen code snippet using com.galenframework.specs.SpecOcr.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
1SpecOcr specOcr = new SpecOcr();2specOcr.setLeft(0);3specOcr.setRight(0);4specOcr.setTop(0);5specOcr.setBottom(0);6specOcr.setOcrText("sample text");7specOcr.setOcrEngine(OCR_ENGINE_TYPE.TESSERACT);8specOcr.setOcrLanguage("eng");9specOcr.setOcrImageType(OCR_IMAGE_TYPE.IMAGE);10specOcr.setOcrImageSource("path to image");11SpecOcr specOcr = new SpecOcr();12specOcr.setLeft(0);13specOcr.setRight(0);14specOcr.setTop(0);15specOcr.setBottom(0);16specOcr.setOcrText("sample text");17specOcr.setOcrEngine(OCR_ENGINE_TYPE.TESSERACT);18specOcr.setOcrLanguage("eng");19specOcr.setOcrImageType(OCR_IMAGE_TYPE.SCREENSHOT);20specOcr.setOcrImageSource("path to image");21SpecOcr specOcr = new SpecOcr();22specOcr.setLeft(0);23specOcr.setRight(0);24specOcr.setTop(0);25specOcr.setBottom(0);26specOcr.setOcrText("sample text");27specOcr.setOcrEngine(OCR_ENGINE_TYPE.TESSERACT);28specOcr.setOcrLanguage("eng");29specOcr.setOcrImageType(OCR_IMAGE_TYPE.SCREENSHOT);30specOcr.setOcrImageSource("path to image");31SpecOcr specOcr = new SpecOcr();32specOcr.setLeft(0);33specOcr.setRight(0);34specOcr.setTop(0);35specOcr.setBottom(0);36specOcr.setOcrText("sample text");37specOcr.setOcrEngine(OCR_ENGINE_TYPE.TESSERACT);38specOcr.setOcrLanguage("eng");
SpecOcr
Using AI Code Generation
1import com.galenframework.api.Galen;2import com.galenframework.reports.GalenTestInfo;3import com.galenframework.specs.SpecOcr;4import com.galenframework.specs.page.PageSection;5import com.galenframework.specs.page.PageSpec;6import com.galenframework.specs.page.PageSpecReader;7import com.galenframework.specs.page.PageSection;8import com.galenframework.validation.ValidationResult;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.chrome.ChromeDriver;11import org.openqa.selenium.chrome.ChromeOptions;12import java.io.IOException;13import java.util.LinkedList;14import java.util.List;15public class GalenSample {16 public static void main(String[] args) throws IOException {17 ChromeOptions options = new ChromeOptions();18 options.addArguments("headless");19 WebDriver driver = new ChromeDriver(options);20 PageSpecReader reader = new PageSpecReader();21 PageSpec spec = reader.read("page.spec");22 List<ValidationResult> results = new LinkedList<>();23 for (PageSection section : spec.getSections()) {24 results.addAll(Galen.checkLayout(driver, section.getObjects(), section.getSpecs()));25 }26 System.out.println(results);27 }28}29import com.galenframework.api.Galen;30import com.galenframework.components.MockedTestNgTestBase;31import com.galenframework.reports.GalenTestInfo;32import com.galenframework.specs.SpecOcr;33import com.galenframework.specs.page.PageSection;34import com.galenframework.specs.page.PageSpec;35import com.galenframework.specs.page.PageSpecReader;36import com.galenframework.specs.page.PageSection;37import com.galenframework.validation.ValidationResult;38import org.openqa.selenium.WebDriver;39import org.openqa.selenium.chrome.ChromeDriver;40import org.openqa.selenium.chrome.ChromeOptions;41import java.io.IOException;42import java.util.LinkedList;43import java.util.List;44public class GalenSample extends MockedTestNgTestBase {45 public static void main(String[] args) throws IOException {46 ChromeOptions options = new ChromeOptions();47 options.addArguments("headless");48 WebDriver driver = new ChromeDriver(options);
SpecOcr
Using AI Code Generation
1package com.galenframework.java.using;2import com.galenframework.java.sample.components.*;3import com.galenframework.java.sample.components.common.*;4import com.galenframework.java.sample.components.common.Button;5import com.galenframework.java.sample.components.common.CheckBox;6import com.galenframework.java.sample.components.common.RadioButton;7import com.galenframework.java.sample.components.common.TextBox;8import com.galenframework.java.sample.components.common.Button;9import com.galenframework.java.sample.components.common.CheckBox;10import com.galenframework.java.sample.components.common.RadioButton;11import com.galenframework.java.sample.components.common.TextBox;12import com.galenframework.java.sample.components.common.Button;13import com.galenframework.java.sample.components.common.CheckBox;14import com.galenframework.java.sample.components.common.RadioButton;15import com.galenframework.java.sample.components.common.TextBox;16import com.galenframework.java.sample.components.common.Button;17import com.galenframework.java.sample.components.common.CheckBox;18import com.galenframework.java.sample.components.common.RadioButton;19import com.galenframework.java.sample.components.common.TextBox;20import com.galenframework.java.sample.components.common.Button;21import com.galenframework.java.sample.components.common.CheckBox;22import com.galenframework.java.sample.components.common.RadioButton;23import com.galenframework.java.sample.components.common.TextBox;24import com.galenframework.java.sample.components.common.Button;25import com.galenframework.java.sample.components.common.CheckBox;26import com.galenframework.java.sample.components.common.RadioButton;27import com.galenframework.java.sample.components.common.TextBox;28import com.galenframework.java.sample.components.common.Button;29import com.galenframework.java.sample.components.common.CheckBox;30import com.galenframework.java.sample.components.common.RadioButton;31import com.galenframework.java.sample.components.common.TextBox;32import com.galenframework.java.sample.components.common.Button;33import com.galenframework.java.sample.components.common.CheckBox;34import com.galenframework.java.sample.components.common.RadioButton;35import com.galenframework.java.sample.components.common.TextBox;36import com.galenframework.java.sample.components.common.Button;37import com.galenframework.java.sample.components.common.CheckBox;38import com.galenframework.java.sample.components.common.RadioButton;39import com.galenframework.java.sample.components.common.TextBox;40import com.galenframework.java.sample.components.common.Button;41import com.galenframework.java.sample.components.common.CheckBox;42import com.galenframework.java.sample.components.common.RadioButton;43import com.galenframework.java.sample.components.common.TextBox;44import com.galenframework.java.sample.components.common.Button;45import com.galenframework.java.sample.components.common.CheckBox;46import com.galenframework.java.sample.components
SpecOcr
Using AI Code Generation
1package com.galenframework.java.official;2import com.galenframework.java.sample.components.HomePage;3import com.galenframework.java.sample.components.LoginPage;4import com.galenframework.java.sample.components.ProfilePage;5import com.galenframework.java.sample.components.SearchPage;6import com.galenframework.java.sample.components.SearchResultsPage;7import com.galenframework.java.sample.components.SignedInHomePage;8import com.galenframework.java.sample.components.SignedInProfilePage;9import com.galenframework.java.sample.components.SignedInSearchResultsPage;10import com.galenframework.java.sample.components.SignedInShoppingCartPage;11import com.galenframework.java.sample.components.SignInPage;12import com.galenframework.java.sample.components.ShoppingCartPage;13import com.galenframework.java.sample.components.SignUpPage;14import com.galenframework.java.sample.components.SignedInSignUpPage;15import com.galenframework.java.sample.components.SignedInHomePage;16import com.galenframework.java.sample.components.SignedInProfilePage;17import com.galenframework.java.sample.components.SignedInSearchResultsPage;18import com.galenframework.java.sample.components.SignedInShoppingCartPage;19import com.galenframework.java.sample.components.SignInPage;20import com.galenframework.java.sample.components.ShoppingCartPage;21import com.galenframework.java.sample.components.SignUpPage;22import com.galenframework.java.sample.components.SignedInSignUpPage;23import com.galenframework.java.sample.components.SignedInHomePage;24import com.galenframework.java.sample.components.SignedInProfilePage;25import com.galenframework.java.sample.components.SignedInSearchResultsPage;26import com.galenframework.java.sample.components.SignedInShoppingCartPage;27import com.galenframework.java.sample.components.SignInPage;28import com.galenframework.java.sample.components.ShoppingCartPage;29import com.galenframework.java.sample.components.SignUpPage;30import com.galenframework.java.sample.components.SignedInSignUpPage;31import com.galenframework.java.sample.components.SignedInHomePage;32import com.galenframework.java.sample.components.SignedInProfilePage;33import com.galenframework.java.sample.components.SignedInSearchResultsPage;34import com.galenframework.java.sample.components.SignedInShoppingCartPage;35import com.galenframework.java.sample.components.SignInPage;36import com.galenframework.java.sample.components.ShoppingCartPage;37import com.galenframework.java.sample.components.SignUpPage;38import com.galenframework.java
SpecOcr
Using AI Code Generation
1import com.galenframework.specs.SpecOcr;2import com.galenframework.specs.page.PageSection;3import com.galenframework.specs.page.PageSection;4import static com.galenframework.validation.ValidationObject.check;5import com.galenframework.validation.ValidationResult;6import com.galenframework.validation.ValidationResult.ValidationError;7import com.galenframework.validation.ValidationResult.ValidationError;8import com.galenframework.validation.ValidationResult;9import com.galenframework.validation.ValidationResult.ValidationError;10import com.galenframework.validation.ValidationResult.ValidationE
SpecOcr
Using AI Code Generation
1import com.galenframework.java.*;2import com.galenframework.java.ocr.*;3import com.galenframework.java.ocr.Image;4import com.galenframework.java.ocr.ImageProcessor;5import com.galenframework.java.ocr.ImageProcessorFactory;6import com.galenframework.java.ocr.ImageProcessorFactoryImpl;7import com.galenframework.java.ocr.ImageProcessorImpl;8import com.galenframework.java.ocr.ImageProcessorResult;
SpecOcr
Using AI Code Generation
1package com.galenframework.java.using;2import com.galenframework.java.sample.components.HomePage;3import com.galenframework.java.sample.components.LoginPage;4import com.galenframework.java.sample.components.ProductPage;5import com.galenframework.java.sample.components.SearchPage;6import com.galenframework.java.sample.components.components.SearchResult;7import com.galenframework.java.sample.components.pages.HomePage;8import com.galenframework.java.sample.components.pages.LoginPage;9import com.galenframework.java.sample.components.pages.ProductPage;10import com.galenframework.java.sample.components.pages.SearchPage;11import com.galenframework.java.sample.components.pages.components.SearchResult;12import com.galenframework.java.sample.components.pages.components.SearchResults;13import com.galenframework.java.sample.components.pages.components.SearchResults;14import com.galenframework.java.sample.components.pages.components.SearchResults;15import com.galenframework.java.sample.components.pages.components.SearchResults;16import org.openqa.selenium.WebDriver;17import org.openqa.selenium.chrome.ChromeDriver;18import org.testng.annotations.AfterMethod;19import org.testng.annotations.BeforeMethod;20import org.testng.annotations.Test;21import java.io.IOException;22import java.util.List;23import static com.galenframework.java.sample.components.pages.components.SearchResults.SEARCH_RESULTS_LIST;24import static com.galenframework.java.sample.components.pages.components.SearchResults.SEARCH_RESULTS_LIST;25import static com.galenframework.java.sample.components.pages.components.SearchResults.SEARCH_RESULTS_LIST;26public class SpecOcrTest {27 private WebDriver driver;28 public void beforeMethod() {29 driver = new ChromeDriver();30 }31 public void afterMethod() {32 driver.quit();33 }34 public void testSpecOcr() throws IOException {35 HomePage homePage = new HomePage(driver);36 LoginPage loginPage = homePage.clickSignIn();37 loginPage.login("username", "password");38 SearchPage searchPage = homePage.search("Galaxy S6");39 List<SearchResult> searchResults = searchPage.getSearchResults();40 SearchResult searchResult = searchResults.get(0);41 ProductPage productPage = searchResult.openProductPage();42 productPage.checkLayout("specs/specOcrTest.spec", "productPage");43 }44}
SpecOcr
Using AI Code Generation
1package com.galenframework.java.sample.tests;2import com.galenframework.java.sample.components.GalenTestBase;3import com.galenframework.reports.model.LayoutReport;4import org.testng.annotations.Test;5import java.io.IOException;6import static com.galenframework.java.sample.components.GalenTestBase.load;7import static com.galenframework.java.sample.components.GalenTestBase.report;8public class GalenSpecOcrTest extends GalenTestBase {9 public void testLayout() throws IOException {10 load("/");11 LayoutReport layoutReport = checkLayout(load("/specs/1.spec"), getReport("1"));12 report(layoutReport, "/reports/1");13 }14}
SpecOcr
Using AI Code Generation
1import com.galenframework.reports.GalenTestInfo;2import com.galenframework.reports.model.LayoutReport;3import com.galenframework.specs.Spec;4import com.galenframework.specs.SpecOcr;5import com.galenframework.specs.page.PageSection;6import com.galenframework.validation.ValidationObject;7import com.galenframework.validation.ValidationResult;8import com.galenframework.validation.ValidationResultListener;9import com.galenframework.validation.ValidationResults;10import java.awt.*;11import java.io.File;12import java.util.Arrays;13import java.util.LinkedList;14import java.util.List;15public class 1 {16 public static void main(String[] args) throws Exception {17 GalenTestInfo test = GalenTestInfo.fromString("testName");18 test.getReport().layout("layout report", "desktop", Arrays.asList(19 new File("test.png")20 ));21 LayoutReport layoutReport = test.getReport().getLayoutReports().get(0);22 ValidationResults validationResults = layoutReport.getValidationResults();23 validationResults.add(new ValidationResult(new ValidationObject("test.png", new PageSection(0, 0, 100, 100)), new SpecOcr("test"), new LinkedList<ValidationResultListener>()));24 ValidationResult validationResult = validationResults.get(0);25 Spec spec = validationResult.getSpec();26 if (spec instanceof SpecOcr) {27 SpecOcr specOcr = (SpecOcr) spec;28 String text = specOcr.getText(new File("test.png"), new Rectangle(0, 0, 100, 100));29 System.out.println(text);30 }31 }32}
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!!