Skip to content

Commit

Permalink
create screenshot dir if plugin is used (closes DevExpress#4477) (Dev…
Browse files Browse the repository at this point in the history
…Express#4488)

* create screenshot dir if plugin is used (closes DevExpress#4477)

* remove obsolete code

* refactoring
  • Loading branch information
AlexKamaev authored and kirovboris committed Dec 18, 2019
1 parent 4f5f54b commit 6d54969
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 66 deletions.
26 changes: 16 additions & 10 deletions src/browser/provider/index.ts
@@ -1,5 +1,7 @@
import browserTools from 'testcafe-browser-tools';
import OS from 'os-family';
import { dirname } from 'path';
import makeDir from 'make-dir';
import BrowserConnection from '../connection';
import delay from '../../utils/delay';
import { GET_TITLE_SCRIPT, GET_WINDOW_DIMENSIONS_INFO_SCRIPT } from './utils/client-functions';
Expand Down Expand Up @@ -304,19 +306,23 @@ export default class BrowserProvider {
}

public async takeScreenshot (browserId: string, screenshotPath: string, pageWidth: number, pageHeight: number, fullPage: boolean): Promise<void> {
const canUseDefaultWindowActions = await this._canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomTakeScreenshot = customActionsInfo.hasTakeScreenshot;
const canUseDefaultWindowActions = await this._canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomTakeScreenshot = customActionsInfo.hasTakeScreenshot;
const connection = BrowserConnection.getById(browserId) as BrowserConnection;
const takeLocalBrowsersScreenshot = canUseDefaultWindowActions && !hasCustomTakeScreenshot;
const isLocalFullPageMode = takeLocalBrowsersScreenshot && fullPage;

if (canUseDefaultWindowActions && !hasCustomTakeScreenshot) {
if (fullPage) {
const connection = BrowserConnection.getById(browserId) as BrowserConnection;
if (isLocalFullPageMode) {
connection.addWarning(WARNING_MESSAGE.screenshotsFullPageNotSupported, connection.browserInfo.alias);

connection.addWarning(WARNING_MESSAGE.screenshotsFullPageNotSupported, connection.browserInfo.alias);
}
else
await this._takeLocalBrowserScreenshot(browserId, screenshotPath);
return;
}

await makeDir(dirname(screenshotPath));

if (takeLocalBrowsersScreenshot)
await this._takeLocalBrowserScreenshot(browserId, screenshotPath);
else
await this.plugin.takeScreenshot(browserId, screenshotPath, pageWidth, pageHeight, fullPage);
}
Expand Down
17 changes: 1 addition & 16 deletions src/utils/promisified-functions.js
Expand Up @@ -3,8 +3,6 @@ import fs from 'graceful-fs';
import { PNG } from 'pngjs';
import promisifyEvent from 'promisify-event';
import { promisify } from 'util';
import makeDir from 'make-dir';
import { dirname } from 'path';

export const readDir = promisify(fs.readdir);
export const stat = promisify(fs.stat);
Expand Down Expand Up @@ -36,9 +34,7 @@ export async function readPngFile (filePath) {
return await readPng(buffer);
}

export async function writePng (filePath, png) {
await ensureDir(filePath);

export function writePng (filePath, png) {
const outStream = fs.createWriteStream(filePath);
const pngStream = png.pack();

Expand All @@ -52,14 +48,3 @@ export async function writePng (filePath, png) {

return finishPromise;
}

async function ensureDir (filePath) {
const dirName = dirname(filePath);

try {
await stat(dirName);
}
catch (err) {
await makeDir(dirName);
}
}
46 changes: 37 additions & 9 deletions test/server/browser-provider-test.js
@@ -1,10 +1,15 @@
const expect = require('chai').expect;
const proxyquire = require('proxyquire');
const testcafeBrowserTools = require('testcafe-browser-tools');
const browserProviderPool = require('../../lib/browser/provider/pool');
const parseProviderName = require('../../lib/browser/provider/parse-provider-name');
const BrowserConnection = require('../../lib/browser/connection');
const WARNING_MESSAGE = require('../../lib/notifications/warning-message');
const expect = require('chai').expect;
const { noop, stubFalse } = require('lodash');
const nanoid = require('nanoid');
const { rmdirSync, statSync } = require('fs');
const { join, dirname } = require('path');
const proxyquire = require('proxyquire');
const testcafeBrowserTools = require('testcafe-browser-tools');
const browserProviderPool = require('../../lib/browser/provider/pool');
const parseProviderName = require('../../lib/browser/provider/parse-provider-name');
const BrowserConnection = require('../../lib/browser/connection');
const ProviderCtor = require('../../lib/browser/provider/');
const WARNING_MESSAGE = require('../../lib/notifications/warning-message');

class BrowserConnectionMock extends BrowserConnection {
constructor () {
Expand Down Expand Up @@ -264,10 +269,12 @@ describe('Browser provider', function () {
expect(result).to.be.true;
});
});
});
});

describe('API', () => {
describe('Screenshots', () => {
it('Should add warning if provider does not support `fullPage` screenshots', () => {
const ProviderCtor = require('../../lib/browser/provider/');

const provider = new ProviderCtor({
isLocalBrowser: () => true,
isHeadlessBrowser: () => false,
Expand All @@ -281,6 +288,27 @@ describe('Browser provider', function () {
expect(bc.message).eql(WARNING_MESSAGE.screenshotsFullPageNotSupported);
});
});

it('Should create a directory in screenshot was made using the plugin', () => {
const provider = new ProviderCtor({
isLocalBrowser: stubFalse,
isHeadlessBrowser: stubFalse,
hasCustomActionForBrowser: stubFalse,
takeScreenshot: noop
});

const dir = `temp${nanoid(7)}`;
const screenshotPath = join(process.cwd(), dir, 'tmp.png');

return provider.takeScreenshot('', screenshotPath, 0, 0, false)
.then(() => {
const stats = statSync(dirname(screenshotPath));

expect(stats.isDirectory()).to.be.true;

rmdirSync(dirname(screenshotPath));
});
});
});
});
});
Expand Down
39 changes: 9 additions & 30 deletions test/server/capturer-test.js
@@ -1,11 +1,11 @@
const expect = require('chai').expect;
const { resolve, dirname } = require('path');
const { rmdirSync, statSync } = require('fs');
const { writePng, readPng, deleteFile } = require('../../lib/utils/promisified-functions');
const Capturer = require('../../lib/screenshots/capturer');
const nanoid = require('nanoid');
const expect = require('chai').expect;
const { resolve, dirname } = require('path');
const { statSync } = require('fs');
const Capturer = require('../../lib/screenshots/capturer');

const image = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg==', 'base64');
const screenshotPath = resolve(process.cwd(), 'temp-screenshots', 'temp.png');

const filePath = resolve(process.cwd(), `temp${nanoid(7)}`, 'temp.png');

class CapturerMock extends Capturer {
constructor (provider) {
Expand All @@ -20,42 +20,21 @@ const emptyProvider = {
}
};

const writingProvider = {
takeScreenshot: async (browserId, filePath) => {
const png = await readPng(image);

await writePng(filePath, png);
}
};

describe('Capturer', () => {
it('Taking screenshots does not create a directory if provider does not', async () => {
let errCode = null;

const capturer = new CapturerMock(emptyProvider);

await capturer._takeScreenshot(screenshotPath);
await capturer._takeScreenshot({ filePath });

try {
statSync(dirname(screenshotPath));
statSync(dirname(filePath));
}
catch (err) {
errCode = err.code;
}

expect(errCode).eql('ENOENT');
});

it('Write png util created directory', async () => {
const capturer = new CapturerMock(writingProvider);

await capturer._takeScreenshot({ filePath: screenshotPath });

statSync(dirname(screenshotPath));
statSync(dirname(screenshotPath));

await deleteFile(screenshotPath);

rmdirSync(dirname(screenshotPath));
});
});
4 changes: 3 additions & 1 deletion test/server/crop-test.js
@@ -1,9 +1,11 @@
const nanoid = require('nanoid');
const expect = require('chai').expect;
const { resolve } = require('path');

const { writePng, readPng, deleteFile, readPngFile } = require('../../lib/utils/promisified-functions');

const image = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg==', 'base64');
const screenshotPath = resolve(process.cwd(), 'temp-screenshots', 'temp.png');
const screenshotPath = resolve(process.cwd(), `temp${nanoid(7)}.png`);

const {
cropScreenshot,
Expand Down

0 comments on commit 6d54969

Please sign in to comment.