Search in sources :

Example 16 with WebDriverRequest

use of org.openqa.grid.web.servlet.handler.WebDriverRequest in project zalenium by zalando.

the class SauceLabsRemoteProxyTest method testInformationIsRetrievedWhenStoppingSession.

@Test
public void testInformationIsRetrievedWhenStoppingSession() throws IOException {
    try {
        // Capability which should result in a created session
        Map<String, Object> requestedCapability = new HashMap<>();
        requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
        requestedCapability.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
        // Getting a test session in the sauce labs node
        SauceLabsRemoteProxy sauceLabsSpyProxy = spy(sauceLabsProxy);
        JsonElement informationSample = TestUtils.getTestInformationSample("saucelabs_testinformation.json");
        CommonProxyUtilities commonProxyUtilities = mock(CommonProxyUtilities.class);
        when(commonProxyUtilities.readJSONFromUrl(anyString(), anyString(), anyString())).thenReturn(informationSample);
        doCallRealMethod().when(commonProxyUtilities).convertFlvFileToMP4(any(TestInformation.class));
        SauceLabsRemoteProxy.setCommonProxyUtilities(commonProxyUtilities);
        TestSession testSession = sauceLabsSpyProxy.getNewSession(requestedCapability);
        Assert.assertNotNull(testSession);
        String mockSeleniumSessionId = "72e4f8ecf04440fe965faf657864ed52";
        testSession.setExternalKey(new ExternalSessionKey(mockSeleniumSessionId));
        // We release the session, the node should be free
        WebDriverRequest request = mock(WebDriverRequest.class);
        HttpServletResponse response = mock(HttpServletResponse.class);
        when(request.getMethod()).thenReturn("DELETE");
        when(request.getRequestType()).thenReturn(RequestType.STOP_SESSION);
        testSession.getSlot().doFinishRelease();
        sauceLabsSpyProxy.afterCommand(testSession, request, response);
        verify(sauceLabsSpyProxy, timeout(1000 * 5)).getTestInformation(mockSeleniumSessionId);
        TestInformation testInformation = sauceLabsSpyProxy.getTestInformation(mockSeleniumSessionId);
        Assert.assertEquals(mockSeleniumSessionId, testInformation.getTestName());
        Assert.assertThat(testInformation.getFileName(), CoreMatchers.containsString("saucelabs_72e4f8ecf04440fe965faf657864ed52_googlechrome_Windows_2008"));
        Assert.assertEquals("googlechrome 56, Windows 2008", testInformation.getBrowserAndPlatform());
        Assert.assertThat(testInformation.getVideoUrl(), CoreMatchers.containsString("jobs/72e4f8ecf04440fe965faf657864ed52/assets/video.flv"));
    } finally {
        SauceLabsRemoteProxy.restoreCommonProxyUtilities();
    }
}
Also used : TestInformation(de.zalando.ep.zalenium.dashboard.TestInformation) ExternalSessionKey(org.openqa.grid.internal.ExternalSessionKey) CommonProxyUtilities(de.zalando.ep.zalenium.util.CommonProxyUtilities) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) TestSession(org.openqa.grid.internal.TestSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebDriverRequest(org.openqa.grid.web.servlet.handler.WebDriverRequest) Test(org.junit.Test)

Example 17 with WebDriverRequest

use of org.openqa.grid.web.servlet.handler.WebDriverRequest in project zalenium by zalando.

the class SauceLabsRemoteProxyTest method slotIsReleasedWhenTestIsIdle.

@Test
public void slotIsReleasedWhenTestIsIdle() throws IOException {
    // Supported desired capability for the test session
    Map<String, Object> requestedCapability = new HashMap<>();
    requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.SAFARI);
    requestedCapability.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
    SauceLabsRemoteProxy sauceLabsSpyProxy = spy(sauceLabsProxy);
    // Set a short idle time
    sauceLabsSpyProxy.setMaxTestIdleTime(1L);
    // Start poller thread
    sauceLabsSpyProxy.startPolling();
    // Get a test session
    TestSession newSession = sauceLabsSpyProxy.getNewSession(requestedCapability);
    Assert.assertNotNull(newSession);
    newSession.setExternalKey(new ExternalSessionKey("RANDOM_EXTERNAL_KEY"));
    // Start the session
    WebDriverRequest request = TestUtils.getMockedWebDriverRequestStartSession(BrowserType.SAFARI, Platform.MAC);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream stream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(stream);
    sauceLabsSpyProxy.beforeCommand(newSession, request, response);
    // The terminateIdleSessions() method should be called after a moment
    verify(sauceLabsSpyProxy, timeout(2000)).terminateIdleSessions();
    verify(sauceLabsSpyProxy, timeout(2000)).addTestToDashboard("RANDOM_EXTERNAL_KEY", false);
}
Also used : ExternalSessionKey(org.openqa.grid.internal.ExternalSessionKey) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) TestSession(org.openqa.grid.internal.TestSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebDriverRequest(org.openqa.grid.web.servlet.handler.WebDriverRequest) Test(org.junit.Test)

Example 18 with WebDriverRequest

use of org.openqa.grid.web.servlet.handler.WebDriverRequest in project zalenium by zalando.

the class TestingBotRemoteProxyTest method testInformationIsRetrievedWhenStoppingSession.

@Test
public void testInformationIsRetrievedWhenStoppingSession() {
    // Capability which should result in a created session
    Map<String, Object> requestedCapability = new HashMap<>();
    requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
    requestedCapability.put(CapabilityType.PLATFORM_NAME, Platform.MAC);
    // Getting a test session in the TestingBot node
    TestingBotRemoteProxy spyProxy = spy(testingBotProxy);
    TestSession testSession = spyProxy.getNewSession(requestedCapability);
    Assert.assertNotNull(testSession);
    String mockSeleniumSessionId = "2cf5d115-ca6f-4bc4-bc06-a4fca00836ce";
    testSession.setExternalKey(new ExternalSessionKey(mockSeleniumSessionId));
    // We release the session, the node should be free
    WebDriverRequest request = mock(WebDriverRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getMethod()).thenReturn("DELETE");
    when(request.getRequestType()).thenReturn(RequestType.STOP_SESSION);
    testSession.getSlot().doFinishRelease();
    spyProxy.afterCommand(testSession, request, response);
    verify(spyProxy, timeout(1000 * 5)).getTestInformation(mockSeleniumSessionId);
    TestInformation testInformation = spyProxy.getTestInformation(mockSeleniumSessionId);
    Assert.assertEquals("loadZalandoPageAndCheckTitle", testInformation.getTestName());
    Assert.assertThat(testInformation.getFileName(), CoreMatchers.containsString("testingbot_loadZalandoPageAndCheckTitle_Safari9_CAPITAN"));
    Assert.assertEquals("Safari9 9, CAPITAN", testInformation.getBrowserAndPlatform());
    Assert.assertEquals("https://s3-eu-west-1.amazonaws.com/eurectestingbot/2cf5d115-ca6f-4bc4-bc06-a4fca00836ce.mp4", testInformation.getVideoUrl());
}
Also used : TestInformation(de.zalando.ep.zalenium.dashboard.TestInformation) ExternalSessionKey(org.openqa.grid.internal.ExternalSessionKey) HashMap(java.util.HashMap) TestSession(org.openqa.grid.internal.TestSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonObject(com.google.gson.JsonObject) WebDriverRequest(org.openqa.grid.web.servlet.handler.WebDriverRequest) Test(org.junit.Test)

Example 19 with WebDriverRequest

use of org.openqa.grid.web.servlet.handler.WebDriverRequest in project zalenium by zalando.

the class TestingBotRemoteProxyTest method requestIsNotModifiedInOtherRequestTypes.

@Test
public void requestIsNotModifiedInOtherRequestTypes() throws IOException {
    // Capability which should result in a created session
    Map<String, Object> requestedCapability = new HashMap<>();
    requestedCapability.put(CapabilityType.BROWSER_NAME, BrowserType.IE);
    requestedCapability.put(CapabilityType.PLATFORM_NAME, Platform.WIN8);
    // Getting a test session in the TestingBot node
    TestSession testSession = testingBotProxy.getNewSession(requestedCapability);
    Assert.assertNotNull(testSession);
    // We need to mock all the needed objects to forward the session and see how in the beforeMethod
    // the TestingBot user and api key get added to the body request.
    WebDriverRequest request = mock(WebDriverRequest.class);
    when(request.getRequestURI()).thenReturn("session");
    when(request.getServletPath()).thenReturn("session");
    when(request.getContextPath()).thenReturn("");
    when(request.getMethod()).thenReturn("POST");
    when(request.getRequestType()).thenReturn(RequestType.REGULAR);
    JsonObject jsonObject = new JsonObject();
    JsonObject desiredCapabilities = new JsonObject();
    desiredCapabilities.addProperty(CapabilityType.BROWSER_NAME, BrowserType.IE);
    desiredCapabilities.addProperty(CapabilityType.PLATFORM_NAME, Platform.WIN8.name());
    jsonObject.add("desiredCapabilities", desiredCapabilities);
    when(request.getBody()).thenReturn(jsonObject.toString());
    Enumeration<String> strings = Collections.emptyEnumeration();
    when(request.getHeaderNames()).thenReturn(strings);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream stream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(stream);
    testSession.forward(request, response, true);
    // The body should not be affected and not contain the TestingBot variables
    Assert.assertThat(request.getBody(), CoreMatchers.containsString(jsonObject.toString()));
    Assert.assertThat(request.getBody(), CoreMatchers.not(CoreMatchers.containsString("key")));
    Assert.assertThat(request.getBody(), CoreMatchers.not(CoreMatchers.containsString("secret")));
    when(request.getMethod()).thenReturn("GET");
    testSession.forward(request, response, true);
    Assert.assertThat(request.getBody(), CoreMatchers.containsString(jsonObject.toString()));
    Assert.assertThat(request.getBody(), CoreMatchers.not(CoreMatchers.containsString("key")));
    Assert.assertThat(request.getBody(), CoreMatchers.not(CoreMatchers.containsString("secret")));
}
Also used : HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) TestSession(org.openqa.grid.internal.TestSession) JsonObject(com.google.gson.JsonObject) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonObject(com.google.gson.JsonObject) WebDriverRequest(org.openqa.grid.web.servlet.handler.WebDriverRequest) Test(org.junit.Test)

Example 20 with WebDriverRequest

use of org.openqa.grid.web.servlet.handler.WebDriverRequest in project zalenium by zalando.

the class CloudTestingRemoteProxy method beforeCommand.

@Override
public void beforeCommand(TestSession session, HttpServletRequest request, HttpServletResponse response) {
    if (request instanceof WebDriverRequest && "POST".equalsIgnoreCase(request.getMethod())) {
        WebDriverRequest seleniumRequest = (WebDriverRequest) request;
        if (seleniumRequest.getRequestType().equals(RequestType.START_SESSION)) {
            int numberOfParallelCloudSessions = getNumberOfSessions();
            MDC.put("numberOfParallelCloudSessions", String.valueOf(numberOfParallelCloudSessions));
            logger.info("Currently using " + numberOfParallelCloudSessions + " parallel sessions towards " + getProxyName() + ". Attempt to start one more.");
            MDC.clear();
            String body = seleniumRequest.getBody();
            JsonObject jsonObject = new JsonParser().parse(body).getAsJsonObject();
            JsonObject desiredCapabilities = jsonObject.getAsJsonObject("desiredCapabilities");
            desiredCapabilities.addProperty(getUserNameProperty(), getUserNameValue());
            desiredCapabilities.addProperty(getAccessKeyProperty(), getAccessKeyValue());
            if (!desiredCapabilities.has(CapabilityType.VERSION) && proxySupportsLatestAsCapability()) {
                desiredCapabilities.addProperty(CapabilityType.VERSION, "latest");
            }
            try {
                seleniumRequest.setBody(jsonObject.toString());
            } catch (UnsupportedEncodingException e) {
                logger.error("Error while setting the body request in " + getProxyName() + ", " + jsonObject.toString());
            }
        }
    }
    super.beforeCommand(session, request, response);
}
Also used : JsonObject(com.google.gson.JsonObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) WebDriverRequest(org.openqa.grid.web.servlet.handler.WebDriverRequest) JsonParser(com.google.gson.JsonParser)

Aggregations

WebDriverRequest (org.openqa.grid.web.servlet.handler.WebDriverRequest)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 Test (org.junit.Test)17 TestSession (org.openqa.grid.internal.TestSession)17 HashMap (java.util.HashMap)11 ExternalSessionKey (org.openqa.grid.internal.ExternalSessionKey)11 JsonObject (com.google.gson.JsonObject)10 CommonProxyUtilities (de.zalando.ep.zalenium.util.CommonProxyUtilities)6 Environment (de.zalando.ep.zalenium.util.Environment)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 TestInformation (de.zalando.ep.zalenium.dashboard.TestInformation)4 JsonElement (com.google.gson.JsonElement)3 Mockito.anyString (org.mockito.Mockito.anyString)3 JsonParser (com.google.gson.JsonParser)2 ObjectName (javax.management.ObjectName)2 RegistrationRequest (org.openqa.grid.common.RegistrationRequest)2 JMXHelper (org.openqa.selenium.remote.server.jmx.JMXHelper)2 GoogleAnalyticsApi (de.zalando.ep.zalenium.util.GoogleAnalyticsApi)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1