title |
---|
writeFile |
Write to a file with the specified contents.
cy.writeFile(filePath, contents)
cy.writeFile(filePath, contents, encoding)
cy.writeFile(filePath, contents, options)
{% fa fa-check-circle green %} Correct Usage
cy.writeFile('menu.json')
{% fa fa-angle-right %} filePath (String)
A path to a file within the project root (the directory that contains cypress.json
).
{% fa fa-angle-right %} contents (String, Array, or Object)
The contents to be written to the file.
{% fa fa-angle-right %} encoding (String)
The encoding to be used when writing to the file. The following encodings are supported:
ascii
base64
binary
hex
latin1
utf8
utf-8
ucs2
ucs-2
utf16le
utf-16le
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.writeFile()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
flag |
w |
File system flag as used with {% url fs.writeFile https://nodejs.org/api/fs.html#fs_file_system_flags %} |
encoding |
utf8 |
The encoding to be used when writing to the file |
{% note info %}
To use encoding with other options, have your options object be your third parameter and include encoding there. This is the same behavior as {% url fs.writeFile
https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback %}.
{% endnote %}
{% yields null cy.writeFile %}
If the path to the file does not exist, the file and its path will be created. If the file already exists, it will be over-written.
cy
.writeFile('path/to/message.txt', 'Hello World')
.then((text) => {
expect(text).to.equal('Hello World') // true
})
{projectRoot}/path/to/message.txt
will be created with the following contents:
"Hello World"
JavaScript arrays and objects are stringified and formatted into text.
cy.writeFile('path/to/data.json', { name: 'Eliza', email: '[email protected]' })
.then((user) => {
expect(user.name).to.equal('Eliza')
})
{projectRoot}/path/to/data.json
will be created with the following contents:
{
"name": "Eliza",
"email": "[email protected]"
}
cy.request('https://jsonplaceholder.typicode.com/users').then((response) => {
cy.writeFile('cypress/fixtures/users.json', response.body)
})
// our fixture file is now generated and can be used
cy.fixture('users').then((users) => {
expect(users[0].name).to.exist
})
cy.writeFile('path/to/ascii.txt', 'Hello World', 'ascii'))
{projectRoot}/path/to/message.txt
will be created with the following contents:
Hello World
cy.writeFile('path/to/ascii.txt', 'Hello World', { encoding: 'ascii', flag: 'a+' })
cy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })
{% requirements write_file cy.writeFile %}
{% assertions once cy.writeFile %}
{% timeouts automation cy.writeFile %}
Write an array to a file
cy.writeFile('info.log', ['foo', 'bar', 'baz'])
The command above will display in the Command Log as:
{% imgTag /img/api/writefile/write-data-to-system-file-for-testing.png "Command Log writeFile" %}
When clicking on the writeFile
command within the command log, the console outputs the following:
{% imgTag /img/api/writefile/console-log-shows-contents-written-to-file.png "Console Log writeFile" %}
{% history %}
{% url "3.1.1" changelog#3-1-1 %} | Added flag
option and appending with a+
{% url "1.0.0" changelog#1.0.0 %} | cy.writeFile()
command added
{% endhistory %}
- {% url
cy.readFile()
readfile %}