// Path: web-selenium/src/main/java/org/jbehave/web/selenium/SauceWebDriverProvider.java
// public static String getSauceAccessKey() {
//     String access_key = System.getProperty("SAUCE_ACCESS_KEY");
//     if (access_key == null) {
//         throw new UnsupportedOperationException("SAUCE_ACCESS_KEY property name variable not specified");
//     }
//     return access_key;
// }
// 
// Path: web-selenium/src/main/java/org/jbehave/web/selenium/SauceWebDriverProvider.java
// public static String getSauceUser() {
//     String username = System.getProperty("SAUCE_USERNAME");
//     if (username == null) {
//         throw new UnsupportedOperationException("SAUCE_USERNAME property name variable not specified");
//     }
//     return username;
// }


import org.jbehave.core.model.Story;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Map;
import static org.jbehave.web.selenium.SauceWebDriverProvider.getSauceAccessKey;
import static org.jbehave.web.selenium.SauceWebDriverProvider.getSauceUser;
        if (storyName.equals("BeforeStories")
                || storyName.equals("AfterStories")
                || storyName.equals("BeforeStory")
                || storyName.equals("AfterStory")
                || storyName.equals("BeforeScenario")
                || storyName.equals("AfterScenario")) {
            return;
        }

        SessionId sessionId = sessionIds.get();

        if (sessionId == null ) {
            // no executed scenarios, as (most likely) excluded
            return;
        }

        boolean pass = passed.get().equals(true);
        String payload = "{ \"passed\":" + pass + "}";
        postJobUpdate(storyName, sessionId, payload);
        System.out.println("Saucelabs Job URL for " + (passed.get() ? "passing" : "failing") + " '" + storyName + "' : " + storyToJobIds.get(storyName));

    }

    private void postJobUpdate(String storyName, SessionId sessionId, String payload) {
        try {

            URL url = new URL("http://saucelabs.com/rest/v1/" + getSauceUser() + "/jobs/" + sessionId.toString());

            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
// Path: web-selenium/src/main/java/org/jbehave/web/selenium/SauceWebDriverProvider.java
// public static String getSauceAccessKey() {
//     String access_key = System.getProperty("SAUCE_ACCESS_KEY");
//     if (access_key == null) {
//         throw new UnsupportedOperationException("SAUCE_ACCESS_KEY property name variable not specified");
//     }
//     return access_key;
// }
// 
// Path: web-selenium/src/main/java/org/jbehave/web/selenium/SauceWebDriverProvider.java
// public static String getSauceUser() {
//     String username = System.getProperty("SAUCE_USERNAME");
//     if (username == null) {
//         throw new UnsupportedOperationException("SAUCE_USERNAME property name variable not specified");
//     }
//     return username;
// }


// Path: web-selenium/src/main/java/org/jbehave/web/selenium/SauceContextStoryReporter.java
import org.jbehave.core.model.Story;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Map;
import static org.jbehave.web.selenium.SauceWebDriverProvider.getSauceAccessKey;
import static org.jbehave.web.selenium.SauceWebDriverProvider.getSauceUser;

        if (storyName.equals("BeforeStories")
                || storyName.equals("AfterStories")
                || storyName.equals("BeforeStory")
                || storyName.equals("AfterStory")
                || storyName.equals("BeforeScenario")
                || storyName.equals("AfterScenario")) {
            return;
        }

        SessionId sessionId = sessionIds.get();

        if (sessionId == null ) {
            // no executed scenarios, as (most likely) excluded
            return;
        }

        boolean pass = passed.get().equals(true);
        String payload = "{ \"passed\":" + pass + "}";
        postJobUpdate(storyName, sessionId, payload);
        System.out.println("Saucelabs Job URL for " + (passed.get() ? "passing" : "failing") + " '" + storyName + "' : " + storyToJobIds.get(storyName));

    }

    private void postJobUpdate(String storyName, SessionId sessionId, String payload) {
        try {

            URL url = new URL("http://saucelabs.com/rest/v1/" + getSauceUser() + "/jobs/" + sessionId.toString());

            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(getSauceUser(), getSauceAccessKey().toCharArray());