Search in sources :

Example 1 with GoogleAnalyticsApi

use of de.zalando.ep.zalenium.util.GoogleAnalyticsApi in project zalenium by zalando.

the class SauceLabsRemoteProxyTest method testEventIsInvoked.

@Test
public void testEventIsInvoked() throws IOException {
    try {
        // Capability which should result in a created session
        Map<String, Object> requestedCapability = new HashMap<>();
        requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
        requestedCapability.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
        // Getting a test session in the sauce labs node
        TestSession testSession = sauceLabsProxy.getNewSession(requestedCapability);
        Assert.assertNotNull(testSession);
        // We release the sessions and invoke the afterCommand with a mocked object
        Environment env = mock(Environment.class);
        when(env.getBooleanEnvVariable("ZALENIUM_SEND_ANONYMOUS_USAGE_INFO", false)).thenReturn(true);
        when(env.getStringEnvVariable("ZALENIUM_GA_API_VERSION", "")).thenReturn("1");
        when(env.getStringEnvVariable("ZALENIUM_GA_TRACKING_ID", "")).thenReturn("UA-88441352");
        when(env.getStringEnvVariable("ZALENIUM_GA_ENDPOINT", "")).thenReturn("https://www.google-analytics.com/collect");
        when(env.getStringEnvVariable("ZALENIUM_GA_ANONYMOUS_CLIENT_ID", "")).thenReturn("RANDOM_STRING");
        HttpClient client = mock(HttpClient.class);
        HttpResponse httpResponse = mock(HttpResponse.class);
        when(client.execute(any(HttpPost.class))).thenReturn(httpResponse);
        GoogleAnalyticsApi ga = new GoogleAnalyticsApi();
        GoogleAnalyticsApi gaSpy = spy(ga);
        gaSpy.setEnv(env);
        gaSpy.setHttpClient(client);
        SauceLabsRemoteProxy.setGa(gaSpy);
        WebDriverRequest webDriverRequest = mock(WebDriverRequest.class);
        HttpServletResponse response = mock(HttpServletResponse.class);
        when(webDriverRequest.getMethod()).thenReturn("DELETE");
        when(webDriverRequest.getRequestType()).thenReturn(RequestType.STOP_SESSION);
        testSession.getSlot().doFinishRelease();
        testSession.setExternalKey(new ExternalSessionKey("testKey"));
        sauceLabsProxy.afterCommand(testSession, webDriverRequest, response);
        verify(gaSpy, times(1)).testEvent(anyString(), anyString(), anyLong());
    } finally {
        SauceLabsRemoteProxy.restoreGa();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) GoogleAnalyticsApi(de.zalando.ep.zalenium.util.GoogleAnalyticsApi) ExternalSessionKey(org.openqa.grid.internal.ExternalSessionKey) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) TestSession(org.openqa.grid.internal.TestSession) HttpClient(org.apache.http.client.HttpClient) Environment(de.zalando.ep.zalenium.util.Environment) WebDriverRequest(org.openqa.grid.web.servlet.handler.WebDriverRequest) Test(org.junit.Test)

Aggregations

Environment (de.zalando.ep.zalenium.util.Environment)1 GoogleAnalyticsApi (de.zalando.ep.zalenium.util.GoogleAnalyticsApi)1 HashMap (java.util.HashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpPost (org.apache.http.client.methods.HttpPost)1 Test (org.junit.Test)1 ExternalSessionKey (org.openqa.grid.internal.ExternalSessionKey)1 TestSession (org.openqa.grid.internal.TestSession)1 WebDriverRequest (org.openqa.grid.web.servlet.handler.WebDriverRequest)1