Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add support of mobitru screen recording feature #5307

Merged
merged 14 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/modules/plugins/pages/plugin-mobitru.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ a|`true` +
|`false`
|Inject special code into application to allow emulation of "touch id" action and QR code scan.

|`mobitru.enable-recording`
a|`true` +
`false`
|`false`
|Enable the video recording for entire appium session. The output recording will be attached to an allure report

|`mobitru.device-wait-timeout`
|The duration in {durations-format-link} format.
|`PT5M`
Expand Down
1 change: 1 addition & 0 deletions vividus-plugin-mobitru/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation project(':vividus-http-client')
implementation project(':vividus-extension-selenium')
implementation project(':vividus-plugin-mobile-app')
implementation project(':vividus-reporter')

implementation platform(group: 'org.slf4j', name: 'slf4j-bom', version: '2.0.16')
implementation(group: 'org.slf4j', name: 'slf4j-api')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ public void returnDevice(String deviceId) throws MobitruOperationException
executeRequest(DEVICE_PATH + "/" + deviceId, HttpMethod.DELETE, UnaryOperator.identity(), HttpStatus.SC_OK);
}

public void startDeviceScreenRecording(String deviceId) throws MobitruOperationException
{
performDeviceRecordingRequest(deviceId, HttpMethod.POST, HttpStatus.SC_CREATED);
}

public byte[] stopDeviceScreenRecording(String deviceId) throws MobitruOperationException
{
return performDeviceRecordingRequest(deviceId, HttpMethod.DELETE, HttpStatus.SC_OK);
}

private byte[] performDeviceRecordingRequest(String deviceId, HttpMethod httpMethod, int status)
throws MobitruOperationException
{
String url = String.format("%s/%s/recording", DEVICE_PATH, deviceId);
return executeRequest(url, httpMethod, UnaryOperator.identity(), status);
}

public byte[] downloadDeviceScreenRecording(String recordingId) throws MobitruOperationException
{
return executeRequest("/recording/" + recordingId, HttpMethod.GET,
UnaryOperator.identity(), HttpStatus.SC_OK);
}

public void installApp(String deviceId, String appId, InstallApplicationOptions options)
throws MobitruOperationException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ public interface MobitruFacade
*/
String takeDevice(DesiredCapabilities desiredCapabilities) throws MobitruOperationException;

/**
* Start the screen recording on the device with specified UDID.
*
* @param deviceId The UDID of the device
* @throws MobitruOperationException In case of any issues during start the recording.
*/
void startDeviceScreenRecording(String deviceId) throws MobitruOperationException;

/**
* Stop the screen recording on the device with specified UDID.
*
* @param deviceId The UDID of the device
* @return The ID of the recording artifact.
* @throws MobitruOperationException In case of any issues during start the recording.
*/
String stopDeviceScreenRecording(String deviceId) throws MobitruOperationException;

/**
* Stop the screen recording on the device with specified UDID.
*
* @param recordingId The ID of the recording artifact
* @return Binary content of the recording artifact.
* @throws MobitruOperationException In case of any issues during start the recording.
*/
byte[] downloadDeviceScreenRecording(String recordingId) throws MobitruOperationException;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in facade it makes sense to have a single method stopDeviceScreenRecording that will return a POJO with the recording ID and its binary content

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean to add bute[] content field to org.vividus.mobitru.client.model.ScreenRecording and then specify it in the facade method?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about Mobitru REST API and its mapping to model ScreenRecording, probably there should be another POJO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but downloadDeviceScreenRecording API call just returns a byte array.
how this another POJO should look like in this case ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just create a new object in the code from the results of 2 API calls

public record ScreenRecording(String recordingId, byte[] content)
{
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean that I should have 2 screen recording classes?
because, right now, I already have org.vividus.mobitru.client.model.ScreenRecording, which is using as DTO for result of stopRecording API and it contains recordingId only.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean that I should have 2 screen recording classes?

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/**
* Installs the desired application on the device with specified UDID
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
Expand All @@ -35,12 +34,14 @@
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LoggingEventBuilder;
import org.vividus.mobitru.client.exception.MobitruDeviceSearchException;
import org.vividus.mobitru.client.exception.MobitruDeviceTakeException;
import org.vividus.mobitru.client.exception.MobitruOperationException;
import org.vividus.mobitru.client.model.Application;
import org.vividus.mobitru.client.model.Device;
import org.vividus.mobitru.client.model.DeviceSearchParameters;
import org.vividus.mobitru.client.model.ScreenRecording;
import org.vividus.util.wait.DurationBasedWaiter;
import org.vividus.util.wait.RetryTimesBasedWaiter;
import org.vividus.util.wait.WaitMode;
Expand All @@ -57,6 +58,8 @@
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

private static final int RETRY_TIMES = 20;
private static final Duration DOWNLOAD_RECORDING_POOLING_TIMEOUT = Duration.ofSeconds(5);
private static final int DOWNLOAD_RECORDING_RETRY_COUNT = 10;

private final MobitruClient mobitruClient;
private Duration waitForDeviceTimeout;
Expand Down Expand Up @@ -109,6 +112,33 @@
mobitruClient.returnDevice(deviceId);
}

@Override
public void startDeviceScreenRecording(String deviceId) throws MobitruOperationException
{
mobitruClient.startDeviceScreenRecording(deviceId);
}

@Override
public String stopDeviceScreenRecording(String deviceId) throws MobitruOperationException
{
byte[] receivedRecordingInfo = mobitruClient.stopDeviceScreenRecording(deviceId);
ScreenRecording screenRecordingInfo = performMapperOperation(mapper ->
mapper.readValue(receivedRecordingInfo, ScreenRecording.class));
return screenRecordingInfo.getRecordingId();
}

@Override
public byte[] downloadDeviceScreenRecording(String recordingId) throws MobitruOperationException
{
Waiter waiter = new RetryTimesBasedWaiter(DOWNLOAD_RECORDING_POOLING_TIMEOUT,
DOWNLOAD_RECORDING_RETRY_COUNT);
Optional<byte[]> receivedRecording = performWaiterOperation(waiter,
() -> mobitruClient.downloadDeviceScreenRecording(recordingId),
LOGGER.atDebug(), "download device screen recording");
return receivedRecording.orElseThrow(() -> new MobitruOperationException(
String.format("Unable to download recording with id %s", recordingId)));

Check warning on line 139 in vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/client/MobitruFacadeImpl.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/client/MobitruFacadeImpl.java#L139

Added line #L139 was not covered by tests
}

private String takeDevice(Device device, Waiter deviceWaiter) throws MobitruOperationException
{
LOGGER.info("Trying to take device with configuration {}", device);
Expand All @@ -120,22 +150,13 @@
private String takeDevice(FailableSupplier<byte[], MobitruOperationException> takeDeviceActions,
Supplier<String> unableToTakeDeviceErrorMessage, Waiter deviceWaiter) throws MobitruOperationException
{
byte[] receivedDevice = deviceWaiter.wait(() -> {
try
{
return takeDeviceActions.get();
}
catch (MobitruDeviceTakeException e)
{
LOGGER.warn("Unable to take device, retrying attempt.", e);
return null;
}
}, Objects::nonNull);
if (null == receivedDevice)
Optional<byte[]> receivedDevice = performWaiterOperation(deviceWaiter, takeDeviceActions,
LOGGER.atWarn(), "take device");
if (receivedDevice.isEmpty())
{
throw new MobitruDeviceTakeException(unableToTakeDeviceErrorMessage.get());
}
Device takenDevice = performMapperOperation(mapper -> mapper.readValue(receivedDevice, Device.class));
Device takenDevice = performMapperOperation(mapper -> mapper.readValue(receivedDevice.get(), Device.class));
LOGGER.info("Device with configuration {} is taken", takenDevice);
return (String) takenDevice.getDesiredCapabilities().get(UDID);
}
Expand Down Expand Up @@ -213,6 +234,25 @@
}
}

private Optional<byte[]> performWaiterOperation(Waiter waiter,
FailableSupplier<byte[], MobitruOperationException> operation,
LoggingEventBuilder operationLoggerBuilder, String operationTitle)
{
return waiter.wait(() -> {
try
{
return Optional.of(operation.get());
}
catch (MobitruOperationException e)
{
operationLoggerBuilder
.setCause(e)
.log("Unable to {}, retrying attempt", operationTitle);
return Optional.empty();
}
}, Optional::isPresent);
}

private boolean isSearchForDevice(DesiredCapabilities desiredCapabilities)
{
Map<String, Object> capabilities = desiredCapabilities.asMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.mobitru.client.model;

public class ScreenRecording
{
private String recordingId;

public String getRecordingId()
{
return recordingId;
}

public void setRecordingId(String recordingId)
{
this.recordingId = recordingId;
}

@Override
public String toString()
{
return "ScreenRecording{id='" + recordingId + '}';

Check warning on line 36 in vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/client/model/ScreenRecording.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/client/model/ScreenRecording.java#L36

Added line #L36 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.mobitru.recording;

import com.google.common.eventbus.Subscribe;

import org.jbehave.core.annotations.AfterScenario;
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.ScenarioType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.mobitru.client.MobitruFacade;
import org.vividus.mobitru.client.exception.MobitruOperationException;
import org.vividus.reporter.event.IAttachmentPublisher;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.event.BeforeWebDriverQuitEvent;
import org.vividus.selenium.event.WebDriverCreateEvent;
import org.vividus.testcontext.TestContext;

public class MobitruRecordingListener
{
private static final String KEY = "mobitruDeviceId";
private static final Logger LOGGER = LoggerFactory.getLogger(MobitruRecordingListener.class);

private final MobitruFacade mobitruFacade;
private final IAttachmentPublisher attachmentPublisher;
private final IWebDriverProvider webDriverProvider;
private boolean enableRecording;
private final TestContext testContext;

public MobitruRecordingListener(MobitruFacade mobitruFacade, IAttachmentPublisher attachmentPublisher,
TestContext testContext, IWebDriverProvider webDriverProvider)
{
this.mobitruFacade = mobitruFacade;
this.attachmentPublisher = attachmentPublisher;
this.testContext = testContext;
this.webDriverProvider = webDriverProvider;
}

@BeforeScenario(uponType = ScenarioType.ANY)
public void startRecordingBeforeScenario()
{
//perform start recording if a web driver session is already exist,
//which means onSessionStart will be not executed
if (webDriverProvider.isWebDriverInitialized())
{
startRecordingIfEnabled();

Check warning on line 61 in vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java#L61

Added line #L61 was not covered by tests
}
}

Check warning on line 63 in vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java#L63

Added line #L63 was not covered by tests

@Subscribe
public void onSessionStart(WebDriverCreateEvent event)
{
startRecordingIfEnabled();
}

@Subscribe
public void onSessionStop(BeforeWebDriverQuitEvent event)
{
publishRecordingIfEnabled();
}

@AfterScenario(uponType = ScenarioType.ANY)
public void publishRecordingAfterScenario()
{
//perform publish if a web driver session is still exist,
//which means onSessionStop method was not executed
if (webDriverProvider.isWebDriverInitialized())
{
publishRecordingIfEnabled();

Check warning on line 84 in vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java#L84

Added line #L84 was not covered by tests
}
}

Check warning on line 86 in vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java

View check run for this annotation

Codecov / codecov/patch

vividus-plugin-mobitru/src/main/java/org/vividus/mobitru/recording/MobitruRecordingListener.java#L86

Added line #L86 was not covered by tests

private void startRecordingIfEnabled()
{
if (enableRecording)
{
String deviceId = getDeviceId();
try
{
mobitruFacade.startDeviceScreenRecording(deviceId);
}
catch (MobitruOperationException e)
{
LOGGER.atWarn()
.addArgument(deviceId)
.setCause(e)
.log("Unable to start recording on device with UUID {}");
}
}
}

private void publishRecordingIfEnabled()
{
if (enableRecording)
{
String deviceId = getDeviceId();
try
{
String recordingId = mobitruFacade.stopDeviceScreenRecording(deviceId);
byte[] recording = mobitruFacade.downloadDeviceScreenRecording(recordingId);
String attachmentName = String.format("mobitru_session_recording_%s.mp4", recordingId);
attachmentPublisher.publishAttachment(recording, attachmentName);
}
catch (MobitruOperationException e)
{
LOGGER.atWarn()
.addArgument(deviceId)
.setCause(e)
.log("Unable to stop or download recording on device with UUID {}");
}
}
}

public void setEnableRecording(boolean enableRecording)
{
this.enableRecording = enableRecording;
}

private String getDeviceId()
{
return testContext.get(KEY);
}
}
Loading
Loading