All Downloads are FREE. Search and download functionalities are using the official Maven repository.

automation-core.3.0.1.source-code.interpreter.js Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
var interpreter = {
	exclusive_gateway: {
		is_input_valid: {
			yes: function() {
				return message.isInputValid
			},
			no: function() {
				
			}
		},
		is_steep_mocked: {
			yes: function() {
				return currentSteep.mocked && null == currentSteep.anImport && currentSteep.rules.length == 1
			},
			no: function() {
				
			}
		},
		for_each_rule: {
			next: function() {
				return ruleIndex < currentSteep.rules.length
			},
			finish: function() {
				
			}
		},
		has_argument: {
			yes: function() {
				return currentSteep.hasArgument && ruleIndex == currentSteep.rules.length -1 || Array.isArray(currentSteep.rules[ruleIndex])
			},
			no: function() {
				
			}
		},
		browser_action: {
			open: function() {
				return currentRule.type.toUpperCase() == "OPEN"
			},
			click: function() {
				return currentRule.type.toUpperCase() == "CLICK"
			},
			validate_url: function() {
				return currentRule.type.toUpperCase() == "VALIDATE_URL"
			},
			find_text: function() {
				return currentRule.type.toUpperCase() == "FIND_TEXT"
			},
			find_text_in_attribute: function() {
				return currentRule.type.toUpperCase() == "FIND_TEXT_IN_ATTRIBUTE"
			},
			count_elements: function() {
				return currentRule.type.toUpperCase() == "COUNT_ELEMENTS"
			},
			input: function() {
				return currentRule.type.toUpperCase() == "INPUT"
			},
			key_press_delete: function() {
				return currentRule.type.toUpperCase() == "KEY_PRESS_DELETE"
			},
			key_press_enter: function() {
				return currentRule.type.toUpperCase() == "KEY_PRESS_ENTER"
			},
			scroll_to: function() {
				return currentRule.type.toUpperCase() == "SCROLL_TO"
			},
			quit: function() {
				return currentRule.type.toUpperCase() == "QUIT"
			},
			script: function() {
				return currentRule.type.toUpperCase() == "SCRIPT"
			},
			api_host: function() {
				return currentRule.type.toUpperCase() == "API_HOST"
			},
			api_port: function() {
				return currentRule.type.toUpperCase() == "API_PORT"
			},
			api_endpoint: function() {
				return currentRule.type.toUpperCase() == "API_ENDPOINT"
			},
			api_ssl: function() {
				return currentRule.type.toUpperCase() == "API_SSL"
			},
			api_load_payload: function() {
				return currentRule.type.toUpperCase() == "API_LOAD_PAYLOAD"
			},
			api_headers: function() {
				return currentRule.type.toUpperCase() == "API_HEADERS"
			},
			api_method: function() {
				return currentRule.type.toUpperCase() == "API_METHOD"
			},
			api_call: function() {
				return currentRule.type.toUpperCase() == "API_CALL"
			},
			api_validate_status_code: function() {
				return currentRule.type.toUpperCase() == "API_VALIDATE_STATUS_CODE"
			},
			api_validate_body: function() {
				return currentRule.type.toUpperCase() == "API_VALIDATE_BODY"
			}
		},
		has_imported_feattures: {
			yes: function() {
				return null != currentSteep.anImport
			},
			no: function() {
				
			}
		},
		is_rule_mocked: {
			no: function() {
				
			},
			yes: function() {
				return currentSteep.mocked && ruleIndex == currentSteep.rules.length -1
			}
		},
		has_custom_implementation: {
			no: function() {
				
			},
			yes: function() {
				return handler != null
			}
		}
	},
	business_tasks: {},
	script_tasks: {
		load_steep_descriptor: function() {
			if(null == scenario) {
			    util.finishStage(stage, "Cucumber stage object is null", "ERROR_CODE", 500);
			}else{
			    var fileUrl = "src/test/resources/build/features/" + browser.name + "/" + scenario.id.match(/\/([^\/]+)\.feature/)[1] + ".json";
			    info.log("Configuration file => {}", fileUrl);
			    fileUtils.readFileToString(
			        fileUrl,
			        function(data){
			            
			            var currentFeature = JSON.parse(data);
			            
			            var currentScenario = currentFeature.scenarios.filter(function(es){
			                return es.name == scenario.name;
			            })[0];
			            
			            var currentSteep = currentScenario.steeps.filter(function(st){
			                return st.name == message.steepName;
			            })[0];
			            
			            util.addGlobalVariable("currentFeature", currentFeature);
			            util.addGlobalVariable("currentScenario", currentScenario);
			            util.addGlobalVariable("currentSteep", currentSteep);
			            util.addGlobalVariable("ruleIndex", 0);
			            info.log("currentSteep => {}", JSON.stringify(currentSteep));
			            
			            stage.complete(); 
			        },
			        function(err){
			            util.finishStage(stage, "Error while ready configuration json file", "BDD_9000", 500);
			        }
			    );
			}
		},
		input_not_valid: function() {
			util.finishStage(stage, "Input is not valid", "ERROR_CODE", 500);
		},
		load_rules: function() {
			var fileUrl = "src/test/resources/features/rules.json";
			fileUtils.readFileToString(
			    fileUrl,
			    function(data){
			        var config = JSON.parse(data);
			        var rules = [];
			        
			        for(var key in config.rules){
			            
			            config.rules[key].forEach(function(r){
			                rules.push(r);
			            });
			            
			        }
			        
			        var environmentVariables = {};
			        
			        if(message.environment != "" && typeof config.env != "undefined" && typeof config.env[message.environment] != "undefined"){
			            environmentVariables = config.env[message.environment];
			        }
			        
			        util.addGlobalVariable("environmentVariables", environmentVariables);
			        util.addGlobalVariable("rules", rules);
			        info.log("Rules for =>{}", browser.name);
			        stage.complete(); 
			    },
			    function(err){
			        util.finishStage(stage, "Error while ready configuration json file", "BDD_9000", 500);
			    }
			);
		},
		get_rule: function() {
			var r = currentSteep.rules[ruleIndex];
			
			var filterRules = rules.filter(function(rr){
			    if(Array.isArray(r)){
			        return rr.name == r[0]
			    }else{
			        return rr.name == r
			    }
			});
			
			
			if(filterRules.length > 0){
			    var currentRule = filterRules[0];
			    addEvironmentValue(currentRule);
			    util.addGlobalVariable("currentRule", currentRule);
			    if(typeof currentRule.sleep != "undefined" && currentRule.sleep != null){
			        seleniumDriver.waitFor(currentRule.sleep, function(f){
			            stage.complete();
			        }, function(err){
			            util.finishStage(stage, err, "BDD_9000", 500);
			        })
			    }else{
			        stage.complete();
			    }
			    
			} else {
			  util.finishStage(stage, "Unable to find the rule in the rules.json file", "BDD_9001", 500);  
			}
		},
		set_argument: function() {
			if(Array.isArray(currentSteep.rules[ruleIndex]) && currentSteep.rules[ruleIndex].length > 1){
			    currentRule.value = currentSteep.rules[ruleIndex][1];
			}else if(currentSteep.hasArgument){
			    currentRule.value = message.argument;   
			}
			addEvironmentValue(currentRule);
			stage.complete();
		},
		open_action: function() {
			info.log("Opening for => {}", browser.name);
			open(currentRule, function(){
			    info.log("Browser opened successfully!!");
			    stage.complete()
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		load_webdriver_functions: function() {
			try{
			    
			    info.log("ENTROOOOOOOO");
			    var findElement = function(rule, isMultiple, onSuccess, onError){
			        var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			        var webDriver = browser.getDriver();
			        var sleepTime = null != rule.sleep ? rule.sleep : 0;
			    
			        var by;
			        
			        if (null != rule.attrId) {
			            seleniumDriver.findElementById(webDriver, sleepTime, rule.attrId, isMultiple, function(el){onSuccess(el)}, function(err){onError(err)});
			        } else if (null != rule.attrName) {
			            seleniumDriver.findElementByName(webDriver, sleepTime, rule.attrName, isMultiple, function(el){onSuccess(el)}, function(err){onError(err)});
			        } else if (null != rule.className) {
			          seleniumDriver.findElementByClassName(webDriver, sleepTime, rule.className, isMultiple, function(el){onSuccess(el)}, function(err){onError(err)});
			        } else if (null != rule.xpath) {
			          seleniumDriver.findElementByXpath(webDriver, sleepTime, rule.xpath, isMultiple, function(el){onSuccess(el)}, function(err){onError(err)});
			        } else if (null != rule.tagName) {
			          seleniumDriver.findElementByTagName(webDriver, sleepTime, rule.tagName, isMultiple, function(el){onSuccess(el)}, function(err){onError(err)});
			        } else {
			          seleniumDriver.findElementByText(webDriver, sleepTime, rule.attrText, isMultiple, function(el){onSuccess(el)}, function(err){onError(err)});
			        }
			    };    
			    
			    var open = function(rule, onSuccess, onError){
			        try{
			            
			            function goTo(webDriver){
			                var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			                info.log("opening.. => {}", JSON.stringify(rule));
			                webDriver.manage().window().maximize();
			                browser.setDriver(webDriver);
			                var url = rule.value.replaceAll("\\\\", "");
			                webDriver.get(url);
			                error.log("process success");
			                onSuccess();
			                
			            }
			            
			            if(null != browser.getDriver()){
			                info.log("reusing driver...");
			                goTo(browser.getDriver());
			            }else{
			                
			                seleniumDriver.createLocalWebDriver(browser.name, currentScenario.locale,
			                    goTo,
			                    function(err){
			                        error.log("selenium exception => {}", err);
			                        onError(err);
			                    }
			                );
			                
			            }
			            
			        } catch(e){
			            error.log("catch exception => {}", e);
			            onError(e);
			        }
			    };
			    
			    var addEvironmentValue = function(rule){
			        
			        if(message.environment != "" && null != rule.value && /{{(.*)}}/.test(rule.value)) {
			            var newVal = environmentVariables[rule.value.match(/{{(.*)}}/)[1]];
			            if(typeof newVal != "undefined" && null != newVal){
			                rule.value = rule.value.replaceAll("\\{\\{(.*)\\}\\}", newVal) ;
			            }
			        }
			        
			    }
			    
			    util.addGlobalVariable("addEvironmentValue", addEvironmentValue);
			    util.addGlobalVariable("open", open);
			    util.addGlobalVariable("findElement", findElement);
			    
			    stage.complete();
			    
			} catch(e){
			    
			    util.finishStage(stage, "Error preparing functions", "ERROR_CODE", 500);
			    
			}
		},
		take_screenshot: function() {
			ruleIndex++;
			util.addGlobalVariable("ruleIndex", ruleIndex);
			if(currentRule.screenShot){
			    var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			    var webDriver = browser.getDriver();
			    seleniumDriver.takeScreenShot(scenario, webDriver, function(f) {
			        stage.complete();
			    }, function(err) {
			        util.finishStage(stage, "Error while taking screenshot => " + err, "BDD_9003", 500);
			    });   
			}else{
			    stage.complete();
			}
		},
		click_action: function() {
			findElement(currentRule, false, function(el){
			    el.click();
			    stage.complete()
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		validate_location: function() {
			var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			var webDriver = browser.getDriver();
			if(currentRule.value == webDriver.getCurrentUrl()){
			    stage.complete();
			}else{
			    util.finishStage(stage, "Assertion error \""+ webDriver.getCurrentUrl() +"\" is not equals to \""+ currentRule.value +"\"", "BDD_9000", 500);
			}
		},
		load_imported_rules: function() {
			fileUtils.getFileList("src/test/resources/build/features/" + browser.name, ["json"],
			    function(arr){
			        var impFeatures = new Array();
			        arr.forEach(function(file){
			            fileUtils.readFileToString(file, function(data){
			                impFeatures.push(JSON.parse(data));
			            }, function(err){
			                util.finishStage(stage, "Unexpected error while reading the features => " + err, "BDD_9000", 500);
			            });
			        });
			        util.addGlobalVariable("impFeatures", impFeatures);
			        stage.complete();
			    },
			    function(err){
			        util.finishStage(stage, "Unexpected error while loading the imported features => " + err, "BDD_9000", 500);
			    }
			);
		},
		process_features: function() {
			try{
			    info.log("total of imported features => {}", impFeatures.length);
			    
			    var importedRules = new Array();
			    
			    function addRules(importedScenario, importObj){
			        
			        var n = 0;
			        
			        importedScenario.steeps.forEach(function(st){
			            
			            if(!st.mocked){
			                
			                if(st.anImport != null) findRule(st.anImport);
			            
			                st.rules.forEach(function(rule){
			                    importedRules.push(rule);
			                    if(importObj.arguments != null && importObj.arguments.length > 0 ){
			                        var rrArr = rules.filter(function(r){
			                            var nme = rule
			                            if(Array.isArray(nme)) nme = nme[0];
			                            return r.name == nme;
			                        });
			                        if(rrArr.length > 0){
			                            info.log("Adding argument => {}", importObj.arguments[n]);
			                            rrArr[0].value = importObj.arguments[n];    
			                        }
			                    }
			                });
			    
			                n++;
			                
			            }
			            
			            
			        });
			    }
			    
			    function findRule(importObj){
			        var ftArr = impFeatures.filter(function(e){
			            return e.name == importObj.featureName;
			        });
			        if(ftArr.length > 0){
			            var ft = ftArr[0];
			            var scArr = ft.scenarios.filter(function(sc){
			                return sc.name == importObj.scenarioName;
			            });
			            if(scArr.length > 0 ){
			                addRules(scArr[0], importObj)
			            }
			        }
			    }
			    
			    findRule(currentSteep.anImport);
			    
			    
			    currentSteep.rules.forEach(function(r){
			        importedRules.push(r);
			    });
			    
			    currentSteep.rules = importedRules;
			    
			    util.addGlobalVariable("rules", rules);
			    util.addGlobalVariable("currentSteep", currentSteep);
			    
			    info.log("Total of rules to be processed => {}", JSON.stringify(currentSteep.rules));
			    
			    stage.complete();
			}catch(e){
			    util.finishStage(stage, "Unexpected error while processing imported rules" + e, "BDD_9000", 500);
			}
		},
		wait_for_sometime: function() {
			info.log("Wait for => {}", browser.name);
			seleniumDriver.waitFor(500, function(f){
			    stage.complete();
			}, function(err){
			    util.finishStage(stage, err, "BDD_9000", 500);
			})
		},
		get_html_text: function() {
			findElement(currentRule,false, function(el){
			    var htmlText = el.getText();
			    if(new RegExp(currentRule.value).test(htmlText)){
			        stage.complete();
			    }else{
			        util.finishStage(stage, "Assertion error \""+ htmlText +"\" is not matching to \""+ currentRule.value +"\"", "BDD_9000", 500);
			    }
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		get_attribute_value: function() {
			findElement(currentRule, false, function(el){
			    
			    var attrText = el.getAttribute(currentRule.attrName);
			    if(new RegExp(currentRule.value).test(attrText)){
			        stage.complete();
			    }else{
			        util.finishStage(stage, "Assertion error \""+ attrText +"\" is not matching to \""+ currentRule.value +"\"", "BDD_9000", 500);
			    }    
			    
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		count_all_elements: function() {
			findElement(currentRule, true, function(arr){
			    if(new RegExp(currentRule.value).test(arr.length)){
			        stage.complete();
			    }else{
			        util.finishStage(stage, "Assertion error \""+ arr.length +"\" is not matching to \""+ currentRule.value +"\"", "BDD_9000", 500);
			    }
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		input_text: function() {
			try{
			    findElement(currentRule, false, function(el){
			        el.sendKeys(currentRule.value);
			        stage.complete();
			    }, function(e){
			        util.finishStage(stage, e, "BDD_9002", 500);
			    });   
			}catch(e){
			    util.finishStage(stage, "Error while sending keys", "ERROR_CODE", 500);
			}
		},
		press_delete: function() {
			findElement(currentRule, false, function(el){
			    el.sendKeys(seleniumDriver.DELETE);
			    stage.complete();
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		press_enter: function() {
			findElement(currentRule, false, function(el){
			    el.sendKeys(seleniumDriver.ENTER);
			    stage.complete();
			}, function(e){
			    util.finishStage(stage, e, "BDD_9002", 500);
			});
		},
		scroll_to: function() {
			try{
			    var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			    var webDriver = browser.getDriver();
			    webDriver.executeScript("window.scrollBy(0," + currentRule.value + ")");
			    stage.complete();    
			}catch(e){
			    util.finishStage(stage, "Error while scrolling => "+ e, "BDD_9000", 500);
			}
		},
		custom_handler: function() {
			var webDriver = browser.getDrivers().get(scenario.name);
			handler(webDriver, currentRule, function(f){
			    if(f){
			        stage.complete();
			    }else{
			        util.finishStage(stage, "The custom implementation has returned an error signal", "BDD_9000", 500);
			    }
			}, function(err){
			    util.finishStage(stage, "Error while calling cutom implementation => " + e, "BDD_9000", 500);
			});
		},
		quit_driver: function() {
			try{
			    var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			    var webDriver = browser.getDriver();
			    webDriver.quit();
			    stage.complete();    
			}catch(e){
			    util.finishStage(stage, "Error while closing the session => "+ e, "BDD_9000", 500);
			}
		},
		run_script: function() {
			try{
			    var featureName = scenario.id.match(/.*:(.*):.*/)[1];
			    var webDriver = browser.getDriver();
			    webDriver.executeScript(currentRule.value);
			    stage.complete();    
			}catch(e){
			    util.finishStage(stage, "Error while scrolling => "+ e, "BDD_9000", 500);
			}
		},
		api_host: function() {
			httpTransaction.host = currentRule.value;
			stage.complete();
		},
		api_port: function() {
			httpTransaction.port = Number(currentRule.value);
			stage.complete();
		},
		add_endpoint: function() {
			httpTransaction.endPoint = currentRule.value;
			stage.complete();
		},
		set_ssl: function() {
			httpTransaction.ssl = currentRule.value;
			stage.complete();
		},
		load_payload: function() {
			var fileUrl = "src/test/resources/" + currentRule.value;
			fileUtils.readFileToString(
			    fileUrl,
			    function(data){
			        httpTransaction.payload = data;
			        info.log(data);
			        stage.complete(); 
			    },
			    function(err){
			        util.finishStage(stage, "Error while ready configuration json file", "BDD_9000", 500);
			    }
			);
		},
		add_headers: function() {
			if(dataTable != null){
			    var headers= {};
			    dataTable.forEach(function(v){
			        headers[v[0]] = v[1];
			    });
			    headers = JSON.stringify(headers);
			    info.log(headers);
			    httpTransaction.headers = headers;
			}
			stage.complete();
		},
		define_http_method: function() {
			httpTransaction.httpMethod = currentRule.value;
			stage.complete();
		},
		call_api: function() {
			info.log(httpTransaction.toString());
			    
			    var wc = webclient.build(httpTransaction.port, httpTransaction.host, 
			                httpTransaction.endPoint)
			                .httpMethod(httpTransaction.httpMethod)
			                .setSSL(httpTransaction.ssl);
			    
			    if(httpTransaction.headers != null){
			        
			        var headers = JSON.parse(httpTransaction.headers);
			        var key = "";
			        for(key in headers){
			            wc.addHeader(key, headers[key]);
			        }
			        
			    }
			    
			    var payload = "{}";
			    
			    if(httpTransaction.payload != null){
			    
			        payload = httpTransaction.payload;
			        
			    }
			     
			    wc.sendJsonObjectAndGetStatusCode(payload, function(httpCode, jsonResponse){
			        httpTransaction.statusCode = httpCode;
			        httpTransaction.response = jsonResponse;
			        info.log("API Response code => {}",httpCode)
			        info.log("API Response body => {}",jsonResponse)
			        stage.complete();
			    }, function(err){
			        stage.fail(err);
			    });
		},
		validate_http_code: function() {
			if(httpTransaction.statusCode == Number(currentRule.value)){
			    stage.complete();
			}else{
			    util.finishStage(stage, "Assertion error \""+ httpTransaction.statusCode +"\" is not matching to \""+ currentRule.value +"\"", "BDD_9000", 500);
			}
		},
		validate_response_body: function() {
			if(dataTable != null){
			    
			    var errors = "";
			    dataTable.forEach(function(v){
			        
			        httpTransaction.validateField(v[0], v[1],
			            function(ass){
			               if (!ass.isStatus()) {
			                    errors += ass.getMessage() + "\n";
			                } 
			            },
			            function(err){
			                util.finishStage(stage, err, "BDD_9000", 500);
			            }
			        );
			    });
			    
			    if(errors.length > 0){
			        util.finishStage(stage, errors, "BDD_9000", 500);
			    }else{
			        stage.complete();    
			    }
			}else{
			    stage.complete();
			}
		}
	},
	service_tasks: null
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy