Best Galen code snippet using com.galenframework.specs.page.Locator.Locator
Source:GalenSpecUtil.java
...17 * limitations under the License.18 * #L%19 */20package io.wcm.qa.glnm.galen.specs;21import static io.wcm.qa.glnm.selectors.base.SelectorFactory.fromLocator;22import java.util.ArrayList;23import java.util.Arrays;24import java.util.Collection;25import java.util.HashMap;26import java.util.List;27import java.util.Map;28import java.util.Map.Entry;29import java.util.Properties;30import org.apache.commons.collections4.CollectionUtils;31import org.apache.commons.collections4.ListUtils;32import org.apache.commons.lang3.ArrayUtils;33import org.apache.commons.lang3.StringUtils;34import org.slf4j.Logger;35import org.slf4j.LoggerFactory;36import com.galenframework.reports.model.LayoutReport;37import com.galenframework.speclang2.pagespec.SectionFilter;38import com.galenframework.specs.page.Locator;39import com.galenframework.specs.page.PageSpec;40import com.google.common.collect.Lists;41import io.wcm.qa.glnm.exceptions.GaleniumException;42import io.wcm.qa.glnm.selectors.SelectorFromLocator;43import io.wcm.qa.glnm.selectors.base.NestedSelector;44/**45 * Utility methods for handling Galen specs.46 *47 * @since 4.0.048 */49final class GalenSpecUtil {50 private static final Logger LOG = LoggerFactory.getLogger(GalenSpecUtil.class);51 static final Map<String, Object> EMPTY_JS_VARS = null;52 static final Properties EMPTY_PROPERTIES = new Properties();53 private GalenSpecUtil() {54 // do not instantiate55 }56 static SectionFilter getSectionFilter(String... tags) {57 if (ArrayUtils.isEmpty(tags)) {58 return getDefaultIncludeTags();59 }60 SectionFilter sectionFilter = getDefaultIncludeTags();61 List<String> includedTags = sectionFilter.getIncludedTags();62 if (CollectionUtils.isEmpty(includedTags)) {63 sectionFilter.setIncludedTags(Arrays.asList(tags));64 } else {65 CollectionUtils.addAll(includedTags, tags);66 }67 return sectionFilter;68 }69 static GalenSpecRun createRun(GalenSpec spec, LayoutReport report) {70 return new GalenSpecRun(spec, report);71 }72 private static String cleanName(String name) {73 if (LOG.isDebugEnabled()) {74 LOG.debug("mapping '" + name + "'");75 }76 String[] nameParts = name.split("\\.");77 List<String> namePartList = new ArrayList<>();78 for (String namePart : nameParts) {79 if (namePart.matches(".*-[0-9][0-9]*")) {80 namePartList.add(namePart.replaceFirst("-[0-9][0-9]*$", ""));81 }82 else {83 namePartList.add(namePart);84 }85 }86 String cleanName = StringUtils.join(namePartList, ".");87 if (LOG.isDebugEnabled()) {88 LOG.debug("clean name for muliple object locator '" + cleanName + "'");89 }90 return cleanName;91 }92 private static List<String> emptyList() {93 return Lists.newArrayList();94 }95 private static SectionFilter emptySectionFilter() {96 return new SectionFilter(emptyList(), emptyList());97 }98 private static Collection<NestedSelector> extractCollectionFromMapping(Map<String, SelectorFromLocator> objectMapping) {99 Collection<NestedSelector> objects = new ArrayList<>();100 Collection<SelectorFromLocator> values = objectMapping.values();101 for (SelectorFromLocator selector : values) {102 if (LOG.isDebugEnabled()) {103 LOG.debug("checking " + selector);104 }105 if (selector.hasParent()) {106 if (LOG.isDebugEnabled()) {107 LOG.debug("has parent " + selector);108 }109 NestedSelector parent = selector.getParent();110 if (LOG.isDebugEnabled()) {111 LOG.debug("parentName: '" + parent.elementName() + "'");112 }113 String parentCss = parent.asString();114 if (LOG.isDebugEnabled()) {115 LOG.debug("parentCss: '" + parentCss + "'");116 }117 SelectorFromLocator trueParent = objectMapping.get(parentCss);118 if (trueParent == null) {119 throw new GaleniumException("parent for '" + selector.elementName() + "' not found in spec ('" + parentCss + "')");120 }121 selector.setParent(trueParent);122 trueParent.addChild(selector);123 }124 else if (LOG.isDebugEnabled()) {125 LOG.debug("no parent found.");126 }127 objects.add(selector);128 if (LOG.isDebugEnabled()) {129 LOG.debug("added: " + selector);130 }131 }132 return objects;133 }134 private static Map<String, SelectorFromLocator> getObjectMapping(PageSpec spec) {135 Map<String, SelectorFromLocator> objectMapping = new HashMap<String, SelectorFromLocator>();136 Map<String, Locator> objects = spec.getObjects();137 if (LOG.isDebugEnabled()) {138 LOG.debug("mapping " + objects.size() + " selector candidates.");139 }140 for (Entry<String, Locator> entry : objects.entrySet()) {141 String name = cleanName(entry.getKey());142 Locator locator = entry.getValue();143 SelectorFromLocator selector = fromLocator(name, locator);144 String asString = selector.asString();145 if (objectMapping.containsKey(asString)) {146 if (LOG.isInfoEnabled()) {147 LOG.info("duplicate object:" + selector + " == " + objectMapping.get(asString));148 }149 }150 else {151 objectMapping.put(asString, selector);152 if (LOG.isDebugEnabled()) {153 LOG.debug("mapped: " + selector);154 }155 }156 }157 if (LOG.isInfoEnabled()) {158 LOG.info("mapped " + objectMapping.size() + " selectors.");159 }160 return objectMapping;161 }162 /**163 * Get tags device as Galen {@link com.galenframework.speclang2.pagespec.SectionFilter}.164 * @param tagsForThisRun tags to use in filter165 * @return filter ready for use with Galen166 * @since 4.0.0167 */168 static SectionFilter asSectionFilter(List<String> tagsForThisRun) {169 List<String> tagList = new ArrayList<String>();170 if (ListUtils.emptyIfNull(tagList).isEmpty()) {171 return emptySectionFilter();172 }173 tagList.addAll(tagsForThisRun);174 return new SectionFilter(tagList, emptyList());175 }176 /**177 * Get tags from current device as Galen {@link com.galenframework.speclang2.pagespec.SectionFilter}. Empty filter178 * when no device set.179 *180 * @return filter ready for use with Galen181 * @since 4.0.0182 */183 static SectionFilter getDefaultIncludeTags() {184 return emptySectionFilter();185 }186 /**187 * Get objects from {@link com.galenframework.specs.page.PageSpec}.188 *189 * @param spec to extract objects from190 * @return selectors for all objects found in spec191 * @since 4.0.0192 */193 static Collection<NestedSelector> getObjects(PageSpec spec) {194 Map<String, SelectorFromLocator> objectMapping = getObjectMapping(spec);195 return extractCollectionFromMapping(objectMapping);196 }197}...
Source:LocatorCorrectionsWrapper.java
...19 */20package io.wcm.qa.glnm.selectors;21import com.galenframework.specs.page.CorrectionsRect;22import com.galenframework.specs.page.CorrectionsRect.Correction;23import com.galenframework.specs.page.Locator;24/**25 * Convenience wrapper to add corrections without modifying original locator.26 *27 * @since 1.0.028 */29public class LocatorCorrectionsWrapper extends Locator {30 private CorrectionsRect additionalCorrections;31 /**32 * <p>Constructor for LocatorCorrectionsWrapper.</p>33 *34 * @param locator locator to delegate everything except additionalCorrections to35 * @param corrections additional corrections to use on this locator36 */37 public LocatorCorrectionsWrapper(Locator locator, CorrectionsRect corrections) {38 super(locator.getLocatorType(), locator.getLocatorValue(), locator.getIndex());39 setParent(locator.getParent());40 setAdditionalCorrections(corrections);41 }42 /**43 * <p>Getter for the field <code>additionalCorrections</code>.</p>44 *45 * @return a {@link com.galenframework.specs.page.CorrectionsRect} object.46 */47 public CorrectionsRect getAdditionalCorrections() {48 return additionalCorrections;49 }50 /** {@inheritDoc} */51 @Override52 public CorrectionsRect getCorrections() {...
Source:IcsFactory.java
...21import java.util.List;22import org.apache.commons.lang3.StringUtils;23import com.galenframework.specs.Spec;24import com.galenframework.specs.page.CorrectionsRect;25import com.galenframework.specs.page.Locator;26import com.galenframework.specs.page.ObjectSpecs;27import com.galenframework.specs.page.PageSection;28import com.galenframework.specs.page.PageSpec;29import io.wcm.qa.glnm.configuration.GaleniumConfiguration;30import io.wcm.qa.glnm.exceptions.GaleniumException;31import io.wcm.qa.glnm.selectors.base.Selector;32/**33 * Factory class to get image comparing Galen specs.34 *35 * @since 2.0.036 */37final class IcsFactory {38 private IcsFactory() {39 }40 /**41 * <p>getPageSpec.</p>42 *43 * @param def parameters for spec generation44 * @return a parsed Galen page spec45 */46 static PageSpec getPageSpec(IcsDefinition def) {47 checkSanity(def);48 // specs49 Spec spec = IcUtil.getSpecForText(IcUtil.getImageComparisonSpecText(def));50 ObjectSpecs objectSpecs = new ObjectSpecs(def.getElementName());51 Spec insideViewportSpec = IcUtil.getSpecForText("inside viewport");52 objectSpecs.addSpec(insideViewportSpec);53 objectSpecs.addSpec(spec);54 if (GaleniumConfiguration.isSamplingVerificationIgnore()) {55 spec.setOnlyWarn(true);56 insideViewportSpec.setOnlyWarn(true);57 }58 if (def.isZeroToleranceWarning()) {59 Spec zeroToleranceSpec = IcUtil.getSpecForText(IcUtil.getZeroToleranceImageComparisonSpecText(def));60 zeroToleranceSpec.setOnlyWarn(true);61 objectSpecs.addSpec(zeroToleranceSpec);62 }63 // page section64 PageSection pageSection = new PageSection(def.getSectionName());65 pageSection.addObjects(objectSpecs);66 // page spec67 PageSpec pageSpec = new PageSpec();68 pageSpec.addObject(def.getElementName(), def.getSelector().asLocator());69 List<Selector> objectsToIgnore = def.getObjectsToIgnore();70 if (!objectsToIgnore.isEmpty()) {71 CorrectionsRect corrections = def.getCorrections().getCorrectionsRect();72 for (Selector objectToIgnore : objectsToIgnore) {73 Locator asLocator = objectToIgnore.asLocator();74 if (corrections != null) {75 asLocator.withCorrections(corrections);76 }77 pageSpec.addObject(objectToIgnore.elementName(), asLocator);78 }79 }80 pageSpec.addSection(pageSection);81 return pageSpec;82 }83 private static void checkSanity(IcsDefinition def) {84 if (def == null) {85 throw new GaleniumException("Definition is null.");86 }87 if (def.getSelector() == null) {88 throw new GaleniumException("Definition has null Selector.");89 }90 if (StringUtils.isBlank(def.getFilename())) {91 throw new GaleniumException("Definition has empty filename.");...
Locator
Using AI Code Generation
1package com.galenframework.java.using.locator;2import java.io.IOException;3import java.util.List;4import com.galenframework.api.Galen;5import com.galenframework.reports.GalenTestInfo;6import com.galenframework.reports.model.LayoutReport;7import com.galenframework.specs.page.Locator;8import com.galenframework.specs.page.PageSpec;9import com.galenframework.specs.page.PageSpecReader;10import com.galenframework.specs.page.PageSection;11import com.galenframework.specs.page.PageSectionFilter;12import com.galenframework.specs.page.PageSectionFilterBuilder;13import com.galenframework.specs.page.PageSectionFilterBuilder.FilterOperator;14import com.galenframework.specs.page.PageSectionFilterBuilder.FilterType;15public class LocatorMethod {16 public static void main(String[] args) throws IOException {17 PageSpecReader reader = new PageSpecReader();18 PageSpec pageSpec = reader.read("specs/locatorMethod.spec");19 PageSection pageSection = pageSpec.getSection("loginPage");20 Locator locator = pageSection.getLocator();21 PageSectionFilter filter = locator.getFilter();22 FilterType type = filter.getType();23 FilterOperator operator = filter.getOperator();24 List<String> values = filter.getValues();25 String value = filter.getValue();26 System.out.println("Filter Type: "+type);27 System.out.println("Filter Operator: "+operator);28 System.out.println("Filter Value: "+value);29 System.out.println("Filter Values: "+values);30 GalenTestInfo test = GalenTestInfo.fromString("Layout test");
Locator
Using AI Code Generation
1package com.galenframework.java.usingLocators;2import com.galenframework.specs.page.Locator;3import com.galenframework.specs.page.PageElement;4import com.galenframework.specs.page.PageSection;5import java.util.ArrayList;6import java.util.List;7import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;11public class GalenLocator {12 public static void main(String[] args) throws InterruptedException {13 System.setProperty("webdriver.chrome.driver", "C:\\Users\\YourUserName\\Desktop\\chromedriver.exe");14 WebDriver driver = new ChromeDriver();15 driver.manage().window().maximize();16 Locator locator = new Locator();17 locator.setCss("nav > ul > li:nth-child(1) > a");18 locator.setTagName("a");19 locator.setClassName("navbar-brand");20 locator.setId("logo");21 PageElement pageElement = new PageElement();22 pageElement.setLocator(locator);23 PageSection pageSection = new PageSection();24 pageSection.setPageElement(pageElement);25 List<PageSection> pageSections = new ArrayList<>();26 pageSections.add(pageSection);27 WebElement element = driver.findElement(By.cssSelector("nav > ul > li:nth-child(1) > a"));28 if (element.isDisplayed()) {29 System.out.println("Element is visible");30 } else {31 System.out.println("Element is not visible");32 }33 }34}35How to use Galen Framework API in Java (Part 2)36How to use Galen Framework API in Java (Part 3)37How to use Galen Framework API in Java (Part 4)38How to use Galen Framework API in Java (Part 5)39How to use Galen Framework API in Java (Part 6)40How to use Galen Framework API in Java (Part 7)41How to use Galen Framework API in Java (Part 8)
Locator
Using AI Code Generation
1package com.galenframework.java.using;2import com.galenframework.specs.page.Locator;3public class LocatorExample {4 public static void main(String[] args) {5 Locator locator = Locator.withText("login");6 System.out.println(locator.toString());7 }8}9locator: text(login)10package com.galenframework.java.using;11import com.galenframework.specs.page.Locator;12public class LocatorExample {13 public static void main(String[] args) {14 Locator locator = Locator.withText("login").inside(Locator.withText("login"));15 System.out.println(locator.toString());16 }17}18locator: text(login) inside text(login)19package com.galenframework.java.using;20import com.galenframework.specs.page.Locator;21public class LocatorExample {22 public static void main(String[] args) {23 Locator locator = Locator.withText("login").inside(Locator.withText("login").inside(Locator.withText("login")));24 System.out.println(locator.toString());25 }26}27locator: text(login) inside text(login) inside text(login)28Locator.withText()29package com.galenframework.java.using;30import com.galenframework.specs.page.Locator;31public class LocatorExample {32 public static void main(String[] args) {33 Locator locator = Locator.withText("login").inside(Locator.withText("login").inside(Locator.withText("login")));34 System.out.println(locator.toString());35 }36}37locator: text(login) inside text(login) inside text(login)38Locator.withClass()39package com.galenframework.java.using;40import com.galenframework.specs.page.Locator;
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!!