Search in sources :

Example 26 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class FieldDetectorConnector method detect.

/**
 * Send image to image field detector and retrieve the box and text
 * @param imageFile
 * @param resizeFactor
 * @param urlPath
 * @return
 */
private JSONObject detect(File imageFile, double resizeFactor, String urlPath) {
    if (imageFile == null) {
        throw new ScenarioException("Image file is null");
    }
    if (!imageFile.exists()) {
        throw new ScenarioException(String.format("Image file %s not found", imageFile.getAbsolutePath()));
    }
    HttpResponse<JsonNode> fieldDefinition = Unirest.post(url + urlPath).field("resize", resizeFactor).field("image", imageFile).asJson();
    JSONObject bodyObject = fieldDefinition.getBody().getObject();
    if (fieldDefinition.getStatus() != 200) {
        try {
            throw new ScenarioException("Field detector returned error: " + bodyObject.get("error"));
        } catch (NullPointerException e) {
            throw new ScenarioException("Field detector returned error");
        }
    }
    List<String> imageNames = new ArrayList<>(Arrays.asList(JSONObject.getNames(bodyObject)));
    imageNames.remove("error");
    if (imageNames.isEmpty()) {
        throw new ScenarioException("Field detector did not return any information: " + bodyObject.get("error"));
    }
    return bodyObject.getJSONObject(imageNames.get(0));
}
Also used : JSONObject(kong.unirest.json.JSONObject) ArrayList(java.util.ArrayList) JsonNode(kong.unirest.JsonNode) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 27 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class Campaign method getAll.

/**
 * Get list of all campaigns
 * @return
 */
@SuppressWarnings("unchecked")
public static List<Campaign> getAll() {
    try {
        JSONObject json = getPagedJSonResponse(buildGetRequest(apiRootUrl + CAMPAIGNS_URL));
        List<Campaign> campaigns = new ArrayList<>();
        if (json.has(FIELD_EMBEDDED)) {
            for (JSONObject folderJson : (List<JSONObject>) json.getJSONObject(FIELD_EMBEDDED).getJSONArray(FIELD_CAMPAIGNS).toList()) {
                campaigns.add(Campaign.fromJson(folderJson));
            }
        }
        return campaigns;
    } catch (UnirestException e) {
        throw new ScenarioException("Cannot get all campaigns", e);
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) ArrayList(java.util.ArrayList) UnirestException(kong.unirest.UnirestException) List(java.util.List) ArrayList(java.util.ArrayList) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 28 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class Campaign method create.

public static Campaign create(Project project, String campaignName, CampaignFolder parentFolder) {
    try {
        JSONObject body = new JSONObject();
        body.put(FIELD_TYPE, TYPE_CAMPAIGN);
        body.put(FIELD_NAME, campaignName);
        body.put("status", "PLANNED");
        JSONObject parent = new JSONObject();
        if (parentFolder == null) {
            parent.put(FIELD_ID, project.id);
            parent.put(FIELD_TYPE, "project");
        } else {
            parent.put(FIELD_ID, parentFolder.id);
            parent.put(FIELD_TYPE, "campaign-folder");
        }
        body.put("parent", parent);
        JSONObject json = getJSonResponse(buildPostRequest(apiRootUrl + CAMPAIGNS_URL).body(body));
        return fromJson(json);
    } catch (UnirestException e) {
        throw new ScenarioException(String.format("Cannot create campaign %s", campaignName), e);
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) UnirestException(kong.unirest.UnirestException) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 29 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class Campaign method getIterations.

/**
 * get iterations for the current campaign
 */
@SuppressWarnings("unchecked")
public List<Iteration> getIterations() {
    try {
        JSONObject json = getPagedJSonResponse(buildGetRequest(url + String.format(ITERATIONS_URL, id)));
        List<Iteration> iterations = new ArrayList<>();
        if (json.has(FIELD_EMBEDDED)) {
            for (JSONObject iterationJson : (List<JSONObject>) json.getJSONObject(FIELD_EMBEDDED).getJSONArray("iterations").toList()) {
                iterations.add(Iteration.fromJson(iterationJson));
            }
        }
        return iterations;
    } catch (UnirestException e) {
        throw new ScenarioException(String.format("Cannot get list of iterations for campaign %s", name));
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) ArrayList(java.util.ArrayList) UnirestException(kong.unirest.UnirestException) List(java.util.List) ArrayList(java.util.ArrayList) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 30 with ScenarioException

use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.

the class CampaignFolder method fromJson.

public static CampaignFolder fromJson(JSONObject json) {
    try {
        Entity parent;
        if (json.has(FIELD_PARENT)) {
            if (TYPE_PROJECT.equals(json.getJSONObject(FIELD_PARENT).getString(FIELD_TYPE))) {
                parent = Project.fromJson(json.getJSONObject(FIELD_PARENT));
            } else if (TYPE_CAMPAIGN_FOLDER.equals(json.getJSONObject(FIELD_PARENT).getString(FIELD_TYPE))) {
                parent = CampaignFolder.fromJson(json.getJSONObject(FIELD_PARENT));
            } else {
                parent = null;
            }
        } else {
            parent = null;
        }
        Project project = null;
        if (json.has(TYPE_PROJECT)) {
            project = Project.fromJson(json.getJSONObject(TYPE_PROJECT));
        }
        return new CampaignFolder(json.getJSONObject("_links").getJSONObject("self").getString("href"), json.getInt(FIELD_ID), json.getString(FIELD_NAME), project, parent);
    } catch (JSONException e) {
        throw new ScenarioException(String.format("Cannot create CampaignFolder from JSON [%s] data: %s", json.toString(), e.getMessage()));
    }
}
Also used : JSONException(kong.unirest.json.JSONException) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Aggregations

ScenarioException (com.seleniumtests.customexception.ScenarioException)68 ArrayList (java.util.ArrayList)17 WebElement (org.openqa.selenium.WebElement)14 UnirestException (kong.unirest.UnirestException)13 GenericPictureElement (com.seleniumtests.uipage.htmlelements.GenericPictureElement)12 IOException (java.io.IOException)12 JSONObject (kong.unirest.json.JSONObject)12 CheckBoxElement (com.seleniumtests.uipage.htmlelements.CheckBoxElement)11 Element (com.seleniumtests.uipage.htmlelements.Element)11 HtmlElement (com.seleniumtests.uipage.htmlelements.HtmlElement)11 LinkElement (com.seleniumtests.uipage.htmlelements.LinkElement)11 RadioButtonElement (com.seleniumtests.uipage.htmlelements.RadioButtonElement)11 File (java.io.File)10 List (java.util.List)9 HashMap (java.util.HashMap)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)7 AWTException (java.awt.AWTException)7 Robot (java.awt.Robot)6 Map (java.util.Map)5 TestStep (com.seleniumtests.reporter.logger.TestStep)4