Best Galen code snippet using com.galenframework.specs.page.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
1import com.galenframework.browser.Browser;2import com.galenframework.specs.page.Locator;3import com.galenframework.specs.page.PageSection;4import com.galenframework.specs.page.PageSpec;5import com.galenframework.specs.page.PageSpecReader;6import com.galenframework.specs.page.PageSpecReaderException;7import com.galenframework.specs.page.PageSpecReaderFactory;8import com.galenframework.specs.page.PageSpecReaderFactoryException;9import com.galenframework.specs.page.PageSpecReaderFactoryImpl;10import com.galenframework.specs.page.PageSpecReaderImpl;11import com.galenframework.specs.page.PageSpecReaderResult;12import com.galenframework.specs.page.PageSpecReaderResultException;13import com.galenframework.specs.page.PageSpecReaderResultImpl;14import com.galenframework.specs.page.PageSpecReaderResultLocator;15import com.galenframework.specs.page.PageSpecReaderResultLocatorException;16import com.galenframework.specs.page.PageSpecReaderResultLocatorImpl;17import com.galenframework.specs.page.PageSpecReaderResultSection;18import com.galenframework.specs.page.PageSpecReaderResultSectionException;19import com.galenframework.specs.page.PageSpecReaderResultSectionImpl;20import com.galenframework.specs.page.PageSpecReaderResultSectionLocator;21import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorException;22import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorImpl;23import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSection;24import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionException;25import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionImpl;26import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionLocator;27import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionLocatorException;28import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionLocatorImpl;29import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionSection;30import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionSectionException;31import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionSectionImpl;32import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionSectionLocator;33import com.galenframework.specs.page.PageSpecReaderResultSectionLocatorSectionSectionLocatorException;34import com.galenframework.specs.page
Locator
Using AI Code Generation
1package com.galenframework.java.using.locator;2import com.galenframework.specs.page.Locator;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.support.ui.WebDriverWait;8import java.util.List;9import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;10public class UsingLocatorClass {11 public static void main(String[] args) throws Exception {12 WebDriver driver = new ChromeDriver();13 WebDriverWait wait = new WebDriverWait(driver, 30);14 Locator searchBoxLocator = new Locator("searchBox", By.id("twotabsearchtextbox"));15 WebElement searchBox = wait.until(visibilityOfElementLocated(searchBoxLocator.getBy()));16 searchBox.sendKeys("iPhone XR");17 WebElement searchButton = wait.until(visibilityOfElementLocated(searchButtonLocator.getBy()));18 searchButton.click();19 System.out.println("Search results count: " + searchResults.size());20 driver.quit();21 }22}
Locator
Using AI Code Generation
1import com.galenframework.specs.page.Locator;2import com.galenframework.specs.page.PageElement;3import com.galenframework.specs.page.PageSection;4public class LocatorExample {5 public static void main(String[] args) {6 PageElement element = new PageElement("element name", Locator.linkText("link text"));7 PageElement element2 = new PageElement("element name", Locator.css("css selector"));8 PageElement element3 = new PageElement("element name", Locator.id("id"));9 PageElement element4 = new PageElement("element name", Locator.name("name"));10 PageElement element5 = new PageElement("element name", Locator.partialLinkText("partial link text"));11 PageElement element6 = new PageElement("element name", Locator.tagName("tag name"));12 PageElement element7 = new PageElement("element name", Locator.xpath("xpath"));13 PageElement element8 = new PageElement("element name", Locator.className("class name"));14 PageElement element9 = new PageElement("element name", Locator.image("image.png"));15 PageElement element10 = new PageElement("element name", Locator.text("text"));16 PageElement element11 = new PageElement("element name", Locator.text("text").withArea(10, 10, 100, 100));17 PageElement element12 = new PageElement("element name", Locator.text("text").withParent(Locator.linkText("link text")));18 PageElement element13 = new PageElement("element name", Locator.text("text").withParent(Locator.linkText("link text")).withArea(10, 10, 100, 100));19 PageElement element14 = new PageElement("element name", Locator.text("text").withParent(Locator.linkText("link text")).withArea(10, 10, 100, 100).withIndex(1));20 PageElement element15 = new PageElement("element name", Locator.text("text").withParent(Locator.linkText("link text")).withArea(10, 10, 100, 100).withIndex(1).withSiblings(Locator.css("css selector")));21 PageElement element16 = new PageElement("element name", Locator.text("text").withParent(Locator.linkText("link text")).withArea(10, 10, 100, 100).withIndex(1).withSiblings(Locator.css("css
Locator
Using AI Code Generation
1package com.galenframework.java.using.locator;2import com.galenframework.java.using.locator.Locator;3import com.galenframework.java.using.locator.LocatorType;4import com.galenframework.java.using.locator.LocatorType;5public class LocatorTest {6 public static void main(String[] args) {7 Locator locator = new Locator(LocatorType.ID, "test-id");8 System.out.println(locator.getLocatorType());9 System.out.println(locator.getLocatorValue());10 }11}12package com.galenframework.java.using.locatorlist;13import com.galenframework.java.using.locator.Locator;14import com.galenframework.java.using.locator.LocatorType;15import com.galenframework.java.using.locatorlist.LocatorList;16public class LocatorListTest {17 public static void main(String[] args) {18 Locator locator = new Locator(LocatorType.ID, "test-id");19 LocatorList locatorList = new LocatorList();20 locatorList.add(locator);21 System.out.println(locatorList.get(0).getLocatorType());22 System.out.println(locatorList.get(0).getLocatorValue());23 }24}25package com.galenframework.java.using.pageelement;26import com.galenframework.java.using.locator.Locator;27import com.galenframework.java.using.locator.LocatorType;28import com.galenframework.java.using.locatorlist.LocatorList;29import com.galenframework.java.using.pageelement.PageElement;30import com.galenframework.java.using.pageelement.PageElementType;31public class PageElementTest {32 public static void main(String[] args) {33 Locator locator = new Locator(LocatorType.ID, "test-id");34 LocatorList locatorList = new LocatorList();35 locatorList.add(locator);36 PageElement pageElement = new PageElement("test-element", locatorList, PageElementType
Locator
Using AI Code Generation
1package com.galenframework.java.sample;2import com.galenframework.java.sample.Locator;3import java.io.IOException;4import java.util.List;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.chrome.ChromeOptions;10import org.testng.annotations.AfterTest;11import org.testng.annotations.BeforeTest;12import org.testng.annotations.Test;13public class Locator {14 WebDriver driver;15 public void setup() throws IOException {16 System.setProperty("webdriver.chrome.driver", "chromedriver.exe");17 ChromeOptions options = new ChromeOptions();18 options.addArguments("disable-infobars");19 driver = new ChromeDriver(options);20 driver.manage().window().maximize();21 }22 public void test() throws IOException {23 if (elements.size() > 0) {24 elements.get(0).click();25 }26 }27 public void tearDown() {28 driver.close();29 }30}31package com.galenframework.java.sample;32import java.io.IOException;33import org.openqa.selenium.WebDriver;34import org.openqa.selenium.chrome.ChromeDriver;35import org.openqa.selenium.chrome.ChromeOptions;36import org.testng.annotations.AfterTest;37import org.testng.annotations.BeforeTest;38import org.testng.annotations.Test;39public class Locator {40 WebDriver driver;41 public void setup() throws IOException {42 System.setProperty("webdriver.chrome.driver", "chromedriver.exe");43 ChromeOptions options = new ChromeOptions();44 options.addArguments("disable-infobars");45 driver = new ChromeDriver(options);46 driver.manage().window().maximize();47 }48 public void test() throws IOException {49 }50 public void tearDown() {51 driver.close();52 }53}
Locator
Using AI Code Generation
1import com.galenframework.specs.page.Locator;2import com.galenframework.specs.page.PageSection;3import com.galenframework.specs.page.PageSectionFactory;4import com.galenframework.specs.page.PageSectionLocator;5import com.galenframework.specs.page.PageSectionLocatorFactory;6import com.galenframework.specs.page.PageSectionLocatorType;7import com.galenframework.specs.page.PageSectionType;8import com.galenframework.specs.page.PageSectionTypeFactory;9public class LocatorExample {10 public static void main(String[] args) {11 Locator locator = new Locator("search box", PageSectionLocatorType.CSS, "#lst-ib");12 ps.addLocator(locator);13 PageSectionLocatorFactory locatorFactory = new PageSectionLocatorFactory();14 PageSectionTypeFactory pageSectionTypeFactory = new PageSectionTypeFactory();15 PageSectionType pageSectionType = pageSectionTypeFactory.createPageSectionType("home page");16 PageSectionLocator pageSectionLocator = locatorFactory.createPageSectionLocator(PageSectionLocatorType.CSS, "#lst-ib");17 Locator locator1 = new Locator("search box", pageSectionLocator, pageSectionType);18 ps.addLocator(locator1);19 PageSectionType pageSectionType1 = pageSectionTypeFactory.createPageSectionType("home page");20 PageSectionLocator pageSectionLocator1 = locatorFactory.createPageSectionLocator(PageSectionLocatorType.CSS, "#lst-ib");21 Locator locator2 = new Locator("search box", pageSectionLocator1, pageSectionType1);22 ps.addLocator(locator2);23 PageSectionType pageSectionType2 = pageSectionTypeFactory.createPageSectionType("home page");
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!!