Last active
April 6, 2019 16:09
-
-
Save sauceaaron/ba064729c742cb5a711653f2515552a6 to your computer and use it in GitHub Desktop.
SauceExecutor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.saucelabs.common; | |
| import com.google.gson.Gson; | |
| import com.sun.deploy.util.StringUtils; | |
| import org.openqa.selenium.remote.RemoteWebDriver; | |
| import java.util.Arrays; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| /** | |
| * Call Sauce Labs Javascript Executor | |
| */ | |
| public class SauceExecutor | |
| { | |
| public static String BREAK = "sauce: break"; | |
| public static String CONTEXT = "sauce:context="; | |
| public static String JOB_BUILD = "sauce:job-build="; | |
| public static String JOB_INFO = "sauce: job-info="; | |
| public static String JOB_NAME = "sauce:job-name="; | |
| public static String JOB_RESULT = "sauce:job-result="; | |
| public static String JOB_TAGS = "sauce:job-tags="; | |
| public static String STOP_NETWORK = "sauce: stop network"; | |
| public static String START_NETWORK = "sauce: start network"; | |
| public enum STATUS { passed, failed } | |
| RemoteWebDriver driver; | |
| String sessionId; | |
| public SauceExecutor(RemoteWebDriver driver) | |
| { | |
| this.driver = driver; | |
| this.sessionId = driver.getSessionId().toString(); | |
| } | |
| public void execute(String... script) | |
| { | |
| StringBuilder sanitizedScript = new StringBuilder(); | |
| for (String s : script) | |
| { | |
| // TODO: check to make sure only valid commands and values | |
| sanitizedScript.append(s); | |
| } | |
| log("executing script: " + sanitizedScript); | |
| driver.executeScript(sanitizedScript.toString()); | |
| } | |
| /** update job **/ | |
| public void updateJobResult(STATUS status) { | |
| execute(JOB_RESULT, status.toString()); | |
| } | |
| public void updateJobResult(String status) | |
| { | |
| updateJobResult(STATUS.valueOf(status)); | |
| } | |
| public void pass() { | |
| updateJobResult(STATUS.passed); | |
| } | |
| public void fail() { | |
| updateJobResult(STATUS.failed); | |
| } | |
| /** Job Info **/ | |
| public void updateJobInfo(String json) | |
| { | |
| execute(JOB_INFO, json); | |
| } | |
| public void updateJobInfo(HashMap<String, Object> map) | |
| { | |
| updateJobInfo(toJson(map)); | |
| } | |
| public void setJobName(String name) | |
| { | |
| execute(JOB_NAME, name); | |
| } | |
| public void setJobBuild(String build) | |
| { | |
| execute(JOB_BUILD, build); | |
| } | |
| public void setJobTags(String tags) | |
| { | |
| execute(JOB_TAGS, tags); | |
| } | |
| public void setJobTags(List<String> tags) | |
| { | |
| setJobTags(toCommaSeparatedList(tags)); | |
| } | |
| /** add context output **/ | |
| public void setContext(String context) | |
| { | |
| execute(CONTEXT, context); | |
| } | |
| /** Control execution flow **/ | |
| public void stopExecution() | |
| { | |
| execute(BREAK); | |
| } | |
| /** Control network **/ | |
| public void stopNetwork() | |
| { | |
| execute(STOP_NETWORK); | |
| } | |
| public void startNetwork() | |
| { | |
| execute(START_NETWORK); | |
| } | |
| /** private helper methods **/ | |
| private void log(String... message) | |
| { | |
| System.out.println(message); | |
| } | |
| private String toJson(HashMap<String, Object> map) | |
| { | |
| return new Gson().toJson(map); | |
| } | |
| public String toCommaSeparatedList(List<String> list) | |
| { | |
| return StringUtils.join(list, ","); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment