I've been on some automation test project now and yesterday my supervisor told me to implement a test case. This test case is on Google's service and it requires me to upload an Excel file during the test.
I have achieved to open the Windows Dialog (it did take a while since Google did some crazy job on placing the elements and naming them) and copy my Excel file's path into the ClipBoard but I have to CTRL + V
it to opened dialog to make it work.
It seems like there used to be a function that can solve this problem, but it isn't available. In Java, using a thing called Robot works fine but I have to implement it in Visual Studio and using IKVM (somehow should help you use Java code in your c# code, but I couldn't make it work.) didn't work.
How can I use the keyboard while the test is running? This ultimate question is this simple but it is not that simple to find an answer. Can someone please help?
You handle file uploads a bit differently with selenium. If you want to use selenium, find the xpath or id for the file upload. Then use the below.
var value= "test.csv"; //in case you run tests with multiple files you can pass in just a file name.Just create a files dir and drop your test files in there.
The below will create: "c:\my\path\to\files\test.xls"
string filePath = string.Format(@"c:\my\path\to\files\{0}", value);
Then once you locate the path to the upload object, you send the file path directly to the upload call.
Click the import button to pop the upload iframe:
driver.FindElement(By.Id("ly0-layerview-import-link")).Click();
This is using an iFrame so you have to switch to it. The problem is the iframe id is dynamic so you need to find it first and get the id attribute and then switch to it.
var iframe = driver.FindElement(By.XPath("//iframe[contains(@src, 'picker')]")).GetAttribute("id");
You will need code to switch to the iframe - If you do not have it, just create the method.
driver.SwitchToIframe(iframe);
Then upload the file-
driver.FindElement(By.XPath("//input[@type='file']")).SendKeys(filePath);