How to read Dynamic files in Cypress - cypress

While doing automation I have to download files and store them in Cypress folder. File downloading and storing works, but I am not sure how to read those files since every time it is downloading its Prefix with some random number.
For Example
in the cypress/Animesh folder I can see some random files like 1234_abc.json, 2345_abc.json, 3334_abc.json, 3454_abc.json
How do I read the first file?

I am having a similar issue. I can successfully download the file but my issue is I cannot find a way to soft wait for it to be ready. I'm looking for something to check that there is at least one file in the folder. I'm using the findFile task but the issue is it doesn't wait, and i'm not sure how to get it to wait?
findFile: (mask) => {
if (!mask)
throw new Error('Missing a file mask to seach');
return globby(mask).then((list) => {
if (!list.length)
throw new Error(`Could not find files matching mask "${mask}"`);
return list[0];
});
}
});

Related

Photoshop script .DS_Store

I'm using Photoshop script. I get files from folders. My problem is that when I get the files and place them in an array the array contains hidden files that are in the folder for example ".DS_Store". I can get around this by using:
if (folders[i] != "~/Downloads/start/.DS_Store"){}
But I would like to use something better as I sometimes look in lots of folders and don't know the "~/Downloads/start/" part.
I tried to use indexOf but Photoshop script does not allow indexOf. Does anybody know of a way to check if ".DS_Store" is in the string "~/Downloads/start/.DS_Store" that works in Photoshop script?
I see this answer but I don't know how to use it to test: Photoshop script to ignore .ds_store
For anyone else looking for a solution to this problem, rather than explicitly trying to skip hidden files like .DS_Store, you can use the Folder Object's getFiles() method and pass an expression to build an array of file types you actually want to open. A simple way to use this method is as follows:
// this expression will match strings that end with .jpg, .tif, or .psd and ignore the case
var fileTypes = new RegExp(/\.(jpg|tif|psd)$/i);
// declare our path
var myFolder = new Folder("~/Downloads/start/");
// create array of files utilizing the expression to filter file types
var myFiles = myFolder.getFiles(fileTypes);
// loop through all the files in our array and do something
for (i = 0; i < myFiles.length; i++) {
var fileToOpen = myFiles[i];
open(fileToOpen);
// do stuff...
}
For anybody looking I used the Polyfill found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
indexOf() was added to the ECMA-262 standard in the 5th edition; as
such it may not be present in all browsers. You can work around this
by utilizing the following code at the beginning of your scripts. This
will allow you to use indexOf() when there is still no native support.
This algorithm matches the one specified in ECMA-262, 5th edition,
assuming TypeError and Math.abs() have their original values.

Processing - loadStrings() case-insensitive

Is there any way to load a text file in Processing while ignoring the case of the file name? I am opening multiple csv files, and some have the extension capitalized, ".CSV" rather than the standard ".csv", which results in errors due to the loadStrings() function being case-sensitive.
String file = sketchPath("test.csv");
String[] array = loadStrings(file);
The above gives the error:
This file is named test.CSV not test.csv. Rename the file or change your code.
I need a way to make the case of the file name or extension not matter. Any thoughts?
Short answer: No. The case-sensitivity of files comes from the operating system itself.
Longer answer: you could create code that just tries to load from multiple places.
Another approach would be to use Java's File class, which has functions for listing various files under a directory, then iterating through them and finding the file that you want. More info is available in the Java reference, but it might look something like this:
String[] array = null;
File dir = new File(sketchPath(""));
for(String file : dir.list()){
if(file.startsWith(yourFileNameHere)){
array = loadStrings(file);
break;
}
}
I haven't tested this code so you might have to play with it a little bit, but that's the basic idea. Of course, you might just want to rename your files ahead of time to avoid this problem.
Why not get the new filename from the error itself? To get the error statement into a String, we need to wrap loadStrings in a try and catch statement.
String[] array;
String file = "heLlo.txt";
try {
//if all is good then we load the file
array = loadStrings(file);
}catch(Exception e){
//otherwise when we get the error, we store it in a String
String error = e.toString();
Then we need to use regular expressions to get the filename from the error statement using match. The regex is /named ([^ +])/ (the filename can be assumed not to have any spaces in it).
String[]matches = match(error, "named ([^ ]+)");
The capture group with be in element 1 in the array containing the matches. So that would be the "real" filename,
String realFile = matches[1];
Finally we load the real file and store it in our array.
array = loadStrings(realFile);
}
Sure, if you want, you can put all of this into a function so that you won't have to use this code again and again every time you load a file. But obviously, it would just be easier if you just renamed or checked your filenames ahead in time.

groovy sort alphabetically

I am updating a groovy script which build a test runner suite inside SoapUI. The scripts scan a folders and add test case for each file in each folders.
The problem I am trying to solve is that the script is using eachFileMatch() which does not have a consistent behavior between Windows and Unix file systems. I need to update the script so that files are listed alphabetically.
I am quite new to groovy so I don't know where to get started. Saw that a sort() method does exist but I am not sure I can use it on eachFileMatch.
Here's the code snippet I need to adapt :
new File(projectPathTest+"/nord").eachDir{dir->
log.info("Dir > "+dir);
operation = dir.name
def wsdlTestCase = testSuite.addNewTestCase( operation )
wsdlTestCase.setFailOnError(false)
dir.eachFileMatch(~/.*_request\.xml/){file->
// Need Alphabetically sorting here
log.info("File >> "+file)
addTestStep(operation, file, wsdlTestCase, projectPath, endPoint)
}
Any starting point to do it will the groovy approach would be really appreciated as for now i don't have time to delve into the groovy API.
Regards
}
You can collect the files in a TreeSet which will maintain their sort order and then iterate over the set to display or otherwise act on your collection of files.
TreeSet<File> files = new TreeSet<File>()
dir.eachFileMatch(~/.*_request\.xml/){file->
files << file
}
files.each { f->
// log or do whatever with the files in order
}
If you need this ordering over all the files, you will need to put the TreeSet outside of the eachDir closure.

Counting number of files from folder on Windows Phone

I have a folder with a number of .wav files inside. I need to dynamically count these files in order to generate the sound in a random way. I need this dynamically, so I don't need to update code everytime I update the sound folder with another sound.
I searched how to do it, but could only found examples for Windows. Here's the code I came up with:
string path = string.Format("/Sound/{0}", sourceSound);
return Directory.GetFiles(path, ".wav").Length;
I tried running it, but VS gives me the error:
"Unable to step. The code is currently unavailable."
Is there anything I'm doing wrong, or any other case how can we count the number of files inside a folder?
Thanks.
Try
using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
var i = myIsolatedStorage.GetFileNames("*.wav").Length;
}
For some reason I couldn't make the example Vitalii gave me work. I was never able to get the count of files I have in the volder.
Looking over the internet, I stumbled upon this link:
confused about resources and GetManifestResourceNames()
The answer Zack gave on this thread, gave the insight I needed to make my App work.
I used Embedded Resource to find all the count of files I needed and, then, play the with a SoundEffectInstance.
The following link helped on this:
http://matthiasshapiro.com/2011/01/10/embedding-a-sound-file-in-windows-phone-7-app-silverlight/
Here's how I managed to make it work:
soundCount = GetSoundCount();
private int GetSoundCount()
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(name => name.Contains(sourceImg)).Count();
}
With these few lines I managed to get the exact number of files I have in my App.
To make the sound play, I used the second link as an example and produced the code below:
if(soundInstance != null)
soundInstance.Stop();
this.soundInstance = SetNextSource();
soundInstance.Play();
private SoundEffectInstance SetNextSource()
{
Random random = new Random();
Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(string.Format("Assembly Name.Folder.{0}.{1}.wav", SubFolder, FileName + random.Next(soundCount)));
SoundEffect se = SoundEffect.FromStream(stream);
return se.CreateInstance();
}
Well after a few days o research finally managed to make it work.
Hope this thread helps people facing the same problem I did.
Thanks.

Using ExtAudioFileWrite to write at the end of a file

I'm trying to open a file and append data to it using ExtAudioFileWrite. Now, creating/initial writing(on creation)/converting works just fine, but unfortunately I can't seem to be able to open the file for writing afterwards. There is only one function that seems to open the file: ExAudioFileOpenURL but only for reading. So, how can I write at the end of a file if I'm not able to open the file for writing?
I might be wrong, or not fully understanding your question. But, once you start writing to the file using ExtAudioFileWrite you can keep writing to it by calling the ExtAudioFileWrite again. I am about to venture into coding with no compiler, hopefully you will get the idea
Example:
....variables declaration and your logic.....
//1.Create File to write output
ExtAudioFileRef outputFile;
//+++++++ LOOP to append as many files as you want +++++++//
//2.Create file reference to files you want to merge
ExtAudioFileRef audioFileObject = 0;
//3.open the first file
ExtAudioFileOpenURL (firstFileURL, &audioFileObject);
//4.get file properties
AudioStreamBasicDescription AudioFileFormat = 0;
UInt64 numberOfPacketsToReadInCurrentFile = 0;
ExtAudioFileGetProperty(audioFileObject,
kExtAudioFileProperty_FileLengthFrames,
sizeof(numberOfPacketsToReadInCurrentFile),
&numberOfPacketsToReadInCurrentFile
);
ExtAudioFileGetProperty(audioFileObject,
kExtAudioFileProperty_FileDataFormat,
sizeof(AudioFileFormat),
&AudioFileFormat
);
//5.Set destination ASBD
ExtAudioFileSetProperty(audioFileObject,
kExtAudioFileProperty_ClientDataFormat,
sizeof (importFormat),
&importFormat
);
//6.Set/Create/Allocate Some Buffers
//use AudioFileFormat to setup your buffers accordingly
AudioBufferList theBufferList = 0 // bufferList setup (I dont remember that)
//7.Read the file into the buffer
ExtAudioFileRead(audioFileObject,&numberOfPacketsToReadInCurrentFile,theBufferList);
//8.Write the buffer to a File
ExtAudioFileWrite(outputFile, numberOfPacketsToReadInCurrentFile, theBufferList);
free(theBufferList);
//if you want to append more files go to point 2 with the next file URL
//+++++++CLOSE LOOP+++++++++//
//once you are done.. just dispose the file and clean up everything left
ExtAudioFileDispose(outputFile);
This will not compile, I guess. But hopefully will help you get the idea.

Resources