Powershell - Copying Folders and Files

Two options for copying files are below. Robocopy - More details can be found at Robocopy Copy_Item cmdlet - More details can be found at Copy-Item Copying Folder structure Only $source = "C:\tools\DevKit2\octopress-blog\source" $dest = "D:\delete" Copy-Item $source $dest -Filter {PSIsContainer} -Recurse -Force #OR robocopy $source $dest /e /xf *.* # /e denotes all folder including empty folders. /xf denotes all files except one of format *.* # /e can be replaced with /s for ignoring empty folders Flattening Folder structure - Copy all files from nested folders to a single folder $source = "C:\tools\DevKit2\octopress-blog\source" $dest = "D:\delete" # Below is required only if we need to create destination folder. Uncomment below line if folder needs to be created #New-Item $dest -type directory Get-ChildItem $source -Recurse | ` Where-Object { $_.PSIsContainer -eq $False } | ` ForEach-Object {Copy-Item -Path $_.Fullname -Destination $dest -Force} Copy same folder structure $source = "C:\tools\DevKit2\octopress-blog\source" $dest = "D:\delete" robocopy $source $dest /e

October 10, 2016

Git - How to solve filename too long error

Git for windows is normally shipped with long path support disabled due to mysys not supporting file path/name greater than 260 character. While cloning repository with large nested directory structute may cause error “file name too long”. This can be fixed by below command. It can be executed using powershell or cmd directly in project ( or anywhere if git variable is available) git config --system core.longpaths true

September 23, 2016

Running Command Line from C#

Code Snippet private void RunCLIjobsOnLocal(string arguments, int WaitTimePerCommand) { var psi = new ProcessStartInfo(); psi.CreateNoWindow = true; //This hides the dos-style black window that the command prompt usually shows psi.FileName = @"cmd.exe"; psi.Arguments = "/C " + arguments; psi.RedirectStandardOutput = true; psi.RedirectStandardInput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; var sspw = new SecureString(); foreach (var c in password) { sspw.AppendChar(c); } psi.Domain = domain; psi.UserName = userName; psi.Password = sspw; psi.WorkingDirectory = @"C:\"; using (Process process = new Process()) { try { process.StartInfo = psi; process.Start(); var procId = process.Id; string owner = GetProcessOwner(procId); // Synchronously read the standard output of the spawned process. StreamReader reader = process.StandardOutput; string output = reader.ReadToEnd(); reader = process.StandardError; string error = reader.ReadToEnd(); if(error.Length >0) process.WaitForExit(); } catch (Exception e) { log.Error(e.Message + "\n" + e.StackTrace); } } } private string GetProcessOwner(int processId) { string query = "Select * From Win32_Process Where ProcessID = " + processId; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection processList = searcher.Get(); foreach (ManagementObject obj in processList) { string[] argList = new string[] { string.Empty, string.Empty }; int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)); if (returnVal == 0) { return argList[1] + "\\" + argList[0]; } } return "NO OWNER"; }

September 8, 2016

Element Location Using XPath Axis Part 2

In previous post, I have mentioned different ways of identifying web elements using XPath . Very often , we will have to identify child elements while automating using selenium. Let us consider below example . This is an HTML layout of table <table id=table1 style="width:100%"> <tr> <td>John</th> <td>Smith</th> <td>50</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table> Assuming we need to iterate across all the rows to identify which row have the name “Eve” and then do some action on that row . This can be achieved by below ...

September 7, 2016

How to take screenshots with Selenium in C#

Very frequently testers will meet a situation where they need to take screenshot of webpage they are testing , either for base line or as a proof of test result. This is same with automated testing . Even though automated test cases have their own of way of publishing test results, it is always desirable to keep a proof of result.. Screenshot come to help in this regards. In this blog post , I will explain , how to take a screenshot with Selenium Web Driver with C#. In future I will add another couple of post to explain , how to consolidate the screenshots into a PDF document. ...

September 6, 2016 · Aby George A

Element location using XPath Axis

During testing we will sometimes come up to situations where developers are not following best practises for testability . We will frequently come up situations where elements doesn’t have any unique identifiable property. XPath axis comes to help in those situations. We can identify elements using various XPath Properties List of various XPath Axis are available in https://developer.mozilla.org/en-US/docs/Web/XPath/Axes If you have well-defined properties to identify the element, use them as your locator. Please read locator strategy Using XPath and Other Parameters ...

September 3, 2016 · Aby George A

Element Location using XPath

XPath is XML query language which can be used for selecting nodes in XML. Hence it can be used to identify elements from DOM since they are represented as XHTML documents. Selenium WebDriver also supports XPath for locating elements. They also help to look for elements in both direction and hence it is generally slow compared to all other locator strategy. We can use XPath with both absolute path and relative path. ...

August 30, 2016 · Aby George A

Nunit Assert

Assert.AreEqual vs Assert.AreSame Very frequently I use Assert.AreEqual and Assert.AreSame for doing assertions in the code. Below is high level difference between both. Assert.AreSame Assert.AreSame checks whether both comparing objects are exactly the same ( reference indicate same object in memory) .It is normally known as Reference Equality Assert.AreEqual Assert.AreEqual checks whether both objects contain same value. It is normally known as Value Equality. For primitive value types ( like int, bool) this is straight forward. But for other types ( especially user defined objects) , it is depends on how the type defines equality. ...

August 22, 2016

Identifying elements using Locators in Selenium

Locators are html properties of a web element , which can be considered as an address of the element. An element will have various html properties. We can use Firebug extension or Chrome dev tools to identify different locators of an element. Selenium Web Driver provides two different methods for identifying html elements . _**FindElement **_for WebDriver and WebElement Class. When locating element matching specified criteria, it looks through DOM( Document Object Model) for matching element and return the first matching element. If there are no matching element, it will throw NoSuchElementFoundException ...

August 17, 2016 · Aby George A

Specflow - Sharing data between steps

In Specflow, Step definitions are global. So a scenario can have multiple step definitions which can be present in different classes. Sometimes, there arise a need to share the data between steps residing in different classes. How do we do it?? There are multiple ways to do it Context Injection Feature Context Scenario Context Let us look into more details about how to store and retrieve data using Scenario Context . ...

August 1, 2016 · Aby George A