How to use setGlobalVariables method of com.galenframework.speclang2.pagespec.PageSpecHandler class

Best Galen code snippet using com.galenframework.speclang2.pagespec.PageSpecHandler.setGlobalVariables

Source:PageSpecHandler.java Github

copy

Full Screen

...66 this.properties = new Properties();67 }68 this.varsParser = new VarsParser(new Context(), this.properties, jsExecutor);69 if (jsVariables != null) {70 setGlobalVariables(jsVariables);71 }72 }73 public PageSpecHandler(PageSpecHandler copy, String contextPath) {74 this.pageSpec = copy.pageSpec;75 this.page = copy.page;76 this.contextPath = contextPath;77 this.specReader = copy.specReader;78 this.jsExecutor = copy.jsExecutor;79 this.varsParser = copy.varsParser;80 this.sectionFilter = copy.sectionFilter;81 this.pageRules = copy.pageRules;82 this.properties = copy.properties;83 this.jsVariables = copy.jsVariables;84 }85 private static GalenJsExecutor createGalenJsExecutor(final PageSpecHandler pageSpecHandler) {86 GalenJsExecutor js = new GalenJsExecutor();87 js.putObject("_pageSpecHandler", pageSpecHandler);88 js.evalScriptFromLibrary("GalenSpecProcessing.js");89 if (pageSpecHandler.page instanceof SeleniumPage) {90 SeleniumPage seleniumPage = (SeleniumPage) pageSpecHandler.page;91 js.putObject("screen", new JsPageElement("screen", new ScreenElement(seleniumPage.getDriver())));92 js.putObject("viewport", new JsPageElement("viewport", new ViewportElement(seleniumPage.getDriver())));93 }94 js.getScope().defineProperty("isVisible", new BaseFunction() {95 @Override96 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {97 if (args.length == 0) {98 throw new IllegalArgumentException("Should take string argument, got nothing");99 }100 if (args[0] == null) {101 throw new IllegalArgumentException("Object name should be null");102 }103 return pageSpecHandler.isVisible(args[0].toString());104 }105 }, ScriptableObject.DONTENUM);106 js.getScope().defineProperty("isPresent", new BaseFunction() {107 @Override108 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {109 if (args.length == 0) {110 throw new IllegalArgumentException("Should take string argument, got nothing");111 }112 if (args[0] == null) {113 throw new IllegalArgumentException("Object name should be null");114 }115 return pageSpecHandler.isPresent(args[0].toString());116 }117 }, ScriptableObject.DONTENUM);118 js.getScope().defineProperty("count", new BaseFunction() {119 @Override120 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {121 if (args.length == 0) {122 throw new IllegalArgumentException("Should take string argument, got nothing");123 }124 if (args[0] == null) {125 throw new IllegalArgumentException("Object name should be null");126 }127 return pageSpecHandler.count(args[0].toString());128 }129 }, ScriptableObject.DONTENUM);130 js.getScope().defineProperty("find", new BaseFunction() {131 @Override132 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {133 return pageSpecHandler.find(getSingleStringArgument(args));134 }135 }, ScriptableObject.DONTENUM);136 js.getScope().defineProperty("findAll", new BaseFunction() {137 @Override138 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {139 return pageSpecHandler.findAll(getSingleStringArgument(args));140 }141 }, ScriptableObject.DONTENUM);142 js.getScope().defineProperty("first", new BaseFunction() {143 @Override144 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {145 return pageSpecHandler.first(getSingleStringArgument(args));146 }147 }, ScriptableObject.DONTENUM);148 js.getScope().defineProperty("last", new BaseFunction() {149 @Override150 public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {151 return pageSpecHandler.last(getSingleStringArgument(args));152 }153 }, ScriptableObject.DONTENUM);154 return js;155 }156 private static String getSingleStringArgument(Object[] args) {157 String singleArgument = null;158 if (args.length == 0) {159 throw new IllegalArgumentException("Should take one string argument, got none");160 } else if (args[0] == null) {161 throw new IllegalArgumentException("Pattern should not be null");162 } else if (args[0] instanceof NativeJavaObject) {163 NativeJavaObject njo = (NativeJavaObject) args[0];164 singleArgument = njo.unwrap().toString();165 } else {166 singleArgument = args[0].toString();167 }168 return singleArgument;169 }170 public Object isVisible(String objectName) {171 for (Map.Entry<String, Locator> object : pageSpec.getObjects().entrySet()) {172 if (object.getKey().equals(objectName)) {173 PageElement pageElement = page.getObject(object.getKey(), object.getValue());174 return pageElement != null && pageElement.isPresent() && pageElement.isVisible();175 }176 }177 return Boolean.FALSE;178 }179 public Object isPresent(String objectName) {180 for (Map.Entry<String, Locator> object : pageSpec.getObjects().entrySet()) {181 if (object.getKey().equals(objectName)) {182 PageElement pageElement = page.getObject(object.getKey(), object.getValue());183 return pageElement != null && pageElement.isPresent();184 }185 }186 return Boolean.FALSE;187 }188 public PageSpec buildPageSpec() {189 PageSpec cleanedSpec = new PageSpec();190 cleanedSpec.setObjects(pageSpec.getObjects());191 cleanedSpec.setSections(cleanEmptySections(pageSpec.getSections()));192 cleanedSpec.setObjectGroups(pageSpec.getObjectGroups());193 return cleanedSpec;194 }195 private List<PageSection> cleanEmptySections(List<PageSection> sections) {196 List<PageSection> cleanedSections = new LinkedList<>();197 for (PageSection pageSection : sections) {198 PageSection cleanedSection = pageSection.cleanSection();199 if (!pageSection.isEmpty()) {200 cleanedSections.add(cleanedSection);201 }202 }203 return cleanedSections;204 }205 public void addSection(PageSection section) {206 PageSection sameSection = findSection(section.getName());207 if (sameSection != null) {208 sameSection.mergeSection(section);209 } else {210 pageSpec.addSection(section);211 }212 }213 private PageSection findSection(String name) {214 for (PageSection pageSection : pageSpec.getSections()) {215 if (pageSection.getName().equals(name)) {216 return pageSection;217 }218 }219 return null;220 }221 public SpecReader getSpecReader() {222 return specReader;223 }224 public void addObjectToSpec(String objectName, Locator locator) {225 pageSpec.addObject(objectName, locator);226 }227 @Override228 public int count(String regex) {229 List<String> objectNames = pageSpec.findOnlyExistingMatchingObjectNames(regex);230 return objectNames.size();231 }232 @Override233 public JsPageElement find(String name) {234 List<String> objectNames = pageSpec.findOnlyExistingMatchingObjectNames(name);235 if (!objectNames.isEmpty()) {236 String objectName = objectNames.get(0);237 Locator locator = pageSpec.getObjects().get(objectName);238 if (locator != null && page != null) {239 PageElement pageElement = page.getObject(objectName, locator);240 if (pageElement != null) {241 return new JsPageElement(objectName, pageElement);242 }243 }244 }245 return new JsPageElement(name, new AbsentPageElement());246 }247 @Override248 public JsPageElement[] findAll(String objectsStatements) {249 List<String> objectNames = pageSpec.findAllObjectsMatchingStrictStatements(objectsStatements);250 List<JsPageElement> jsElements = new ArrayList<>(objectNames.size());251 for (String objectName : objectNames) {252 Locator locator = pageSpec.getObjects().get(objectName);253 PageElement pageElement = null;254 if (locator != null) {255 pageElement = page.getObject(objectName, locator);256 }257 if (pageElement != null) {258 jsElements.add(new JsPageElement(objectName, pageElement));259 } else {260 jsElements.add(new JsPageElement(objectName, new AbsentPageElement()));261 }262 }263 return jsElements.toArray(new JsPageElement[jsElements.size()]);264 }265 @Override266 public JsPageElement first(String objectsStatements) {267 return extractSingleElement(objectsStatements, list -> list.get(0));268 }269 @Override270 public JsPageElement last(String objectsStatements) {271 return extractSingleElement(objectsStatements, list -> list.get(list.size() - 1));272 }273 private JsPageElement extractSingleElement(String objectsStatements, FilterFunction<String> filterFunction) {274 List<String> objectNames = pageSpec.findAllObjectsMatchingStrictStatements(objectsStatements);275 PageElement pageElement = null;276 String objectName = objectsStatements;277 if (!objectNames.isEmpty()) {278 objectName = filterFunction.filter(objectNames);279 Locator locator = pageSpec.getObjects().get(objectName);280 if (locator != null) {281 pageElement = page.getObject(objectName, locator);282 }283 }284 if (pageElement != null) {285 return new JsPageElement(objectName, pageElement);286 } else {287 return new JsPageElement(objectName, new AbsentPageElement());288 }289 }290 public void setGlobalVariable(String name, Object value, StructNode source) {291 if (!isValidVariableName(name)) {292 throw new SyntaxException(source, "Invalid name for variable: " + name);293 }294 if (value != null && value instanceof NativeJavaObject) {295 jsExecutor.putObject(name, ((NativeJavaObject) value).unwrap());296 } else {297 jsExecutor.putObject(name, value);298 }299 }300 private boolean isValidVariableName(String name) {301 if (name.isEmpty()) {302 return false;303 }304 for (int i = 0; i < name.length(); i++) {305 int symbol = (int)name.charAt(i);306 if (!(symbol > 64 && symbol < 91) //checking uppercase letters307 && !(symbol > 96 && symbol < 123) //checking lowercase letters308 && !(symbol > 47 && symbol < 58 && i > 0) //checking numbers and that its not the first letter309 && symbol != 95) /*underscore*/ {310 return false;311 }312 }313 return true;314 }315 public VarsParser getVarsParser() {316 return varsParser;317 }318 public StructNode processExpressionsIn(StructNode originNode) {319 String result;320 try {321 result = getVarsParser().parse(originNode.getName());322 } catch (Exception ex) {323 throw new SyntaxException(originNode, "JavaScript error inside statement", ex);324 }325 StructNode processedNode = new StructNode(result);326 processedNode.setPlace(originNode.getPlace());327 processedNode.setChildNodes(originNode.getChildNodes());328 return processedNode;329 }330 public void setGlobalVariables(Map<String, Object> variables, StructNode originNode) {331 for(Map.Entry<String, Object> variable : variables.entrySet()) {332 setGlobalVariable(variable.getKey(), variable.getValue(), originNode);333 }334 }335 public void setGlobalVariables(Map<String, Object> variables) {336 setGlobalVariables(variables, StructNode.UNKNOWN_SOURCE);337 }338 public String getContextPath() {339 return contextPath;340 }341 public List<PageSection> getPageSections() {342 return pageSpec.getSections();343 }344 public void runJavaScriptFromFile(String scriptPath) {345 jsExecutor.runJavaScriptFromFile(scriptPath);346 }347 public String getFullPathToResource(String scriptPath) {348 if (contextPath != null) {349 return contextPath + File.separator + scriptPath;350 } else {...

Full Screen

Full Screen

setGlobalVariables

Using AI Code Generation

copy

Full Screen

1import com.galenframework.speclang2.pagespec.PageSpecHandler2import com.galenframework.speclang2.pagespec.SectionFilter3PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])4SectionFilter sectionFilter = new SectionFilter()5sectionFilter.setTags(["tag1", "tag2"])6sectionFilter.setSections(["section1", "section2"])7PageSpecHandler.setSectionFilter(sectionFilter)8PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])9PageSpecHandler.setSectionFilter(sectionFilter)10import com.galenframework.speclang2.pagespec.PageSpecHandler11import com.galenframework.speclang2.pagespec.SectionFilter12PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])13SectionFilter sectionFilter = new SectionFilter()14sectionFilter.setTags(["tag1", "tag2"])15sectionFilter.setSections(["section1", "section2"])16PageSpecHandler.setSectionFilter(sectionFilter)17PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])18PageSpecHandler.setSectionFilter(sectionFilter)19import com.galenframework.speclang2.pagespec.PageSpecHandler20import com.galenframework.speclang2.pagespec.SectionFilter21PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])22SectionFilter sectionFilter = new SectionFilter()23sectionFilter.setTags(["tag1", "tag2"])24sectionFilter.setSections(["section1", "section2"])25PageSpecHandler.setSectionFilter(sectionFilter)26PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])27PageSpecHandler.setSectionFilter(sectionFilter)28import com.galenframework.speclang2.pagespec.PageSpecHandler29import com.galenframework.speclang2.pagespec.SectionFilter30PageSpecHandler.setGlobalVariables([key1: "value1", key2: "value2"])31SectionFilter sectionFilter = new SectionFilter()

Full Screen

Full Screen

setGlobalVariables

Using AI Code Generation

copy

Full Screen

1import com.galenframework.speclang2.pagespec.PageSpecHandler2import com.galenframework.speclang2.pagespec.reader.PageSpecReader3import com.galenframework.speclang2.pagespec.reader.PagespecReaderContext4import com.galenframework.parser.SyntaxException5import com.galenframework.specs.page.Locator6import java.util.regex.Matcher7import java.util.regex.Pattern8import static com.galenframework.parser.Expectations.list9import static com.galenframework.parser.Expectations.text10import static com.galenframework.parser.Expectations.variable11PagespecReaderContext context = new PagespecReaderContext()12while ((line = reader.readLine()) != null) {13 lines.add(line)14}15String content = lines.join("\n")16String[] lines = content.split("\n")17PageSpecHandler handler = new PageSpecHandler()18handler.setGlobalVariables(context, lines)19context.getGlobalVariables()20import com.galenframework.speclang2.pagespec.PageSpecHandler21import com.galenframework.speclang2.pagespec.reader.PageSpecReader22import com.galenframework.speclang2.pagespec.reader.PagespecReaderContext23import com.galenframework.parser.SyntaxException24import com.galenframework.specs.page.Locator25import java.util.regex.Matcher26import java.util.regex.Pattern27import static com.galenframework.parser.Expectations.list28import static com.galenframework.parser.Expectations.text29import static com.galenframework.parser.Expectations.variable30PagespecReaderContext context = new PagespecReaderContext()31PageSpecHandler handler = new PageSpecHandler()32handler.read(context, reader)33context.getSpecs()34import com.galenframework.speclang2.pagespec.PageSpecHandler35import com.galenframework.speclang2.pagespec.reader.PageSpecReader36import com.galenframework.speclang2.pagespec.reader.PagespecReaderContext37import com.galenframework.parser.SyntaxException38import com.galenframework.specs.page.Locator39import java.util.regex.Matcher40import java.util.regex.Pattern41import static com.galenframework.parser.Expectations.list42import static com.galenframework.parser.Expect

Full Screen

Full Screen

setGlobalVariables

Using AI Code Generation

copy

Full Screen

1def globalVariables = new HashMap<String, Object>()2globalVariables.put("globalVariable1", "value1")3globalVariables.put("globalVariable2", "value2")4globalVariables.put("globalVariable3", "value3")5com.galenframework.speclang2.pagespec.PageSpecHandler.setGlobalVariables(globalVariables)6def globalVariables = new HashMap<String, Object>()7globalVariables.put("globalVariable1", "value1")8globalVariables.put("globalVariable2", "value2")9globalVariables.put("globalVariable3", "value3")10com.galenframework.speclang2.pagespec.PageSpecHandler.setGlobalVariables(globalVariables)11def globalVariables = new HashMap<String, Object>()12globalVariables.put("globalVariable1", "value1")13globalVariables.put("globalVariable2", "value2")14globalVariables.put("globalVariable3", "value3")15com.galenframework.speclang2.pagespec.PageSpecHandler.setGlobalVariables(globalVariables)16def globalVariables = new HashMap<String, Object>()17globalVariables.put("globalVariable1", "value1")

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful