diff --git a/lib/helper/FileSystem.js b/lib/helper/FileSystem.js index 4827ea6bf..9392f0c8b 100644 --- a/lib/helper/FileSystem.js +++ b/lib/helper/FileSystem.js @@ -59,6 +59,21 @@ class FileSystem extends Helper { assert.ok(fileExists(this.file), `File ${name} not found in ${this.dir}`); } + /** + * Checks that file with a name including given text exists in the current directory. + * + *```js + * I.handleDownloads(); + * I.click('Download as PDF'); + * I.amInPath('output/downloads'); + * I.seeFileNameMatching('.pdf'); + * ``` + */ + seeFileNameMatching(text) { + assert.ok(this.grabFileNames().some(file => file.includes(text)), + `File name which contains ${text} not found in ${this.dir}`); + } + /** * Checks that file found by `seeFile` includes a text. * @param {string} text @@ -98,6 +113,21 @@ class FileSystem extends Helper { const content = getFileContents(this.file, encoding); fileEquals(this.file).negate(text, content); } + + /** + * Returns file names in current directory. + * + * ```js + * I.handleDownloads(); + * I.click('Download Files'); + * I.amInPath('output/downloads'); + * const downloadedFileNames = I.grabFileNames(); + * ``` + */ + grabFileNames() { + return fs.readdirSync(this.dir) + .filter(item => !fs.lstatSync(path.join(this.dir, item)).isDirectory()); + } } function getFileContents(file, encoding) { diff --git a/test/unit/helper/FileSystem_test.js b/test/unit/helper/FileSystem_test.js index 075ce6a8f..b6ae48d5a 100644 --- a/test/unit/helper/FileSystem_test.js +++ b/test/unit/helper/FileSystem_test.js @@ -22,6 +22,14 @@ describe('FileSystem', () => { fs.dir.should.eql(path.join(global.codecept_dir, '/data')); }); + it('should see file', () => { + fs.seeFile('data/fs_sample.txt'); + fs.amInPath('data'); + fs.seeFile('fs_sample.txt'); + fs.grabFileNames().should.contain('fs_sample.txt'); + fs.seeFileNameMatching('sample'); + }); + it('should check file contents', () => { fs.seeFile('data/fs_sample.txt'); fs.seeInThisFile('FileSystem');