Best Galen code snippet using com.galenframework.page.PageElement
Source:InsideValidationTest.java  
...24import java.util.List;25import com.galenframework.page.Rect;26import com.galenframework.reports.model.LayoutMeta;27import com.galenframework.specs.*;28import com.galenframework.page.PageElement;29import com.galenframework.validation.ValidationObject;30import org.testng.annotations.DataProvider;31public class InsideValidationTest extends ValidationTestBase {32    public static final List<ValidationObject> NO_AREA = null;33    @Override34    @SuppressWarnings("serial")35    @DataProvider36    public Object[][] provideGoodSamples() {37        return new Object[][]{38            {specInside("container"), page(new HashMap<String, PageElement>(){{39                put("object", element(10, 20, 100, 100));40                put("container", element(10, 10, 110, 110));41            }})},42            {specInside("container", location(exact(10), RIGHT, TOP)), page(new HashMap<String, PageElement>(){{43                put("object", element(10, 20, 100, 100));44                put("container", element(10, 10, 110, 110));45            }})},46            {specInsidePartly("container", location(exact(10), LEFT, TOP)), page(new HashMap<String, PageElement>(){{47                put("object", element(20, 20, 200, 100));48                put("container", element(10, 10, 110, 110));49            }})},50            {specInside("container", location(between(5, 12), RIGHT, TOP)), page(new HashMap<String, PageElement>(){{51                put("object", element(10, 20, 100, 100));52                put("container", element(10, 10, 110, 110));53            }})},54            {specInside("container", location(between(5, 20), LEFT, RIGHT, TOP)), page(new HashMap<String, PageElement>(){{55                put("object", element(10, 10, 100, 100));56                put("container", element(5, 5, 120, 120));57            }})},58            {specInside("container", location(exact(5), LEFT), location(between(5, 15), TOP)), page(new HashMap<String, PageElement>(){{59                put("object", element(10, 15, 100, 100));60                put("container", element(5, 5, 120, 120));61            }})},62            {specInside("container", location(exact(20).withPercentOf("container/height"), TOP)), page(new HashMap<String, PageElement>(){{63                put("object", element(10, 15, 100, 20));64                put("container", element(5, 5, 120, 50));65            }})},66            {specInside("container", location(between(15, 22).withPercentOf("container/height"), TOP)), page(new HashMap<String, PageElement>(){{67                put("object", element(10, 15, 100, 20));68                put("container", element(5, 5, 120, 50));69            }})},70            // checking that it allows 2 pixels overlap71            {specInside("container", location(exact(5), TOP)), page(new HashMap<String, PageElement>(){{72                put("object", element(0, 5, 102, 97));73                put("container", element(0, 0, 100, 100));74            }})}75        };76    }77    @SuppressWarnings("serial")78    @DataProvider79    public Object[][] provideBadSamples() {80        return new Object[][]{81            {validationResult(areas(new ValidationObject(new Rect(10, 10, 500, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),82                    messages("\"object\" is not completely inside. The offset is 380px."), NULL_META),83                specInside("container"), page(new HashMap<String, PageElement>(){{84                    put("object", element(10, 10, 500, 50));85                    put("container", element(0, 0, 130, 120));86                }})87            },88            {validationResult(areas(new ValidationObject(new Rect(10, 10, 500, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),89                    messages("\"object\" is not completely inside. The offset is 380px."), NULL_META),90                specInside("container", location(exact(10), LEFT)), page(new HashMap<String, PageElement>(){{91                    put("object", element(10, 10, 500, 50));92                    put("container", element(0, 0, 130, 120));93                }})94            },95            {validationResult(areas(new ValidationObject(new Rect(10, 10, 500, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),96                    messages("\"object\" is not completely inside. The offset is 380px."), NULL_META),97                specInside("container", location(exact(10), LEFT)), page(new HashMap<String, PageElement>(){{98                    put("object", element(10, 10, 500, 50));99                    put("container", element(0, 0, 130, 120));100                }})101            },102            {validationResult(areas(new ValidationObject(new Rect(190, 110, 500, 500), "object"), new ValidationObject(new Rect(10, 10, 100, 100), "container")),103                    messages("\"object\" is 180px left instead of 10px"),104                    asList(LayoutMeta.distance("object", LEFT, "container", LEFT, "10px", "180px"))),105                specInsidePartly("container", location(exact(10), LEFT)), page(new HashMap<String, PageElement>(){{106                    put("object", element(190, 110, 500, 500));107                    put("container", element(10, 10, 100, 100));108                }})109            },110            {validationResult(areas(new ValidationObject(new Rect(30, 10, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),111                    messages("\"object\" is 30px left instead of 10px"),112                    asList(LayoutMeta.distance("object", LEFT, "container", LEFT, "10px", "30px"))),113                specInside("container", location(exact(10), LEFT)), page(new HashMap<String, PageElement>(){{114                    put("object", element(30, 10, 50, 50));115                    put("container", element(0, 0, 130, 120));116                }})117            },118            {validationResult(areas(new ValidationObject(new Rect(30, 20, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),119                    messages("\"object\" is 30px left and 20px top instead of 10px"),120                    asList( LayoutMeta.distance("object", LEFT, "container", LEFT, "10px", "30px"),121                            LayoutMeta.distance("object", TOP, "container", TOP, "10px", "20px") )122            ),123                specInside("container", location(exact(10), LEFT, TOP)), page(new HashMap<String, PageElement>(){{124                    put("object", element(30, 20, 50, 50));125                    put("container", element(0, 0, 130, 120));126                }})127            },128            {validationResult(areas(new ValidationObject(new Rect(30, 10, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),129                    messages("\"object\" is 50px right instead of 10px"),130                    asList(LayoutMeta.distance("object", RIGHT, "container", RIGHT, "10px", "50px"))131                ),132                specInside("container", location(exact(10), RIGHT)), page(new HashMap<String, PageElement>(){{133                    put("object", element(30, 10, 50, 50));134                    put("container", element(0, 0, 130, 120));135                }})},136            {validationResult(areas(new ValidationObject(new Rect(30, 20, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),137                    messages("\"object\" is 20px top instead of 10px"),138                    asList(LayoutMeta.distance("object", TOP, "container", TOP, "10px", "20px"))139                ),140                specInside("container", location(exact(10), TOP)), page(new HashMap<String, PageElement>(){{141                    put("object", element(30, 20, 50, 50));142                    put("container", element(0, 0, 130, 120));143                }})},144            {validationResult(areas(new ValidationObject(new Rect(30, 10, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),145                    messages("\"object\" is 60px bottom instead of 10px"),146                    asList(LayoutMeta.distance("object", BOTTOM, "container", BOTTOM, "10px", "60px"))147                ),148                specInside("container", location(exact(10), BOTTOM)), page(new HashMap<String, PageElement>(){{149                    put("object", element(30, 10, 50, 50));150                    put("container", element(0, 0, 130, 120));151                }})},152            {validationResult(areas(new ValidationObject(new Rect(30, 10, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),153                    messages("\"object\" is 30px left which is not in range of 10 to 20px"),154                    asList(LayoutMeta.distance("object", LEFT, "container", LEFT, "10 to 20px", "30px"))155                ),156                specInside("container", location(between(10, 20), LEFT)), page(new HashMap<String, PageElement>(){{157                    put("object", element(30, 10, 50, 50));158                    put("container", element(0, 0, 130, 120));159                }})},160            {validationResult(NO_AREA, messages("Cannot find locator for \"container\" in page spec"), NULL_META),161                specInside("container", location(between(10, 20), LEFT)), page(new HashMap<String, PageElement>(){{162                    put("object", element(30, 10, 50, 50));163                }})},164            {validationResult(areas(new ValidationObject(new Rect(30, 5, 50, 50), "object"), new ValidationObject(new Rect(0, 0, 130, 120), "container")),165                    messages("\"object\" is 30px left instead of 10px and 5px top instead of 20px"),166                    asList(167                            LayoutMeta.distance("object", LEFT, "container", LEFT, "10px", "30px"),168                            LayoutMeta.distance("object", TOP, "container", TOP, "20px", "5px")169                    )170                ),171                specInside("container", location(exact(10), LEFT), location(exact(20), TOP)), page(new HashMap<String, PageElement>(){{172                    put("object", element(30, 5, 50, 50));173                    put("container", element(0, 0, 130, 120));174                }})},175            {validationResult(areas(new ValidationObject(new Rect(30, 5, 10, 50), "object"), new ValidationObject(new Rect(0, 0, 50, 120), "container")),176                    messages("\"object\" is 60% [30px] left instead of 20% [10px]"), NULL_META),177                specInside("container", location(exact(20).withPercentOf("container/width"), LEFT)), page(new HashMap<String, PageElement>(){{178                    put("object", element(30, 5, 10, 50));179                    put("container", element(0, 0, 50, 120));180                }})},181            {validationResult(areas(new ValidationObject(new Rect(30, 5, 10, 50), "object"), new ValidationObject(new Rect(0, 0, 50, 120), "container")),182                    messages("\"object\" is 60% [30px] left which is not in range of 20 to 40% [10 to 20px]"), NULL_META),183                specInside("container", location(between(20, 40).withPercentOf("container/width"), LEFT)), page(new HashMap<String, PageElement>(){{184                    put("object", element(30, 5, 10, 50));185                    put("container", element(0, 0, 50, 120));186                }})},187            {validationResult(NO_AREA, messages("\"object\" is absent on page"), NULL_META),188                specInside("container", location(exact(10), LEFT), location(exact(20), TOP)), page(new HashMap<String, PageElement>(){{189                    put("object", absentElement(30, 5, 50, 50));190                    put("container", element(0, 0, 130, 120));191                }})},192            {validationResult(NO_AREA, messages("\"object\" is not visible on page"), NULL_META),193                specInside("container", location(exact(10), LEFT), location(exact(20), TOP)), page(new HashMap<String, PageElement>(){{194                    put("object", invisibleElement(30, 5, 50, 50));195                    put("container", element(0, 0, 130, 120));196                }})},197            {validationResult(NO_AREA, messages("\"container\" is absent on page"), NULL_META),198                specInside("container", location(exact(10), LEFT), location(exact(20), TOP)), page(new HashMap<String, PageElement>(){{199                    put("object", element(30, 5, 50, 50));200                    put("container", absentElement(0, 0, 130, 120));201                }})},202            {validationResult(NO_AREA, messages("\"container\" is not visible on page"), NULL_META),203                specInside("container", location(exact(10), LEFT), location(exact(20), TOP)), page(new HashMap<String, PageElement>(){{204                    put("object", element(30, 5, 50, 50));205                    put("container", invisibleElement(0, 0, 130, 120));206                }})}207        };208    }209    private SpecInside specInside(String parentObjectName, Location...locations) {210        return new SpecInside(parentObjectName, asList(locations));211    }212    private SpecInside specInsidePartly(String parentObjectName, Location...locations) {213        return new SpecInside(parentObjectName, asList(locations)).withPartlyCheck();214    }215}...Source:GalenPageDumpWrapper.java  
...17import com.cognizant.cognizantits.engine.constants.FilePath;18import com.fasterxml.jackson.databind.ObjectMapper;19import com.galenframework.api.GalenPageDump;20import com.galenframework.api.PageDump;21import com.galenframework.page.PageElement;22import com.galenframework.page.Rect;23import com.galenframework.utils.GalenUtils;24import java.io.File;25import java.io.IOException;26import java.nio.charset.Charset;27import java.util.ArrayList;28import java.util.HashMap;29import java.util.LinkedList;30import java.util.List;31import java.util.Map;32import java.util.Set;33import java.util.logging.Level;34import java.util.logging.Logger;35import java.util.regex.Pattern;36import org.apache.commons.io.FileUtils;37/**38 *39 * 40 */41public class GalenPageDumpWrapper extends GalenPageDump {42    public GalenPageDumpWrapper(String pageName) {43        super(pageName);44    }45    public void dumpPage(PageValidationWrapper pageValidation, String testCaseName, File reportFolder) throws IOException {46        if (!reportFolder.exists()) {47            if (!reportFolder.mkdirs()) {48                throw new RuntimeException("Cannot create dir: " + reportFolder.getAbsolutePath());49            }50        }51        Set<String> objectNames = pageValidation.elementMap.keySet();52        PageDump pageDump = new PageDump();53        pageDump.setTitle(testCaseName);54        List< Pattern> patterns = convertPatterns(getExcludedObjects());55        for (String objectName : objectNames) {56            if (!matchesExcludedPatterns(objectName, patterns)) {57                PageElement pageElement = pageValidation.findPageElement(objectName);58                if (pageElement.isVisible() && pageElement.getArea() != null) {59                    PageDump.Element element = new PageDump.Element(objectName, pageElement.getArea().toIntArray());60                    if (pageElement.isPresent() && pageElement.isVisible() && isWithinArea(pageElement, getMaxWidth(), getMaxHeight())) {61                        element.setHasImage(true);62                    }63                    pageDump.addElement(element);64                }65            }66        }67        if (!isOnlyImages()) {68            pageDump.setPageName(getPageName());69            exportAsJson(pageDump, new File(reportFolder.getAbsoluteFile() + File.separator + "page.js"));70        }71        exportAllScreenshots(pageDump, pageValidation.getBrowser(), reportFolder);72    }73    @Override74    public void exportAsJson(PageDump dump, File file) throws IOException {75        updatePageMap(dump.getTitle());76        makeSureFileExists(file);77        ObjectMapper objectMapper = new ObjectMapper();78        String json = objectMapper.writeValueAsString(dump);79        json = "var page=" + json;80        FileUtils.writeStringToFile(file, json, Charset.defaultCharset());81    }82    private boolean matchesExcludedPatterns(String objectName, List<Pattern> patterns) {83        for (Pattern pattern : patterns) {84            if (pattern.matcher(objectName).matches()) {85                return true;86            }87        }88        return false;89    }90    private List<Pattern> convertPatterns(List<String> excludedObjects) {91        List<Pattern> patterns = new LinkedList<>();92        if (excludedObjects != null) {93            for (String excludedObject : excludedObjects) {94                patterns.add(GalenUtils.convertObjectNameRegex(excludedObject));95            }96        }97        return patterns;98    }99    private static boolean isWithinArea(PageElement element, Integer maxWidth, Integer maxHeight) {100        Rect area = element.getArea();101        if (maxWidth != null && maxHeight != null) {102            return maxWidth * maxHeight > area.getWidth() * area.getHeight();103        } else if (maxWidth != null) {104            return maxWidth > area.getWidth();105        } else if (maxHeight != null) {106            return maxHeight > area.getHeight();107        } else {108            return true;109        }110    }111    @SuppressWarnings("unchecked")112    private void updatePageMap(String pageSource) {113        File file = new File(FilePath.getORpageListJsonFile());...PageElement
Using AI Code Generation
1import com.galenframework.page.PageElement;2import com.galenframework.page.Rect;3import com.galenframework.page.GalenPageAction;4import com.galenframework.page.PageElement;5import com.galenframework.page.Rect;6import com.galenframework.page.GalenPageAction;7import com.galenframework.page.PageElement;8import com.galenframework.page.Rect;9public class GalenPageAction {10    private final String action;11    private final PageElement element;12    private final Rect elementRect;13    private final String value;14    public GalenPageAction(String action, PageElement element, Rect elementRect, String value) {15        this.action = action;16        this.element = element;17        this.elementRect = elementRect;18        this.value = value;19    }20    public String getAction() {21        return action;22    }23    public PageElement getElement() {24        return element;25    }26    public Rect getElementRect() {27        return elementRect;28    }29    public String getValue() {30        return value;31    }32    public String toString() {33        return "GalenPageAction{" +34                '}';35    }36}37import com.galenframework.page.GalenPageAction;38import com.galenframework.page.PageElement;39import com.galenframework.page.Rect;40import com.galenframework.page.GalenPageAction;41import com.galenframework.page.PageElement;42import com.galenframework.page.Rect;43import com.galenframework.page.GalenPageAction;44import com.galenframework.page.PageElement;45import com.galenframework.page.Rect;46public class GalenPageAction {47    private final String action;48    private final PageElement element;49    private final Rect elementRect;50    private final String value;51    public GalenPageAction(String action, PageElement element, Rect elementRect, String value) {PageElement
Using AI Code Generation
1PageElement pageElement = new PageElement("div", "id=login");2Spec spec = new Spec("size", "200px", "200px");3LayoutObject layoutObject = new LayoutObject("login", pageElement, spec);4TestResult testResult = new TestResult("login", layoutObject);5TestResultContainer testResultContainer = new TestResultContainer(testResult);6PageElement pageElement = new PageElement("div", "id=login");7Spec spec = new Spec("size", "200px", "200px");8LayoutObject layoutObject = new LayoutObject("login", pageElement, spec);9TestResult testResult = new TestResult("login", layoutObject);10TestResultContainer testResultContainer = new TestResultContainer(testResult);11PageElement pageElement = new PageElement("div", "id=login");12Spec spec = new Spec("size", "200px", "200px");13LayoutObject layoutObject = new LayoutObject("login", pageElement, spec);14TestResult testResult = new TestResult("login", layoutObject);15TestResultContainer testResultContainer = new TestResultContainer(testResult);PageElement
Using AI Code Generation
1PageElement element = new PageElement("elementName", By.className("className"));2PageElement element = new PageElement("elementName", By.cssSelector("cssSelector"));3PageElement element = new PageElement("elementName", By.id("id"));4PageElement element = new PageElement("elementName", By.linkText("linkText"));5PageElement element = new PageElement("elementName", By.name("name"));6PageElement element = new PageElement("elementName", By.partialLinkText("partialLinkText"));7PageElement element = new PageElement("elementName", By.tagName("tagName"));8PageElement element = new PageElement("elementName", By.xpath("xpath"));9PageElement element = new PageElement("elementName", By.xpath("xpath"), "parentElementName");10PageElement element = new PageElement("elementName", By.xpath("xpath"), "parentElementName", "parentElementName");11PageElement element = new PageElement("elementName", By.xpath("xpath"), "parentElementName", "parentElementName", "parentElementName");12PageElement element = new PageElement("elementName", By.xpath("xpath"), "parentElementName", "parentElementName", "parentElementName", "parentElementName");13PageElement element = new PageElement("elementName", By.xpath("xpath"), "parentElementName", "parentElementName", "parentElementName", "parentElementName", "parentElementName");14PageElement element = new PageElement("elementName", By.xpath("xpath"), "parentElementName", "parentElementName", "parentElementName", "parentElementName", "parentElementName", "parentElementName");15PageElement element = new PageElement("ePageElement
Using AI Code Generation
1import java.util.List;2import com.galenframework.page.PageElement;3import com.galenframework.page.Element;4import com.galenframework.specs.Rectangle;5import com.galenframework.specs.Spec;6import com.galenframework.specs.SpecAssertion;7import com.galenframework.reports.LayoutReport;8import com.galenframework.reports.ReportBuilder;9public class GalenTest {10    public static void main(String[] args) throws Exception {11        PageElement pageElement = new PageElement();12        Rectangle rectangle = elements.get(0).getRectangle();13        Spec spec = new Spec("inside", rectangle);14        SpecAssertion specAssertion = new SpecAssertion(spec);15        LayoutReport layoutReport = specAssertion.check(elements.get(0));16        ReportBuilder reportBuilder = new ReportBuilder();17        reportBuilder.build(layoutReport, "C:\\Users\\Sudhanshu\\Desktop\\report");18    }19}PageElement
Using AI Code Generation
1package com.galenframework.java.using.page;2import com.galenframework.page.PageElement;3import com.galenframework.page.Rect;4import com.galenframework.page.selenium.SeleniumPage;5import com.galenframework.java.sample.components.SamplePage;6import java.io.IOException;7import java.util.List;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.firefox.FirefoxDriver;10public class GetPageElement {11    public static void main(String[] args) throws IOException {12        WebDriver webDriver = new FirefoxDriver();13        SeleniumPage seleniumPage = new SeleniumPage(webDriver);14        SamplePage samplePage = new SamplePage(seleniumPage);15        PageElement searchBox = samplePage.getSearchBox();16        Rect searchBoxArea = searchBox.getArea();17        System.out.println("Search box area: " + searchBoxArea);18        List<PageElement> searchButtons = samplePage.getSearchButtons();19        for (PageElement searchButton : searchButtons) {20            Rect searchButtonArea = searchButton.getArea();21            System.out.println("Search button area: " + searchButtonArea);22        }23        webDriver.close();24    }25}26package com.galenframework.java.sample.components;27import com.galenframework.page.PageElement;28import com.galenframework.page.selenium.SeleniumPage;29import org.openqa.selenium.By;30public class SamplePage {31    private final SeleniumPage seleniumPage;32    public SamplePage(SeleniumPage seleniumPage) {33        this.seleniumPage = seleniumPage;34    }35    public PageElement getSearchBox() {36        return seleniumPage.findElement(By.id("lst-ib"));37    }38    public PageElement getSearchButton() {39        return seleniumPage.findElement(By.name("btnG"));40    }41    public PageElement getSearchButton2() {42        return seleniumPage.findElement(By.name("btnK"));43    }44}45package com.galenframework.java.sample.components;46import com.gLearn 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!!
