/// <summary> /// Rename the file and check if file was renamed with given timeout, shorten the name of file if needed be removing "_". /// </summary> /// <param name="waitTime">Timeout for checking if file was removed</param> /// <param name="oldName">The old name.</param> /// <param name="newName">The new name.</param> /// <param name="subFolder">The subFolder.</param> /// <returns>The new name in case its shorten</returns> /// <example>How to use it: <code> /// string newName = FilesHelper.RenameFile(BaseConfiguration.ShortTimeout, "filename.txt", "newname.txt", this.DriverContext.DownloadFolder); /// </code></example> public static string RenameFile(double waitTime, string oldName, string newName, string subFolder) { Logger.Debug(CultureInfo.CurrentCulture, "new file name: {0}", newName); NameHelper.ShortenFileName(subFolder, newName, "_", 255); if (File.Exists(newName)) { File.Delete(newName); } // Use ProcessStartInfo class string command = "/c ren " + '\u0022' + oldName + '\u0022' + " " + '\u0022' + newName + '\u0022'; ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe") { WorkingDirectory = subFolder, Arguments = command }; Thread.Sleep(1000); var timeoutMessage = string.Format(CultureInfo.CurrentCulture, "Waiting till file will be renamed {0}", subFolder); Process.Start(cmdsi); WaitHelper.Wait(() => File.Exists(subFolder + Separator + newName), TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); return(newName); }
/// <summary> /// Waits for file with given name with given timeout. /// </summary> /// <param name="waitTime">Wait timeout</param> /// <param name="filesName">Name of the files.</param> /// <param name="folder">The folder.</param> /// <example>How to use it: <code> /// var file = "some-file.txt" /// this.Driver.GetElement(this.fileLink.Format(file), "Click on file").Click(); /// FilesHelper.WaitForFileOfGivenName(BaseConfiguration.LongTimeout, file, this.DriverContext.DownloadFolder); /// </code></example> public static void WaitForFileOfGivenName(double waitTime, string filesName, string folder) { Logger.Debug(CultureInfo.CurrentCulture, "Wait for file: {0}", filesName); var timeoutMessage = string.Format(CultureInfo.CurrentCulture, "Waiting for file {0} in folder {1}", filesName, folder); WaitHelper.Wait( () => File.Exists(folder + Separator + filesName), TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); Logger.Debug("File exists, checking if size of last file > 0 bytes"); timeoutMessage = string.Format(CultureInfo.CurrentCulture, "Checking if size of file {0} > 0 bytes", filesName); WaitHelper.Wait( () => GetFileByName(folder, filesName).Length > 0, TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); }
/// <summary> /// Waits for file for given timeout till number of files increase in sub folder. /// </summary> /// <param name="waitTime">Wait timeout</param> /// <param name="filesNumber">The initial files number.</param> /// <param name="folder">The folder.</param> /// <example>How to use it:<code> /// var filesNumber = FilesHelper.CountFiles(this.DriverContext.DownloadFolder); /// this.Driver.GetElement(this.fileLink.Format("some-file.txt")).Click(); /// FilesHelper.WaitForFile(BaseConfiguration.LongTimeout, filesNumber, this.DriverContext.DownloadFolder); /// </code></example> public static void WaitForFile(double waitTime, int filesNumber, string folder) { Logger.Debug("Wait for file"); var timeoutMessage = string.Format(CultureInfo.CurrentCulture, "Waiting for file number to increase in {0}", folder); WaitHelper.Wait( () => CountFiles(folder) > filesNumber, TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); Logger.Debug("Number of files increased, checking if size of last file > 0 bytes"); timeoutMessage = "Checking if size of last file > 0 bytes"; WaitHelper.Wait( () => GetLastFile(folder).Length > 0, TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); }
/// <summary> /// Waits for file of given type for given timeout till number of files increase in sub folder,checks the size of the current file. /// </summary> /// <param name="type">The type of file.</param> /// <param name="waitTime">Wait timeout</param> /// <param name="filesNumber">The initial files number.</param> /// <param name="folder">The folder.</param> /// <param name="checkSize">Check if the size, in bytes, of the current file > 0.</param> /// <example>How to use it: <code> /// var filesNumber = FilesHelper.CountFiles(this.DriverContext.DownloadFolder, FileType.Txt); /// this.Driver.GetElement(this.fileLink.Format("some-file.txt")).Click(); /// FilesHelper.WaitForFileOfGivenType(FileType.Txt, BaseConfiguration.LongTimeout, filesNumber, this.DriverContext.DownloadFolder); /// </code></example> public static void WaitForFileOfGivenType(FileType type, double waitTime, int filesNumber, string folder, bool checkSize) { Logger.Debug("Wait for file: {0}", type); var timeoutMessage = string.Format(CultureInfo.CurrentCulture, "Waiting for file number to increase in {0}", folder); WaitHelper.Wait( () => CountFiles(folder, type) > filesNumber, TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); Logger.Debug("Number of files increased"); if (checkSize) { Logger.Debug("Checking if size of last file of given type {0} > 0 bytes", type); timeoutMessage = string.Format(CultureInfo.CurrentCulture, "Checking if size of last file of given type {0} > 0 bytes", type); WaitHelper.Wait( () => GetLastFile(folder, type).Length > 0, TimeSpan.FromSeconds(waitTime), TimeSpan.FromSeconds(1), timeoutMessage); } }