Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Appium screen recording #895

Merged
merged 3 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion karate-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
| <a href="#screenshotfull"><code>screenshotFull()</code></a>
</td>
</tr>
<tr>
<th>Appium</th>
<td>
<a href="#screen-recording">Screen Recording</a>
| <a href="#hideKeyboard"><code>hideKeyboard()</code></a>
</td>
</tr>
</table>

## Capabilities
Expand Down Expand Up @@ -1333,4 +1340,20 @@ For more control or custom options, the `start()` method takes a `Map<String, Ob
Only supported for driver type [`chrome`](#driver-types). See [Chrome Java API](#chrome-java-api). This will snapshot the entire page, not just what is visible in the viewport.

## `pdf()`
Only supported for driver type [`chrome`](#driver-types). See [Chrome Java API](#chrome-java-api).
Only supported for driver type [`chrome`](#driver-types). See [Chrome Java API](#chrome-java-api).

# Appium

## `Screen Recording`
Only supported for driver type [`android | ios`](#driver-types).
```cucumber
* driver.startRecordingScreen()
# test
* driver.saveRecordingScreen("invoice.mp4",true)
```
above example would save the file and perform "auto-embedding" to HTML report.

you can also use `startRecordingScreen()` and `stopRecordingScreen()`, both takes recording options as JSON input.

## `hideKeyboard()`
Only supported for driver type [`android | ios`](#driver-types), for hide soft keyboard.
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,10 @@ public void embed(byte[] bytes, String contentType) {
Embed embed = new Embed();
embed.setBytes(bytes);
embed.setMimeType(contentType);
embed(embed);
}

public void embed(Embed embed) {
babusekaran marked this conversation as resolved.
Show resolved Hide resolved
if (prevEmbeds == null) {
prevEmbeds = new ArrayList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
package com.intuit.karate.driver;

import com.intuit.karate.*;
import com.intuit.karate.core.Embed;
import com.intuit.karate.shell.Command;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
* @author babusekaran
*/
Expand Down Expand Up @@ -84,6 +91,51 @@ public void hideKeyboard() {
http.path("appium", "device", "hide_keyboard").post("{}");
}

public String startRecordingScreen() {
return http.path("appium", "start_recording_screen").post("{}").jsonPath("$.value").asString();
}

public String startRecordingScreen(Map<String, Object> payload) {
Map<String, Object> options = new HashMap<>();
options.put("options",payload);
return http.path("appium", "start_recording_screen").post(options).jsonPath("$.value").asString();
}

public String stopRecordingScreen() {
return http.path("appium", "stop_recording_screen").post("{}").jsonPath("$.value").asString();
}

public String stopRecordingScreen(Map<String, Object> payload) {
Map<String, Object> options = new HashMap<>();
options.put("options",payload);
return http.path("appium", "stop_recording_screen").post(options).jsonPath("$.value").asString();
}

public void saveRecordingScreen(String fileName, boolean embed) {
String videoTemp = stopRecordingScreen();
byte[] bytes = Base64.getDecoder().decode(videoTemp);
File src = new File(fileName);
try (FileOutputStream fileOutputStream = new FileOutputStream(src.getAbsolutePath())){
fileOutputStream.write(bytes);
}
catch (Exception e){
logger.error("error while saveRecordingScreen {}", e.getMessage());
}
if (embed){
if (src.exists()) {
String path = FileUtils.getBuildDir() + File.separator
+ "cucumber-html-reports" + File.separator + System.currentTimeMillis() + ".mp4";
File dest = new File(path);
FileUtils.copy(src, dest);
options.embedContent(Embed.forVideoFile(dest.getName()));
}
}
}

public void saveRecordingScreen(String fileName) {
saveRecordingScreen(fileName,false);
}

@Override
public String text(String locator) {
String id = elementId(locator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.intuit.karate.FileUtils;
import com.intuit.karate.LogAppender;
import com.intuit.karate.Logger;
import com.intuit.karate.core.Embed;
import com.intuit.karate.core.ScenarioContext;
import com.intuit.karate.driver.android.AndroidDriver;
import com.intuit.karate.driver.chrome.Chrome;
Expand Down Expand Up @@ -481,6 +482,12 @@ public void embedPngImage(byte[] bytes) {
}
}

public void embedContent(Embed embed) {
if (context != null) {
context.embed(embed);
}
}

public static final Set<String> DRIVER_METHOD_NAMES = new HashSet();

static {
Expand Down