elgalu / docker-selenium

[NOT MAINTAINED] Please use <https://github.com/SeleniumHQ/docker-selenium>

Home Page:https://github.com/SeleniumHQ/docker-selenium

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Grid plugin: create a separate video for each test

elgalu opened this issue · comments

Also read blog post http://qa-automation-notes.blogspot.de/2016/04/docker-selenium-and-bit-of-allure-how.html

Now we need to create a HubProxy, which will intercept sessions

@Override
public void beforeSession(final TestSession session) {
 super.beforeSession(session);
 processRecording(session, Command.START_RECORDING);
}

@Override
public void afterSession(final TestSession session) {
 super.afterSession(session);
 processRecording(session, Command.STOP_RECORDING);
}

private void processRecording(final TestSession session, final Command command) {
 final String videoInfo = getCapability(session, "videoInfo");

 if (!videoInfo.isEmpty()) {
  final String url = "http://" + this.getRemoteHost().getHost() + ":" + this.getRemoteHost().getPort() +
   "/extra/" + VideoRecordingServlet.class.getSimpleName() + "?command=" + command;

  switch (command) {
   case START_RECORDING:
    sendRecordingRequest(url, videoInfo);
    break;
   case STOP_RECORDING:
    sendRecordingRequest(url, "");
    break;
  }
 }
}

private void sendRecordingRequest(final String url, final String entity) {
 CloseableHttpResponse response = null;

 try (final CloseableHttpClient client = HttpClientBuilder.create().build()) {
  final HttpPost post = new HttpPost(url);

  if (!entity.isEmpty()) {
   post.setEntity(new StringEntity(entity, ContentType.APPLICATION_JSON));
  }

  response = client.execute(post);
  LOGGER.info("Node response: " + response);
 } catch (Exception ex) {
  LOGGER.severe("Unable to send recording request to node: " + ex);
 } finally {
  HttpClientUtils.closeQuietly(response);
 }
}

Note that we're getting video options from end-user in a form of DesiredCapabilities. It means that on proxy level we need to retrieve corresponding json and put it into request's entity, which will be sent then to VideoRecordingServlet.
That's it. Now you can rebuild selenium-server-standalone.jar with video recording feature on board using maven-assembly-plugin.
Modify NodeChrome / NodeFirefox config.json to allow our custom servlet and proxy support.

{
  "capabilities": [
    {
      "browserName": "chrome",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "configuration": {
    "proxy": "com.blogspot.notes.automation.qa.grid.HubProxy",
    "servlets": "com.blogspot.notes.automation.qa.grid.VideoRecordingServlet",
    "maxSession": 1,
    "port": 5555,
    "register": true,
    "registerCycle": 5000
  }
}

Usage

private Capabilities getCapabilities() {
 DesiredCapabilities capabilities = new DesiredCapabilities();
 capabilities.setBrowserName(getBrowser());
 capabilities.setPlatform(getDefaultPlatform());

 String videoInfo = getVideoRecordingInfo();
 if (!videoInfo.isEmpty()) {
  capabilities.setCapability("videoInfo", videoInfo);
 }

 return capabilities;
}

public String getVideoRecordingInfo() {
 try {
  VideoInfo videoInfo = new VideoInfo("/e2e/uploads", getVideoRecordingPath(), 18, 25);
  return new ObjectMapper().writeValueAsString(videoInfo);
 } catch (Exception ignored) {
  return "";
 }
}