Created
November 15, 2019 00:52
-
-
Save sauceaaron/173bfdf9bef74fbbc222092c8dc8bb0f to your computer and use it in GitHub Desktop.
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 calculator.ios; | |
| import com.testobject.TestObjectResultWatcher; | |
| import io.appium.java_client.AppiumDriver; | |
| import io.appium.java_client.MobileElement; | |
| import io.appium.java_client.ios.IOSDriver; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.junit.rules.TestName; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| public class JUnitTestWithTestWatcher | |
| { | |
| static String testobject_datacenter = "https://us1.appium.testobject.com/wd/hub"; | |
| static String testobject_api_key = System.getenv("TESTOBJECT_API_KEY"); | |
| AppiumDriver<MobileElement> appium; | |
| String sessionId; | |
| @Rule | |
| public TestName testName = new TestName(); | |
| @Rule | |
| public TestObjectResultWatcher watcher = new TestObjectResultWatcher(testobject_api_key); | |
| @Before | |
| public void setup() throws MalformedURLException | |
| { | |
| URL url = getTestObjectURL(); | |
| DesiredCapabilities capabilities = getCapabilities(); | |
| appium = new IOSDriver<>(url, capabilities); | |
| sessionId = appium.getSessionId().toString(); | |
| watcher.forSession(sessionId); // pass sessionID to TestWatcher | |
| } | |
| @Test | |
| public void passingTestWithTestObjectResultWatcher() | |
| { | |
| assertThat(true).isTrue(); | |
| } | |
| @Test | |
| public void failingTestWithTestObjectResultWatcher() | |
| { | |
| assertThat(false).isTrue(); | |
| } | |
| @After | |
| public void cleanup() throws InterruptedException | |
| { | |
| appium.quit(); | |
| } | |
| private URL getTestObjectURL() throws MalformedURLException | |
| { | |
| return new URL(testobject_datacenter); | |
| } | |
| private DesiredCapabilities getCapabilities() | |
| { | |
| String testobject_test_name = getTestName(); | |
| String testobject_suite_name = getSuiteName(); | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| capabilities.setCapability("testobject_api_key", testobject_api_key); | |
| capabilities.setCapability("testobject_test_name", testobject_test_name); | |
| capabilities.setCapability("testobject_suite_name", testobject_suite_name); | |
| capabilities.setCapability("platformName", "iOS"); | |
| return capabilities; | |
| } | |
| private String getTestName() | |
| { | |
| return this.getClass().getSimpleName() + " " + testName.getMethodName(); | |
| } | |
| private String getSuiteName() | |
| { | |
| Path path = Paths.get(System.getProperty("user.dir")); | |
| return path.getFileName().toString(); | |
| } | |
| } |
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.testobject; | |
| import kong.unirest.HttpResponse; | |
| import kong.unirest.RequestBodyEntity; | |
| import kong.unirest.Unirest; | |
| import kong.unirest.json.JSONObject; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| public class TestObjectAPI | |
| { | |
| public String username; | |
| public String apiKey; | |
| public TestObjectAPI(String username, String apiKey) | |
| { | |
| this.username = username; | |
| this.apiKey = apiKey; | |
| } | |
| public void updateTestStatus(String sessionId, Boolean passed) | |
| { | |
| try | |
| { | |
| URL url = Endpoints.UpdateTestStatusURL(sessionId); | |
| JSONObject body = new JSONObject().put("passed", passed); | |
| RequestBodyEntity updateStatusRequest = Unirest.put(url.toString()) | |
| .body(body.toString()) | |
| .header("Content-type", "application/json") | |
| .basicAuth(username, apiKey); | |
| HttpResponse<String> response = updateStatusRequest.asString(); | |
| if (response.getStatus() != 204) | |
| { | |
| throw new TestStatusException("Failed to update test status."); | |
| } | |
| } | |
| catch (MalformedURLException e) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static class Endpoints | |
| { | |
| public static final String URL_BASE = "https://app.testobject.com/api/rest"; | |
| public static final String UPDATE_TEST_STATUS = "/v2/appium/session/{sessionId}/test"; | |
| public static URL UpdateTestStatusURL(String sessionId) throws MalformedURLException | |
| { | |
| return new URL(URL_BASE + UPDATE_TEST_STATUS.replace("{sessionId}", sessionId)); | |
| } | |
| } | |
| } |
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.testobject; | |
| import org.junit.rules.TestWatcher; | |
| import org.junit.runner.Description; | |
| public class TestObjectResultWatcher extends TestWatcher | |
| { | |
| String sessionId; | |
| TestObjectAPI testObject; | |
| public TestObjectResultWatcher(String apiKey) | |
| { | |
| testObject = new TestObjectAPI("", apiKey); | |
| } | |
| public void forSession(String sessionId) | |
| { | |
| this.sessionId = sessionId; | |
| } | |
| @Override | |
| protected void failed(Throwable e, Description description) | |
| { | |
| testObject.updateTestStatus(sessionId, false); | |
| } | |
| @Override | |
| protected void succeeded(Description description) | |
| { | |
| testObject.updateTestStatus(sessionId, true); | |
| } | |
| } |
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.testobject; | |
| public class TestStatusException extends RuntimeException | |
| { | |
| public TestStatusException(String message) | |
| { | |
| super(message); | |
| } | |
| public void printMessage() | |
| { | |
| System.err.println(getMessage()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment