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

com.seleniumtests.connectors.selenium.fielddetector.Label Maven / Gradle / Ivy

There is a newer version: 5.1.15
Show newest version
package com.seleniumtests.connectors.selenium.fielddetector;

import java.awt.Rectangle;
import java.util.List;
import java.util.stream.Collectors;

import com.seleniumtests.core.testanalysis.ErrorCause;

import kong.unirest.json.JSONObject;
import net.ricecode.similarity.JaroWinklerStrategy;
import net.ricecode.similarity.SimilarityStrategy;
import net.ricecode.similarity.StringSimilarityService;
import net.ricecode.similarity.StringSimilarityServiceImpl;

public class Label {

	private String text;
	private int top;
	private int bottom;
	private int left;
	private int right;
	private int width;
	private int height;
	
	private SimilarityStrategy strategy = new JaroWinklerStrategy();
	private StringSimilarityService stringService = new StringSimilarityServiceImpl(strategy);
	
	public Label() {}
	
	// for test purpose
	public Label(int left, int right, int top, int bottom, String text) {
		this.left = left;
		this.right = right;
		this.top = top;
		this.bottom = bottom;
		this.text = text;
		this.width = right - left;
		this.height = bottom - top;
	}
	
	
	public String getText() {
		return text;
	}

	public int getTop() {
		return top;
	}

	public int getBottom() {
		return bottom;
	}

	public int getLeft() {
		return left;
	}

	public int getRight() {
		return right;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public Rectangle getRectangle() {
		return new Rectangle(left, top, width, height);
	}

	public JSONObject toJson() {
		JSONObject json = new JSONObject(this);
		json.remove("strategy");
		json.remove("stringService");
		return json;
	}
	
	public static Label fromJson(JSONObject json) {
		Label label = new Label();
		
		label.top = json.getInt("top");
		label.bottom = json.getInt("bottom");
		label.left = json.getInt("left");
		label.right = json.getInt("right");
		label.width = json.getInt("width");
		label.height = json.getInt("height");
		label.text = json.optString("text", null);
		
		return label;
	}
	
	/**
	 * From a JSONObject get from seleniumServer, returns the list of Labels
	 * 
	 * {
	"fields": [
		{
			"class_id": 4,
			"top": 142,
			"bottom": 166,
			"left": 174,
			"right": 210,
			"class_name": "field_with_label",
			"text": null,
			"related_field": {
				"class_id": 0,
				"top": 142,
				"bottom": 165,
				"left": 175,
				"right": 211,
				"class_name": "field",
				"text": null,
				"related_field": null,
				"with_label": false,
				"width": 36,
				"height": 23
			},
			"with_label": true,
			"width": 36,
			"height": 24
		},

	],
	"labels": [
		{
			"top": 3,
			"left": 16,
			"width": 72,
			"height": 16,
			"text": "Join Us",
			"right": 88,
			"bottom": 19
		},

	]
	"error": null,
	"version": "afcc45"
}
	 * 
	 * @param detectionData
	 * @return
	 */
	public static List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy