NightMare でスクリーンショットを撮る時にディレクトリを作成する
Nightmare を使ってテストする時にスクリーンショットを撮るのですが、指定したディレクトリが存在しなかった場合、作成してから保存したい。
そんなときは make-dir - npm を使用する。
make-dirはサブディレクトリも含めて存在しなければ作成してくれるスグレモノ。
使い方も簡単。
Nightmare
の /node_modules/eramthgin/lib/actions.js
を修正する。
actions.js
var paths = require('path');
var makeDir = require('make-dir');
// 中略
exports.screenshot = function (path, clip, done) {
debug('.screenshot()');
if (typeof path === 'function') {
done = path;
clip = undefined;
path = undefined;
} else if (typeof clip === 'function') {
done = clip;
clip = (typeof path === 'string') ? undefined : path;
path = (typeof path === 'string') ? path : undefined;
}
this.child.call('screenshot', path, clip, function (error, img) {
var buf = new Buffer(img);
// debug('.screenshot() captured with length %s', buf.length);
// path ? fs.writeFile(path, buf, done) : done(null, buf);
// ↓
makeDir(paths.dirname(path)).then(res => {
console.log(res);
debug('.screenshot() captured with length %s', buf.length);
path ? fs.writeFile(path, buf, done) : done(null, buf);
});
});
};
Author And Source
この問題について(NightMare でスクリーンショットを撮る時にディレクトリを作成する), 我々は、より多くの情報をここで見つけました https://qiita.com/axcelwork@github/items/897d884fbb91e13f16a7著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .