Cypress file upload for negative test - cypress

I have a form that accepts a file and using cypress-file-upload, I made it to work correctly. But I also have a scenario where I need to verify an error when no file is uploaded. The package itself is validating the file is exist or not. How we can test in such case?
cy.get('input[type="file"]').attachFile();// Cannot read property 'filePath' of undefined
cy.get('input[type="file"]').attachFile(""); //missing "filePath" or "fileName".
Please make sure you are passing either "filePath" or "fileName"
cy.get('input[type="file"]').attachFile("expense1.json")//Works for the positive case
cy.get(".alert").should('be.visible').and('contain', "You poor guy did not upload a file...")

It's not clear from the question how a user would do the wrong thing and cause the alert, this is what you'd want to replicate in test.
Maybe there is no such user path, if it's just for checking some internal error occurred.
You should find out exactly what triggers the alert and test accordingly.
In this case, here's some ideas
option allowEmpty: true and cy.get('input[type="file"]').attachFile("")
make expense1.json a binary file, or an empty file, or a badly formed json (remove a bracket)
give an invalid encoding in options, encoding: 'binary' and use valid expense1.json`
give an invalid mimeType in options

Related

Makecat failure: no members found

I am trying to modify existing input cdf file to use SHA256 instead of SHA1 by adding following two lines under [CatalogHeader] section:
CatalogVersion=2
HashAlgorithms=SHA256
Executing makecat.exe now gives me following failure message even though nothing under [CatalogFiles] has changed:
Failed: CryptCATCDFEnumMembersByCDFTagEx. Last Error: 0x00000057
Failed: No members found. Last Error: 0x00000057
Failed 0x00000057 (87)
Makecat does find and hash all files if I take out two lines I added.
Can anybody give me an idea what might be going wrong here?
Here is an example cdf file for MCVE:
[CatalogHeader]
Name=MCVE.cat
CatalogVersion=2
HashAlgorithms=SHA256
[CatalogFiles]
MCVE.xml=MCVE.xml
MCVE.xml is any old xml file you can find.
I encountered the same problem but was able to get it to work by putting '< HASH >' (without spaces) in front of each file entry. Example:
[CatalogFiles]
<HASH>manifest.json=.\manifest.json
<HASH>bsi.json=.\bsi.json
However, this causes the catalog file's entries to be tagged by their hash, instead of their filename, when viewing the .cat file in Windows Explorer. You can somewhat work around this by adding a custom attribute to display the filename in the catalog entry's details, as follows:
[CatalogFiles]
<HASH>manifest.json=.\manifest.json
<HASH>manifest.jsonATTR1=0x11010001:File:manifest.json
<HASH>bsi.json=.\bsi.json
<HASH>bsi.jsonATTR1=0x11010001:File:bsi.json
The attribute type is composed of (https://learn.microsoft.com/en-us/windows/desktop/seccrypto/makecat):
0x10000000: attribute is included in the catalog's hash
0x01000000: don't create a duplicated attribute with SHA1 hash (when using SHA256 and catalog version 2)
0x00010000: attribute is in plaintext, not base64
0x00000001: attribute is a keyvalue pair (e.g. File=bsi.json)
I discovered this workaround after running into the same problem as you when I found this example here: https://www-user.tu-chemnitz.de/~heha/viewzip.cgi/basteln/PC/USB2LPT/usb2lpt.zip/src/Makefile?auto=MAK
Hope this helps.
Can't add comments yet ---
Just wanted to say Jonathan's example with the 0x11010001 attribute works great, but PowerShell's Test-FileCatalog will still say it fails to parse the file. Using FilePath instead of File fixed this. Not sure if this is in the spec or just a powershell quirk or what, but it's what PowerShell does with New-FileCatalog.
Bonus points for not including the SHA1 hash, thanks!

PROLOG - check if file exists

In my program I want to first check if a certain file exists. If it does I want to read it, if not I want to create it (using tell) and work with it. I can't get Prolog to check if a file exists.
I've tried
exists_file('testfile.pl')
exists('testfile.pl')
access_file('testfile.pl',exists)
For all three I get the message "existence error ... does not exist"
What's wrong here?
What I want is a predicate that returns true or false, depending on whether the file exists or not.
Thanks

Nightwatch error message

Can I change text output of failed tests in Nightwatch.js?
× Password Input in "Log in" Pop-up (#password) visible for 16000ms - expected "visible" but got: "not found"
I expect:
× Password Input in "Log in" Pop-up (#password) NOT FOUND for 16000ms
It depends on what you are using. If you are using any of the expect commands then no, you cannot customize the message. However, if you are using assert then you can.
browser.assert.visible('.should_be_visible', 'This is my custom message');
Old question but the above answer is not strictly correct. While some expect commands do not support an optional message, there are also those that do. You can look through the api docs which will tell you which commands support messages and which do not here: http://nightwatchjs.org/api
What is important to note is that optional messages as shown in the answer above will output regardless of pass/fail, meaning they are not strictly 'error messages'.
One way to adapt this is through your naming, for example if a test is looking for an element on a page, do not use a message of 'Cannot find element' as this will display even if the element is found successfully. Instead use something like 'Looking for element'...
.waitForElementVisible('#element', 1000, 'Looking for something on some page')
This at least tells the user what the test was trying to do when it failed but also makes sense when it passes. Hope that helps.

HP UFT (Unified Functional Testing) : Results View : Is there a way to capture the row, line number of the script where it failed?

I usually report the events when it passes or fails like:
Reporter.ReportEvent micPass," Title of the passed Event", " Description of Passed Event"
And
Reporter.ReportEvent micFail," Title of the failed Event", " Description of Failed Event"
Is it a good idea to hard code the line number in the title or description of the event like ?
Reporter.ReportEvent micFail," Test Failed at Line 432 ", " Please check line 432 "
To answer the 2nd part of your question, hard-coding will not be a good idea. If you hard-code the line number and in the future, a few lines get added to the code above that line number, the whole point hard-coding goes in vain as the report will now contain the incorrect-line number(which was hard-coded).
And for the first part, I can understand why you want the line-number to be reported. But I don't think there is any "predefined" way of doing it.
But, you can make your own function to get the line number. Here's an idea that you can implement:
Go to the location where your QTP test is stored. Get the full path of the file script.mts(you can find it inside the Action1 Folder). Also, this path can be constructed dynamically if you have good framework and proper folder structure.
After getting the path, you can open that file(script.mts) as a text stream(using the filesystemobject) in Read only mode.
Now you can traverse through the file line-by-line searching for the text "Reporter.ReportEvent micPass," Title of the passed Event", " Description of Passed Event"". After you have found it, you can easily get the line number and return that from your function to store it in some variable which you can use while reporting.
I have not tried this method myself but would have surely given it a try, If I really needed it.
Hardcoding is not a good idea from an end user perspective. In case it's a regression suite it could be used by a tester who doesn't know how to debug the code or analyze. To get the root cause of the failure through reporting (micPass/micFail) you can further refine your scripts

How can I check if a string is a valid file name for windows using R?

I've been writing a program in R that outputs randomization schemes for a research project I'm working on with a few other people this summer, and I'm done with the majority of it, except for one feature. Part of what I've been doing is making it really user friendly, so that the program will prompt the user for certain pieces of information, and therefore know what needs to be randomized. I have it set up to check every piece of user input to make sure it's a valid input, and give an error message/prompt the user again if it's not. The only thing I can't quite figure out is how to get it to check whether or not the file name for the .csv output is valid. Does anyone know if there is a way to get R to check if a string makes a valid windows file name? Thanks!
These characters aren't allowed: /\:*?"<>|. So warn the user if it contains any of those.
Some other names are also disallowed: COM, AUX, NUL, COM1 to COM9, LPT1 to LPT9.
You probably want to check that the filename is valid using a regular expression. See this other answer for a Java example that should take minimal tweaking to work in R.
https://stackoverflow.com/a/6804755/134830
You may also want to check the filename length (260 characters for maximum portability, though longer names are allowed on some systems).
Finally, in R, if you try to create a file in a directory that doesn't exist, it will still fail, so you need to split the name up into the filename and directory name (using basename and dirname) and try to create the directory first, if necessary.
That said, David Heffernan gives good advice in his comment to let Windows do the wok in deciding whether or not it can create the file: you don't want to erroneously tell the user that a filename is invalid.
You want something a little like this:
nice_file_create <- function(filename)
{
directory_name <- dirname(filename)
if(!file.exists(directory_name))
{
ok <- dir.create(directory_name)
if(!ok)
{
warning("The directory of that path could not be created.")
return(invisible())
}
}
tryCatch(
file.create(filename),
error = function(e)
{
warning("The file could not be created.")
}
)
}
But test it thoroughly first! There are all sorts of edge cases where things can fall over: try UNC network path names, "~", and paths with "." and ".." in them.
I'd suggest that the easiest way to make sure a filename is valid is to use fs::path_sanitize().
It removes control characters, reserved characters, and Windows-reserved filenames, truncating the string at 255 bytes in length.

Resources