0
votes

I have an issue with one of my Selenium UI tests that is running on a Dynamics 365 portal. The test is very simple:

  1. It logs into the portal
  2. Navigates to a certain page
  3. Uses SendKeys to get the file from a specific location
  4. Clicks upload button

When I run this UI test I receive a message stating "Please only upload csv files". This is correct behavior in the event that a user is attempting to upload a different file type. However, in this scenario the file extension is correct and should successfully upload. If I manually go into the portal and upload the same file myself, it uploads without error.

Has anyone experienced a similar sort of issue with a D365 portal? Or any other portal/website alike?

Below is my code to click the upload button. I have used By XPath, Id, LinkText, etc., and it all works fine.

public static void BulkUploadBrowse()
{
    try
    {
        //Click upload button
        var browse = Driver.Instance.FindElement(By.CssSelector("#AttachFile"));
        browse.Click();
        Thread.Sleep(1000);
    }
    catch (Exception ex)
    {
        throw (ex);               
    }
}

Here is the logic for uploading the file:

public static void UploadFile()
{
    // Method 1 : File Upload Using Send Keys
    try
    {
        SendKeys.SendWait(@"C:\Users\Shea.Leonard\Desktop\Upload20190625161051.csv");
        Thread.Sleep(1500);

        SendKeys.SendWait(@"{ENTER}");
    }
    catch (Exception ex)
    {
        throw (ex);
    }
}

I have also attempted another method using the AutoITX3 Library:

//Method 2 : File Upload Using Auto IT || Leaving this here incase method 1 stops working.

AutoItX3 autoIt = new AutoItX3();
autoIt.WinActivate("Open");

autoIt.Send(@"F:\Users\adm_shea.leonard\Desktop\Upload20190625161051.csv");
Thread.Sleep(1000);
autoIt.Send("{ENTER}");

However, I still get the same error.

Based on this it should upload a file successfully. Not sure if this is an issue with Selenium WebDriver and the D365 Portal Add-on. The error message we receive, "Please only upload csv files", is specified in the Web Form configuration of the Dynamics portal.

That same file is valid because, as mentioned, I have been able to successfully upload it manually through the portal.

Any help would be much appreciated.

1

1 Answers

0
votes

The first thing I would try is to leave the .csv extension off your SendKeys.SendWait(@"C:\Users\Shea.Leonard\Desktop\Upload20190625161051.csv"); line of code.

So try it just like this:

SendKeys.SendWait(@"C:\Users\Shea.Leonard\Desktop\Upload20190625161051");

This is just a sanity test to make sure the .csv file extension isn't duplicated for any reason.

Another thing I would try, is running some Javascript to make your File Upload input element visible, like this:

var fileInputElement = Driver.FindElement(By.CssSelector("#AttachFile"));

// display the file input so we can send keys
(IJavaScriptExecutor)_driver.ExecuteScript("arguments[0].style.display = 'block';", fileInputElement);

fileInputElement.SendKeys(@"C:\Users\Shea.Leonard\Desktop\Upload20190625161051.csv");

If that doesn't work, I would try manually navigating to this page and right clicking on the file input element that you are sending keys to.

Using devtools, you can manually enter text into the file input element (just as WebDriver does) and you can see if the same issue occurs there. Once you do that comparison, you will have a better idea if the issue has to do with WebDriver, or with the Webpage itself.

Lastly....I would also experiment using different file path string formats. For example, you use (@"C:\Users\Shea.Leonard\Desktop\Upload20190625161051.csv"). I would also try using ("C:\\Users\\Shea.Leonard\\Desktop\\Upload20190625161051.csv");as a sanity test to make sure there's nothing wrong with the file path you are providing.

The fact that you are receiving an error message relating to the file .csv format leads me to believe you are on the right track with this. The most likely scenario here is that manually uploading the file & automating the file upload are sending different text strings to the UI. So experimenting with different text strings that you send to the file input may resolve your issue.