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

com.ats.script.actions.ActionText Maven / Gradle / Ivy

The newest version!
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */

package com.ats.script.actions;

import java.util.ArrayList;
import java.util.function.Predicate;

import com.ats.element.SearchedElement;
import com.ats.executor.ActionStatus;
import com.ats.executor.ActionTestScript;
import com.ats.executor.SendTextException;
import com.ats.generator.variables.CalculatedValue;
import com.ats.script.Script;
import com.ats.script.ScriptLoader;
import com.ats.tools.Utils;
import com.google.gson.JsonObject;

public class ActionText extends ActionExecuteElement {

	public static final String SCRIPT_LABEL = "keyboard";
	public static final Predicate PREDICATE = g -> SCRIPT_LABEL.equals(g);
	
	public static final String WAIT_CHAR = "waitchar";

	private CalculatedValue text;

	private int waitChar = 0;

	public ActionText() {}

	public ActionText(ScriptLoader script, int stopPolicy, ArrayList options, String text, ArrayList objectArray) {
		super(script, stopPolicy, options, objectArray);
		setText(new CalculatedValue(script, text));

		for (String data : options) {
			if(data.contains(WAIT_CHAR)){
				setWaitChar(Utils.string2Int(data.replace(WAIT_CHAR, "").replace("=", "")));
				break;
			}
		}
	}

	public ActionText(Script script, int stopPolicy, int maxTry, int delay, SearchedElement element, CalculatedValue text) {
		super(script, stopPolicy, maxTry, delay, element);
		setText(text);
	}

	public ActionText(Script script, int stopPolicy, int maxTry, int delay, SearchedElement element, CalculatedValue text, int wait) {
		super(script, stopPolicy, maxTry, delay, element);
		setText(text);
		setWaitChar(wait);
	}

	//---------------------------------------------------------------------------------------------------------------------------------
	// Code Generator
	//---------------------------------------------------------------------------------------------------------------------------------

	@Override
	public StringBuilder getJavaCode() {
		if(waitChar > 0) {
			return super.getJavaCode().append(", ").append(text.getJavaCode()).append(", ").append(waitChar).append(")");
		}else {
			return super.getJavaCode().append(", ").append(text.getJavaCode()).append(")");
		}
	}

	@Override
	public ArrayList getKeywords() {
		ArrayList keywords = super.getKeywords();
		keywords.add(text.getKeywords());
		return keywords;
	}

	//---------------------------------------------------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------------------------------------------------

	@Override
	public String execute(ActionTestScript ts, String testName, int testLine, int tryNum) {

		int maxTry = 0;
		while(maxTry < 3) {
			try {
				super.execute(ts, testName, testLine, tryNum + maxTry);
				return null;
			}catch(SendTextException e) {}
			maxTry++;
		}

		status.setError(ActionStatus.ENTER_TEXT_FAIL, "send keys action fail on the element");
		terminateExecution(ts, ActionStatus.ENTER_TEXT_FAIL);
		
		return null;
	}

	@Override
	public void terminateExecution(ActionTestScript ts) {
		super.terminateExecution(ts);
		if(status.isPassed()) {

			String safeDataValue = getTestElement().enterText(status, text, ts, waitChar);
			status.endAction();

			if (text.isCrypted()) {
				safeDataValue = CalculatedValue.CRYPTED_DATA;
			}
			
			ts.getRecorder().updateTextScreen(status, safeDataValue);
			
		}else {
			
			String safeDataValue = text.getCalculated();
			
			if (text.isCrypted()) {
				safeDataValue = CalculatedValue.CRYPTED_DATA;
			}
			
			ts.getRecorder().update(status.getCode(), status.getDuration(), safeDataValue);
		}

		ts.getRecorder().updateScreen(false);
	}

	@Override
	public StringBuilder getActionLogs(String scriptName, int scriptLine, JsonObject data) {
		data.addProperty("text", text.getCalculated().replaceAll("\"", "\\\""));
		return super.getActionLogs(scriptName, scriptLine, data);
	}

	//--------------------------------------------------------
	// getters and setters for serialization
	//--------------------------------------------------------

	public CalculatedValue getText() {
		return text;
	}

	public void setText(CalculatedValue text) {
		this.text = text;
	}

	public int getWaitChar() {
		return waitChar;
	}

	public void setWaitChar(int value) {
		if(value > 5000) {
			value = 5000;
		}
		this.waitChar = value;
	}

	public static StringBuilder getAtsCodeStr(String subFolder) {
		return new StringBuilder().append("callscript -> ").append(subFolder).append(".EnterText");
		// return new StringBuilder().append(SCRIPT_LABEL).append(" -> ");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy