Skip to content
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ owl build --platform ios --config ./owl.config.json

#### Options

| Name | Required | Default | Options/Types | Description |
| ---------------- | -------- | ----------------- | --------------- | ----------------------------------------------- |
| `config`, `-c` | false | ./owl.config.json | String | Path to the configuration file |
| `platform`, `-p` | true | - | `ios`,`android` | The platform the app should be built on |
| `update`, `-u` | true | false | Boolean | A flag about rewriting existing baseline images |
| Name | Required | Default | Options/Types | Description |
| ------------------ | -------- | ----------------- | --------------- | ----------------------------------------------- |
| `--config`, `-c` | false | ./owl.config.json | String | Path to the configuration file |
| `--platform`, `-p` | true | - | `ios`,`android` | The platform the app should be built on |
| `--update`, `-u` | true | false | Boolean | A flag about rewriting existing baseline images |

#### Examples

Expand All @@ -90,7 +90,7 @@ import { takeScreenshot } from 'react-native-owl';

describe('App.tsx', () => {
it('takes a screenshot of the first screen', async () => {
const screen = await takeScreenshot();
const screen = await takeScreenshot('homescreen');

expect(screen).toMatchBaseline();
});
Expand All @@ -99,9 +99,9 @@ describe('App.tsx', () => {

### Methods

#### `takeScreenshot()`
#### `takeScreenshot(filename: string)`

Grabs a screenshot from the simulator and stores it under `latest` screenshots(ie. `./owl/latest/ios/`). If running the tests using the `--update` or `-u` flag, this will store the screenshot under the `baseline` directory. See example above.
Grabs a screenshot from the simulator and stores it under `latest` screenshots(ie. `./owl/latest/ios/`) with the specified filename(without the extension). If running the tests using the `--update` or `-u` flag, this will store the screenshot under the `baseline` directory. See example above.

### Jest Matchers

Expand Down
5 changes: 1 addition & 4 deletions example/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
bracketSpacing: false,
jsxBracketSameLine: true,
tabWidth: 2,
singleQuote: true,
trailingComma: 'all',
arrowParens: 'avoid',
};
4 changes: 2 additions & 2 deletions example/__tests__/App.owl.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {takeScreenshot} from 'react-native-owl';
import { takeScreenshot } from 'react-native-owl';

describe('App.tsx', () => {
it('takes a screenshot of the first screen', async () => {
const screen = await takeScreenshot();
const screen = await takeScreenshot('homescreen');

expect(screen).toMatchBaseline();
});
Expand Down
10 changes: 6 additions & 4 deletions src/take-screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path';

import { takeScreenshot } from './take-screenshot';

const SCREENSHOT_FILENAME = 'screen';

describe('take-screenshot.ts', () => {
const commandMock = jest.spyOn(execa, 'command');

Expand All @@ -27,7 +29,7 @@ describe('take-screenshot.ts', () => {
});

it('should take a screenshot', async () => {
await takeScreenshot();
await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
'xcrun simctl io booted screenshot screen.png',
Expand All @@ -47,7 +49,7 @@ describe('take-screenshot.ts', () => {
});

it('should take a screenshot', async () => {
await takeScreenshot();
await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
'adb exec-out screencap -p > screen.png',
Expand All @@ -73,7 +75,7 @@ describe('take-screenshot.ts', () => {
});

it('should take a screenshot', async () => {
await takeScreenshot();
await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
'xcrun simctl io booted screenshot screen.png',
Expand All @@ -93,7 +95,7 @@ describe('take-screenshot.ts', () => {
});

it('should take a screenshot', async () => {
await takeScreenshot();
await takeScreenshot(SCREENSHOT_FILENAME);

expect(commandMock).toHaveBeenCalledWith(
'adb exec-out screencap -p > screen.png',
Expand Down
21 changes: 14 additions & 7 deletions src/take-screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@ import path from 'path';
import { Platform } from './cli/types';
import { Logger } from './logger';

export const takeScreenshot = async (): Promise<string> => {
/**
* Takes a screenshot from the simulator.
* @param filename - Required. The filename(excluding the extension) that will be used to save the screenshot. ie. 'homepage'
* @returns the path to the screenshot.
*/
export const takeScreenshot = async (filename: string): Promise<string> => {
const platform = process.env.OWL_PLATFORM as Platform;
const debug = process.env.OWL_DEBUG === 'true';
const updateBaseline = process.env.OWL_UPDATE_BASELINE === 'true';
const screenshotFilename = `${filename}.png`;

const stdio = debug ? 'inherit' : 'ignore';
const logger = new Logger(!!debug);

const DEFAULT_FILENAME = 'screen.png';
const DIR_NAME = updateBaseline ? 'baseline' : 'latest';
const cwd = path.join(process.cwd(), '.owl', DIR_NAME, platform);
const screenshotsDirPath = path.join(process.cwd(), '.owl');
await fs.mkdir(screenshotsDirPath, { recursive: true });

const DIR_NAME = updateBaseline ? 'baseline' : 'latest';
const cwd = path.join(screenshotsDirPath, DIR_NAME, platform);
await fs.mkdir(cwd, { recursive: true });

const screenshotCommand =
platform === 'ios'
? `xcrun simctl io booted screenshot ${DEFAULT_FILENAME}`
: `adb exec-out screencap -p > ${DEFAULT_FILENAME}`;
? `xcrun simctl io booted screenshot ${screenshotFilename}`
: `adb exec-out screencap -p > ${screenshotFilename}`;

logger.info(`[OWL] Will run the screenshot command: ${screenshotCommand}.`);
await execa.command(screenshotCommand, {
Expand All @@ -31,7 +38,7 @@ export const takeScreenshot = async (): Promise<string> => {
shell: platform === 'android',
});

const screenshotPath = `${cwd}/${DEFAULT_FILENAME}`;
const screenshotPath = `${cwd}/${screenshotFilename}`;
logger.info(`[OWL] Screenshot saved to ${screenshotPath}.`);
return screenshotPath;
};