How to use GmPageSpec class of com.galenframework.generator.model package

Best Galen code snippet using com.galenframework.generator.model.GmPageSpec

Source:SpecGenerator.java Github

copy

Full Screen

...14* limitations under the License.15******************************************************************************/16package com.galenframework.generator;17import com.galenframework.generator.builders.SpecGeneratorOptions;18import com.galenframework.generator.model.GmPageSpec;19import com.galenframework.page.Point;20import com.galenframework.page.Rect;21import java.io.IOException;22import java.io.InputStream;23import java.util.*;24import java.util.List;25import java.util.stream.Collectors;26import static java.lang.String.format;27import static java.util.Arrays.asList;28import static java.util.Collections.singletonList;29import static java.util.stream.Collectors.toList;30public class SpecGenerator {31 private PageItemJsonMapper piJsonMapper = new PageItemJsonMapper();32 public PageSpecGenerationResult generate(InputStream stream, SpecGeneratorOptions specGeneratorOptions) throws IOException {33 List<PageItem> pageItems = piJsonMapper.loadItems(stream);34 return generate(pageItems, specGeneratorOptions);35 }36 private PageSpecGenerationResult generate(List<PageItem> pageItems, SpecGeneratorOptions specGeneratorOptions) {37 Set<String> allObjectNames = extractItemNamesOnAllPages(pageItems);38 return generate(pageItems, allObjectNames, specGeneratorOptions);39 }40 private Set<String> extractItemNamesOnAllPages(List<PageItem> pageItems) {41 return pageItems.stream().map(PageItem::getName).distinct().collect(Collectors.toSet());42 }43 private PageSpecGenerationResult generate(List<PageItem> pageItems, Set<String> allObjectNames, SpecGeneratorOptions specGeneratorOptions) {44 List<PageItem> convertedItems = new LinkedList<>();45 PageItem screenItem = null;46 Size largestSize = new Size();47 for (PageItem pageItem : pageItems) {48 if (!"viewport".equals(pageItem.getName())) {49 convertedItems.add(pageItem);50 if (screenItem == null && "screen".equals(pageItem.getName())) {51 screenItem = pageItem;52 }53 if (largestSize.width < pageItem.getArea().getWidth()) {54 largestSize.width = pageItem.getArea().getWidth();55 largestSize.height = pageItem.getArea().getHeight();56 }57 }58 }59 // Sorting items by size first and then by location60 convertedItems.sort(bySizeAndLocation());61 removeDuplicatedElements(convertedItems);62 List<PageItemNode> rootPins = restructurePageItems(convertedItems);63 List<String> objectNamesPerPage = new LinkedList<>();64 rootPins.forEach(p -> p.visitTree(pin -> {65 objectNamesPerPage.add(pin.getPageItem().getName());66 if (pin.getChildren() != null) {67 sortPinsHorizontally(pin.getChildren());68 }69 }));70 SuggestionTestResult results = new SuggestionTestResult();71 rootPins.forEach(p -> p.visitTree(pin -> results.merge(proposeSpecsFor(pin, objectNamesPerPage, specGeneratorOptions))));72 List<String> missingObjects = proposeAbsenseSpecs(results, pageItems, allObjectNames);73 // adding missing objects to pins. For now we will put missing objects inside a first root pin74 missingObjects.forEach(missingObjectName -> {75 new PageItemNode(new PageItem(missingObjectName)).moveToParent(rootPins.get(0));76 objectNamesPerPage.add(missingObjectName);77 });78 return new PageSpecGenerationResult(largestSize, objectNamesPerPage, rootPins, results);79 }80 private List<String> proposeAbsenseSpecs(SuggestionTestResult results, List<PageItem> pageItems, Set<String> allObjectNames) {81 Set<String> allItemsOnCurrentPage = pageItems.stream().map(PageItem::getName).collect(Collectors.toSet());82 List<String> missingObjectNames = new LinkedList<>();83 allObjectNames.stream().filter(itemName -> !allItemsOnCurrentPage.contains(itemName)).forEach(itemName -> {84 results.getGeneratedObjectSpecs().put(itemName, singletonList(new SpecStatement("absent")));85 missingObjectNames.add(itemName);86 });87 return missingObjectNames;88 }89 private void removeDuplicatedElements(List<PageItem> convertedItems) {90 ListIterator<PageItem> it = convertedItems.listIterator();91 if (it.hasNext()) {92 PageItem item = it.next();93 while (it.hasNext()) {94 PageItem nextItem = it.next();95 if (nextItem.getArea().equals(item.getArea())) {96 it.remove();97 } else {98 item = nextItem;99 }100 }101 }102 }103 private Comparator<PageItem> bySizeAndLocation() {104 return (a, b) -> {105 int size = a.getArea().getWidth() * a.getArea().getHeight() - b.getArea().getWidth() * b.getArea().getHeight();106 if (size != 0) {107 return size;108 } else {109 int diff = a.getArea().getLeft() - b.getArea().getLeft();110 if (diff != 0) {111 return diff;112 } else {113 return a.getArea().getTop() - b.getArea().getTop();114 }115 }116 };117 }118 /**119 * Orders page items into a tree by their area. Tries to fit one item inside another120 * @param items121 * @return A list of pins which are root elements (don't have a parent)122 */123 private List<PageItemNode> restructurePageItems(List<PageItem> items) {124 List<PageItemNode> pins = items.stream().map(PageItemNode::new).collect(toList());125 for (PageItemNode pinA : pins) {126 for (PageItemNode pinB: pins) {127 if (pinA != pinB) {128 if (isInside(pinA.getPageItem().getArea(), pinB.getPageItem().getArea())) {129 if (pinB.getParent() == pinA) {130 throw new RuntimeException(format("The following objects have identical areas: %s, %s. Please remove one of the objects", pinA.getPageItem().getName(), pinB.getPageItem().getName()));131 }132 pinA.moveToParent(pinB);133 break;134 }135 }136 }137 }138 return pins.stream().filter(pin -> pin.getParent() == null && pin.getChildren().size() > 0).collect(toList());139 }140 private SuggestionTestResult proposeSpecsFor(PageItemNode pin, List<String> objectNamesPerPage, SpecGeneratorOptions specGeneratorOptions) {141 SuggestionTestResult allResults = new SuggestionTestResult();142 SpecSuggester specSuggester = new SpecSuggester(new SuggestionOptions(objectNamesPerPage));143 if (pin.getParent() != null) {144 allResults.merge(specSuggester.suggestSpecsForTwoObjects(asList(pin.getParent(), pin), SpecSuggester.parentSuggestions, specGeneratorOptions));145 }146 if (pin.getChildren() != null && !pin.getChildren().isEmpty()) {147 List<PageItemNode> horizontallySortedPins = pin.getChildren();148 List<PageItemNode> verticallySortedPins = copySortedVertically(pin.getChildren());149 if (specGeneratorOptions.isUseGalenExtras()) {150 allResults.merge(specSuggester.suggestSpecsForMultipleObjects(horizontallySortedPins, SpecSuggester.horizontallyOrderComplexRulesSuggestions, specGeneratorOptions));151 allResults.merge(specSuggester.suggestSpecsForMultipleObjects(verticallySortedPins, SpecSuggester.verticallyOrderComplexRulesSuggestions, specGeneratorOptions));152 }153 allResults.merge(specSuggester.suggestSpecsRayCasting(pin, horizontallySortedPins, specGeneratorOptions));154 allResults.merge(specSuggester.suggestSpecsForSingleObject(horizontallySortedPins, SpecSuggester.singleItemSuggestions, specGeneratorOptions));155 }156 return allResults;157 }158 private void sortPinsHorizontally(List<PageItemNode> pins) {159 Collections.sort(pins, (a,b) -> {160 int ax = a.getPageItem().getArea().getLeft();161 int ay = a.getPageItem().getArea().getTop();162 int bx = b.getPageItem().getArea().getLeft();163 int by = b.getPageItem().getArea().getTop();164 if (ax != bx) {165 return ax - bx;166 } else {167 return ay - by;168 }169 });170 }171 private List<PageItemNode> copySortedVertically(List<PageItemNode> pins) {172 ArrayList<PageItemNode> sortedPins = new ArrayList<>(pins);173 Collections.sort(sortedPins, (a,b) -> {174 int ax = a.getPageItem().getArea().getLeft();175 int ay = a.getPageItem().getArea().getTop();176 int bx = b.getPageItem().getArea().getLeft();177 int by = b.getPageItem().getArea().getTop();178 if (ay != by) {179 return ay - by;180 } else {181 return ax - bx;182 }183 });184 return sortedPins;185 }186 private boolean isInside(Rect area, Rect areaParent) {187 for (Point p : area.getPoints()) {188 if (!areaParent.contains(p)) {189 return false;190 }191 }192 return true;193 }194 public static String generateSpecSections(PageSpecGenerationResult result) {195 StringBuilder finalSpec = new StringBuilder();196 GmPageSpec pageSpecGM = GmPageSpec.create(result);197 finalSpec.append(pageSpecGM.render());198 return finalSpec.toString();199 }200 public static String generatePageSpec(PageSpecGenerationResult result, SpecGeneratorOptions specGeneratorOptions) {201 StringBuilder builder = new StringBuilder();202 if (specGeneratorOptions.isUseGalenExtras()) {203 builder.append("@lib galen-extras\n\n");204 }205 return builder.append(SpecGenerator.generateSpecSections(result))206 .toString();207 }208}...

Full Screen

Full Screen

Source:GmPageSpec.java Github

copy

Full Screen

...22import java.io.InputStreamReader;23import java.io.StringWriter;24import java.util.*;25import static java.util.stream.Collectors.toList;26public class GmPageSpec {27 private List<GmPageSection> sections = new LinkedList<>();28 public static GmPageSpec create(PageSpecGenerationResult result) {29 GmPageSpec pageSpec = new GmPageSpec();30 GmPageSection skeletonSection = pageSpec.createNewSection("Skeleton");31 Map<PageItemNode, GmPageSection> pinPageSections = new HashMap<>();32 result.getObjects().forEach(rootObject -> rootObject.getChildren().forEach(bigPin -> {33 GmPageSection pageSection = pageSpec.createNewSection(bigPin.getPageItem().getName() + " elements");34 bigPin.visitTree(p -> {35 if (p == bigPin) {36 pinPageSections.put(p, skeletonSection);37 } else {38 pinPageSections.put(p, pageSection);39 }40 });41 }));42 Map<String, List<SpecStatement>> generatedRules = result.getSuggestionResults().getGeneratedRules();43 Map<String, List<SpecStatement>> generatedObjectSpecs = result.getSuggestionResults().getGeneratedObjectSpecs();...

Full Screen

Full Screen

GmPageSpec

Using AI Code Generation

copy

Full Screen

1import com.galenframework.generator.model.GmPageSpec;2import com.galenframework.generator.model.GmSpec;3import com.galenframework.generator.model.GmSpecs;4import com.galenframework.generator.model.GmSpecsGroup;5import com.galenframework.generator.model.GmTag;6import com.galenframework.generator.model.GmTags;7import java.util.ArrayList;8import java.util.HashMap;9import java.util.List;10import java.util.Map;11public class GmPageSpecExample {12 public static void main(String[] args) {13 GmPageSpec gmPageSpec = new GmPageSpec();14 gmPageSpec.setPageName("Example page");15 GmTags gmTags = new GmTags();16 List<GmTag> gmTagList = new ArrayList<GmTag>();17 GmTag gmTag = new GmTag();18 gmTag.setName("tag1");19 gmTagList.add(gmTag);20 gmTags.setTags(gmTagList);21 gmPageSpec.setTags(gmTags);22 GmSpecsGroup gmSpecsGroup = new GmSpecsGroup();23 gmSpecsGroup.setGroupName("Example group");24 GmSpecs gmSpecs = new GmSpecs();25 List<GmSpec> gmSpecList = new ArrayList<GmSpec>();26 GmSpec gmSpec = new GmSpec();27 gmSpec.setObject("object1");28 gmSpec.setSpec("spec1");29 gmSpecList.add(gmSpec);30 gmSpecs.setSpecs(gmSpecList);31 gmSpecsGroup.setSpecs(gmSpecs);32 GmSpecsGroup gmSpecsGroup2 = new GmSpecsGroup();33 gmSpecsGroup2.setGroupName("Example group2");34 GmSpecs gmSpecs2 = new GmSpecs();35 List<GmSpec> gmSpecList2 = new ArrayList<GmSpec>();36 GmSpec gmSpec2 = new GmSpec();37 gmSpec2.setObject("object2");38 gmSpec2.setSpec("spec2");39 gmSpecList2.add(gmSpec2);40 gmSpecs2.setSpecs(gmSpecList2);41 gmSpecsGroup2.setSpecs(gmSpecs2);

Full Screen

Full Screen

GmPageSpec

Using AI Code Generation

copy

Full Screen

1package com.galenframework.generator;2import com.galenframework.generator.model.GmPageSpec;3import java.io.File;4public class GmPageSpecExample {5 public static void main(String[] args) {6 GmPageSpec gmPageSpec = new GmPageSpec();7 gmPageSpec.setPageName("testPage");8 gmPageSpec.setPageTitle("testPage");9 gmPageSpec.setPageWidth(800);10 gmPageSpec.setPageHeight(600);11 File file = new File("testPage.gspec");12 gmPageSpec.save(file);13 }14}15package com.galenframework.generator;16import com.galenframework.generator.model.GmPageSpec;17import java.io.File;18public class GmPageSpecExample {19 public static void main(String[] args) {20 GmPageSpec gmPageSpec = new GmPageSpec();21 gmPageSpec.setPageName("testPage");22 gmPageSpec.setPageTitle("testPage");23 gmPageSpec.setPageWidth(800);24 gmPageSpec.setPageHeight(600);25 File file = new File("testPage.gspec");26 gmPageSpec.save(file);27 }28}29package com.galenframework.generator;30import com.galenframework.generator.model.GmPageSpec;31import java.io.File;32public class GmPageSpecExample {33 public static void main(String[] args) {34 GmPageSpec gmPageSpec = new GmPageSpec();35 gmPageSpec.setPageName("testPage");36 gmPageSpec.setPageTitle("testPage");37 gmPageSpec.setPageWidth(800);38 gmPageSpec.setPageHeight(600);39 File file = new File("testPage.gspec");40 gmPageSpec.save(file);41 }42}43package com.galenframework.generator;44import com.galenframework.generator.model.GmPageSpec

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Galen automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful