Best Galen code snippet using com.galenframework.validation.PageValidation
Source:ReportingListenerTestUtils.java  
...21import java.io.File;22import java.io.IOException;23import java.util.HashMap;24import java.util.Map;25import com.galenframework.components.MockedPageValidation;26import com.galenframework.components.validation.MockedPageElement;27import com.galenframework.page.Rect;28import com.galenframework.reports.GalenTestInfo;29import com.galenframework.reports.TestReport;30import com.galenframework.runner.SuiteListener;31import com.galenframework.runner.TestListener;32import com.galenframework.specs.*;33import com.galenframework.specs.page.PageSection;34import com.galenframework.specs.Place;35import com.galenframework.suite.actions.GalenPageActionCheck;36import com.galenframework.tests.GalenBasicTest;37import com.galenframework.validation.*;38import com.galenframework.page.PageElement;39import com.galenframework.rainbow4j.Rainbow4J;40public class ReportingListenerTestUtils {41    private static final com.galenframework.specs.Spec NO_SPEC = null;42    private static String comparisonMapImagePath = ReportingListenerTestUtils.class.getResource("/imgs/page-sample-correct.png").getFile();43    public static void performSampleReporting(String suiteName, TestListener testListener, ValidationListener validationListener, SuiteListener suiteListener) throws IOException {44        45        GalenBasicTest suite = new GalenBasicTest();46        suite.setName(suiteName);47        48        if (testListener != null) testListener.onTestStarted(suite);49        50        Map<String, PageElement> pageElements = new HashMap<>();51        pageElements.put("objectA1", new MockedPageElement(10, 10, 100, 50));52        pageElements.put("objectA2", new MockedPageElement(200, 300, 50, 30));53        pageElements.put("objectB1", new MockedPageElement(10, 10, 100, 50));54        pageElements.put("objectB2", new MockedPageElement(200, 300, 50, 30));55        pageElements.put("sub-objectA1", new MockedPageElement(200, 300, 50, 30));56        57        MockedPageValidation pageValidation = new MockedPageValidation(pageElements);58        59        60        61        GalenPageActionCheck action = new GalenPageActionCheck();62        action.setOriginalCommand("check homepage.spec --include all,mobile");63        validationListener.onBeforePageAction(action);64        {65            PageSection section1 = new PageSection("", new Place("specs.spec", 5));66            validationListener.onBeforeSection(pageValidation, section1);67            68            validationListener.onObject(pageValidation, "objectA1"); {69                validationListener.onSpecGroup(pageValidation, "some spec group");70                    onSpecError(validationListener, pageValidation, "objectA1",71                            new SpecInside("other-object", asList(new Location(exact(10), asList(Side.LEFT))))72                                    .withOriginalText("inside other-object 10px left")73                                    .withPlace(new Place("specs.spec", 12)),74                            new ValidationResult(NO_SPEC,75                                    asList(76                                            new ValidationObject(new Rect(10, 10, 100, 50), "objectA1"),77                                            new ValidationObject(new Rect(1, 1, 90, 100), "other-object")),78                                    new ValidationError(asList("objectA1 is not inside other-object")), emptyList()79                            ));80                validationListener.onAfterSpecGroup(pageValidation, "some spec group");81                onSpecSuccess(validationListener, pageValidation, "objectA1",82                    new SpecWidth(between(10, 20))83                        .withOriginalText("width 10 to 20px")84                        .withPlace(new Place("specs.spec", 12))85                        .withAlias("Some alias"),86                    new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(10, 10, 100, 50), "objectA1"))));87                onSpecSuccess(validationListener, pageValidation, "objectA1",88                    new SpecWidth(between(10, 20))89                        .withOriginalText("width 10 to 20px")90                        .withPlace(new Place("specs.spec", 12)),91                    new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(10, 10, 100, 50), "objectA1"))));92            }93            validationListener.onAfterObject(pageValidation, "objectA1");94            95            validationListener.onObject(pageValidation, "objectA2"); {96                onSpecSuccess(validationListener, pageValidation, "objectA2",97                        new SpecWidth(between(10, 20))98                                .withOriginalText("width 10 to 20px")99                                .withPlace(new Place("specs.spec", 12)),100                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(200, 300, 50, 30), "objectA2"))));101                onSpecError(validationListener, pageValidation,102                        "objectA2",103                        new SpecWidth(exact(10)).withOriginalText("width 10px")104                                .withPlace(new Place("specs.spec", 12)),105                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(200, 300, 50, 30), "objectA2")),106                                new ValidationError(asList("objectA2 width is 20px instead of 10px")), emptyList()));107                onSpecError(validationListener, pageValidation,108                        "objectA2",109                        new SpecText(SpecText.Type.IS, "Login").withOriginalText("text is \"Login\"")110                                .withPlace(new Place("specs.spec", 12))111                                .withOnlyWarn(true),112                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(200, 300, 50, 30), "objectA2")),113                                new ValidationError(asList("objectA2 text is \"Logout\" instead of \"Login\"")), emptyList()));114            }115            validationListener.onAfterObject(pageValidation, "objectA2");116            117            validationListener.onAfterSection(pageValidation, section1);118            119            PageSection section2 = new PageSection("some section 2", new Place("specs.spec", 14));120            validationListener.onBeforeSection(pageValidation, section2);121            122            validationListener.onObject(pageValidation, "objectA1"); {123                onSpecSuccess(validationListener, pageValidation, "objectA1",124                        new SpecHeight(between(10, 20))125                                .withOriginalText("height 10 to 20px")126                                .withPlace(new Place("specs.spec", 12)),127                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(10, 10, 100, 50), "objectA1"))));128                /* Calling before spec event as after it will be a sub-layout */129                validationListener.onBeforeSpec(pageValidation, "objectA1",130                        new SpecHeight(between(10, 20))131                        .withOriginalText("component some-component.spec")132                        .withPlace(new Place("specs.spec", 12)));133                //Doing sub-layout call134                {135                    validationListener.onSubLayout(pageValidation, "objectA1");136                    PageSection subSection = new PageSection("Sub section", new Place("specs.spec", 15));137                    validationListener.onBeforeSection(pageValidation, subSection);138                    validationListener.onObject(pageValidation, "sub-objectA1"); {139                        onSpecSuccess(validationListener, pageValidation, "sub-objectA1",140                                new SpecHeight(between(10, 20))141                                        .withOriginalText("height 10 to 20px")142                                        .withPlace(new Place("specs.spec", 12)),143                                new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(200, 300, 50, 30), "sub-objectA1"))));144                        onSpecError(validationListener, pageValidation,145                                "sub-objectA1",146                                new SpecWidth(exact(10)).withOriginalText("width 10px")147                                        .withPlace(new Place("specs.spec", 12)),148                                new ValidationResult(NO_SPEC,149                                        asList(new ValidationObject(new Rect(200, 300, 50, 30), "sub-objectA1")),150                                        new ValidationError(asList("sub-objectA1 width is 20px instead of 10px")), emptyList()));151                    }152                    validationListener.onAfterObject(pageValidation, "sub-objectA1");153                    validationListener.onAfterSection(pageValidation, subSection);154                    validationListener.onAfterSubLayout(pageValidation, "objectA1");155                }156                validationListener.onSpecSuccess(pageValidation, "objectA1",157                        new SpecHeight(between(10, 20))158                                .withOriginalText("component some-component.spec")159                                .withPlace(new Place("specs.spec", 12)),160                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(10, 10, 100, 50), "objectA1"))));161            }162            validationListener.onAfterObject(pageValidation, "objectA1");163            164            validationListener.onAfterSection(pageValidation, section2);165        166        }167        validationListener.onAfterPageAction(action);168        169        validationListener.onBeforePageAction(action);170        {171            PageSection section1 = new PageSection("some section 1", new Place("specs.spec", 16));172            validationListener.onBeforeSection(pageValidation, section1);173            174            validationListener.onObject(pageValidation, "objectB1"); {175                onSpecSuccess(validationListener, pageValidation, "objectB1",176                        new SpecWidth(between(10, 20))177                                .withOriginalText("width 10 to 20px")178                                .withPlace(new Place("specs.spec", 12)),179                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(10, 10, 100, 50), "objectB1"))));180                181                onSpecError(validationListener, pageValidation,182                        "objectB1",183                        new SpecInside("other-object", asList(new Location(exact(10), asList(Side.LEFT)))).withOriginalText("inside other-object 10px left")184                                .withPlace(new Place("specs.spec", 12)),185                        new ValidationResult(NO_SPEC,186                                asList(new ValidationObject(new Rect(10, 10, 100, 50), "objectB1")),187                                new ValidationError(asList("objectB1 is not inside other-object", "second error message with <xml> &tags"))188                                    .withImageComparison(createSampleImageComparison()), emptyList()));189            }190            validationListener.onAfterObject(pageValidation, "objectB1");191            192            validationListener.onObject(pageValidation, "objectB2"); {193                onSpecSuccess(validationListener, pageValidation, "objectB2",194                        new SpecHeight(exact(100))195                                .withOriginalText("height 100px")196                                .withPlace(new Place("specs.spec", 12)),197                        new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(200, 300, 50, 30), "objectB2"))));198            }199            validationListener.onAfterObject(pageValidation, "objectB2");200            validationListener.onObject(pageValidation, "objectB2"); {201            onSpecSuccess(validationListener, pageValidation, "objectB2",202                    new SpecWidth(exact(100))203                            .withOriginalText("width 100px")204                            .withPlace(new Place("specs.spec", 13)),205                    new ValidationResult(NO_SPEC, asList(new ValidationObject(new Rect(200, 300, 50, 30), "objectB2"))));206        }207            validationListener.onAfterObject(pageValidation, "objectB2");208            validationListener.onGlobalError(new FakeException("Some exception here"));209            210            validationListener.onAfterSection(pageValidation, section1);211        }212        validationListener.onAfterPageAction(action);213        if (suiteListener != null) {214            tellAfterSuite(suiteListener);215        }216        217    }218    private static ImageComparison createSampleImageComparison() throws IOException {219        File file = new File(comparisonMapImagePath);220        return new ImageComparison(file, file, file);221    }222    private static void onSpecError(ValidationListener validationListener, MockedPageValidation pageValidation, String objectName, Spec spec, ValidationResult result) {223        validationListener.onBeforeSpec(pageValidation, objectName, spec);224        validationListener.onSpecError(pageValidation, objectName, spec, result);225    }226    private static void onSpecSuccess(ValidationListener validationListener, MockedPageValidation pageValidation, String objectName, Spec spec, ValidationResult result) {227        validationListener.onBeforeSpec(pageValidation, objectName, spec);228        validationListener.onSpecSuccess(pageValidation, objectName, spec, result);229    }230    private static void tellAfterSuite(SuiteListener suiteListener) {231        GalenTestInfo test = new GalenTestInfo("page1.test", null);232        TestReport report = new TestReport();233        for (int i=0; i< 6; i++) {234            report.info("info" + i);235        }236        for (int i=0; i< 5; i++) {237            report.error("error" + i);238        }239        test.setReport(report);240        suiteListener.afterTestSuite(asList(test));...Source:ValidationListenerImpl.java  
...10import com.galenframework.page.selenium.WebPageElement;11import com.galenframework.specs.Spec;12import com.galenframework.specs.page.PageSection;13import com.galenframework.suite.GalenPageAction;14import com.galenframework.validation.PageValidation;15import com.galenframework.validation.ValidationListener;16import com.galenframework.validation.ValidationResult;17import com.qmetry.qaf.automation.core.CheckpointResultBean;18import com.qmetry.qaf.automation.core.MessageTypes;19import com.qmetry.qaf.automation.core.QAFTestBase;20import com.qmetry.qaf.automation.keys.ApplicationProperties;21import com.qmetry.qaf.automation.step.StringTestStep;22import com.qmetry.qaf.automation.ui.webdriver.QAFExtendedWebElement;23import com.qmetry.qaf.automation.util.FileUtil;24import com.qmetry.qaf.automation.util.StringUtil;25public class ValidationListenerImpl implements ValidationListener {26	private CheckpointResultBean sectionResult;27	@Override28	public void onObject(PageValidation pageValidation, String objectName) {29	}30	@Override31	public void onAfterObject(PageValidation pageValidation, String objectName) {32	}33	@Override34	public void onBeforeSpec(PageValidation pageValidation, String objectName, Spec spec) {35	}36	@Override37	public void onSpecError(PageValidation pageValidation, String objectName, Spec spec,38			ValidationResult validationResult) {39		40		sectionResult.setType(MessageTypes.TestStepFail);41		QAFTestBase testbase = instance().get();42		int verificationErrors = testbase.getVerificationErrors() + 1;43		testbase.getContext().setProperty("verificationErrors", verificationErrors);44		MessageTypes mtype = validationResult.getError().isOnlyWarn() ? MessageTypes.Warn : MessageTypes.Fail;45		if (mtype.shouldReport()) {46			CheckpointResultBean result = new CheckpointResultBean();47			result.setMessage(String.join("\n", validationResult.getError().getMessages()));48			result.setType(mtype);49			result.setScreenshot(getScreenShot(pageValidation, objectName, spec.toText()));50			sectionResult.getSubCheckPoints().add(result);51		}52	}53	@Override54	public void onSpecSuccess(PageValidation pageValidation, String objectName, Spec spec,55			ValidationResult validationResult) {56		if (MessageTypes.Pass.shouldReport()) {57			CheckpointResultBean result = new CheckpointResultBean();58			result.setMessage(validationResult.getValidationObjects().get(0).getName() + "  " + spec.toText());59			result.setType(MessageTypes.Pass);60			if (ApplicationProperties.SUCEESS_SCREENSHOT.getBoolenVal(true)) {61				result.setScreenshot(getScreenShot(pageValidation, objectName, spec.toText()));62			}63			sectionResult.getSubCheckPoints().add(result);64		}65	}66	@Override67	public void onGlobalError(Exception e) {68	}69	@Override70	public void onBeforePageAction(GalenPageAction action) {71	}72	@Override73	public void onAfterPageAction(GalenPageAction action) {74		System.out.println("GalenPageAction:: " + action.getOriginalCommand());75	}76	@Override77	public void onBeforeSection(PageValidation pageValidation, PageSection pageSection) {78		sectionResult = new CheckpointResultBean();79		sectionResult.setMessage("Verify " + pageSection.getName() +" On " + pageValidation.getSectionFilter().getIncludedTags());80		sectionResult.setType(MessageTypes.TestStepPass);81	}82	@Override83	public void onAfterSection(PageValidation pageValidation, PageSection pageSection) {84		try {85			sectionResult86					.setScreenshot(screenshotOfEle(new QAFExtendedWebElement("tagName=body"), pageSection.getName()));87		} catch (Exception e) {88			sectionResult.setScreenshot(getScreenShot(pageValidation, pageSection.getName()));89		}90		instance().get().getCheckPointResults().add(sectionResult);91		new StringTestStep("COMMENT: '"+pageSection.getName()+"'").execute();92	}93	@Override94	public void onSubLayout(PageValidation pageValidation, String objectName) {95	}96	@Override97	public void onAfterSubLayout(PageValidation pageValidation, String objectName) {98	}99	@Override100	public void onSpecGroup(PageValidation pageValidation, String specGroupName) {101	}102	@Override103	public void onAfterSpecGroup(PageValidation pageValidation, String specGroupName) {104	}105	private String getScreenShot(PageValidation pageValidation, String name) {106		try {107			BufferedImage bi = pageValidation.getPage().getScreenshotImage();108			if (null != bi) {109				File outputfile = FileUtil.generateFile(StringUtil.toCamelCaseIdentifier(name), ".png",110						ApplicationProperties.SCREENSHOT_DIR.getStringVal("img"));111				ImageIO.write(bi, "png", outputfile);112				instance().get().setLastCapturedScreenShot(outputfile.getName());113				return outputfile.getPath();114			}115		} catch (IOException e) {116		}117		return null;118	}119	private String getScreenShot(PageValidation pageValidation, String objectName, String name) {120		PageElement ele = pageValidation.findPageElement(objectName);121		if (ele instanceof WebPageElement) {122			WebElement webele = ((WebPageElement) ele).getWebElement();123			if (ele.isPresent() && ele.isVisible())124				try {125					return screenshotOfEle(webele, name);126				} catch (Exception e) {127					// return getScreenShot(pageValidation, name);128				}129		}130		return null;131	}132	private String screenshotOfEle(WebElement webele, String name) throws Exception {133		String base64Image = webele.getScreenshotAs(OutputType.BASE64);...Source:Page.java  
...4import com.galenframework.speclang2.pagespec.PageSpecReader;5import com.galenframework.speclang2.pagespec.SectionFilter;6import com.galenframework.specs.page.PageSpec;7import com.galenframework.validation.CombinedValidationListener;8import com.galenframework.validation.PageValidation;9import com.galenframework.validation.SectionValidation;10import com.galenframework.validation.ValidationResult;11import de.qualityminds.gta.webapplication.exceptions.WrongPageValidationError;12import de.qualityminds.gta.webapplication.annotations.Spec;13import org.openqa.selenium.WebDriver;14import java.io.IOError;15import java.io.IOException;16import java.util.Collections;17import java.util.LinkedList;18import java.util.List;19import java.util.Properties;20public class Page extends net.serenitybdd.core.pages.PageObject {21	public Page(WebDriver driver) {22		super(driver);23		WebDriverRunner.setWebDriver(driver);24	}25	26	@Override27	public void shouldBeDisplayed() {28		List<ValidationResult> validationList = validatePage(true);29		if(!validationList.isEmpty()) {30			throw new WrongPageValidationError(validationList.toString());31		}32		super.shouldBeDisplayed();33	}34	35	private List<ValidationResult> validatePage(boolean fast){36		Spec specAnnotation = this.getClass().getAnnotation(Spec.class);37		if(specAnnotation==null) {38			return new LinkedList<>();39		}40		41		try {42			return galenCheck((fast && !specAnnotation.fast().isEmpty()) ? specAnnotation.fast() : specAnnotation.value());43		} catch (IOException e) {44			throw new IOError(e);45		}46	}47	48	private List<ValidationResult> galenCheck(String specPath) throws IOException {49		SectionFilter sectionFilter = new SectionFilter(new LinkedList<>(), new LinkedList<>());50		Properties properties = new Properties();51				52		SeleniumBrowser browser = new SeleniumBrowser(getDriver());53        PageSpecReader reader = new PageSpecReader();54        55        PageSpec pageSpec = reader.read(specPath, browser.getPage(), sectionFilter, properties, null, null);56        CombinedValidationListener listener = new CombinedValidationListener();57        PageValidation pageValidation = new PageValidation(browser, browser.getPage(), pageSpec, listener, sectionFilter);58		return new SectionValidation(pageSpec.getSections(), pageValidation, listener).check();59	}60}...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!!
