Shane Cleary

On t'internet!

Uploading Document Selenium

Uploading Document Selenium C# using AutoIt

Building the File Upload AutoIt Script

We need a third party tool to write to windows dialog box. In this case we use AUTOIT

https://www.autoitscript.com/site/autoit/

1. Download autoit

2. Create a file (Notepad++) with the extension of .au3, then right click on Edit Script

3. Create the file like below


Steps

a. Create a loop which will wait for file upload dialog box

b. Bring upload dialog in focus

c. Supply the file along with its path in the upload dialog

d. Click on upload button


AutoIt Script

$count = 0
Sleep(3000)
While $count <> 10
   $firefox = WinActivate("File Upload")
   $chrome = WinActivate("Open")
   If $firefox <> 0 Then
	  ControlFocus("File Upload","","Edit1")
	  Sleep(500)
	  ControlSetText("File Upload","","Edit1",$CmdLine[1])
	  Sleep(1500)
	  ControlClick("File Upload","","Button1")
	  Exit
   ElseIf $chrome <> 0 Then
	  ControlFocus("Open","","Edit1")
	  Sleep(500)
	  ControlSetText("Open","","Edit1",$CmdLine[1])
	  Sleep(1500)
	  ControlClick("Open","","Button1")
	  Exit
   EndIf
   Sleep(1000)
   $count  = $count + 1
WEnd

		

INFO:

//Counter to set the number of times the loop should execute

$count = 0

//Delay for the dialog box to open

Sleep(3000)

//Loop will run 10 times

While $count <> 10 Then

//wait for the window to be in focus (WinActivate – automatically brings the window in focus)

$chrome = WinActivate(“Open”)

Open AutoIt Window Info and use the Finder Tool to find title. Title in entered in the WinActivate.


SpecFlow

//Verify chrome has focused on the element

If $chrome <> 0 Then

//Next bring the textbox in the dialog in focus

ControlFocus(“Open”,””,”Edit1”)

//Next provide the text, supplying the argument from the commandline

ControlSetText(“Open”,””,”Edit1”,$CmdLine[1])

//Clicking the button up upload

//a[contains(text(),'Click Me')]

ControlClick(“Open”,””,”Button1”)

Exit


4. When done click Tools -> Build. This will generate an .exe file.


Executing the File Upload Script using Webdriver Api

How to trigger AutoIt script from Webdriver API. In order to trigger executable we need to create a process in our test method

Create Object to ProcessStartInfo class

Create a new class file to upload document.



[TestMethod]
        public void TestUpload()
        {
            //Wait for upload button and click
            WebDriverWait waitForElement = new WebDriverWait(Driver, TimeSpan.FromSeconds(4));
            waitForElement.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@onclick='OpenAddDocumentReview()']")));
            UploadDocumentLink.Click();

            //Wait for Upload Modal and enter title
            Thread.Sleep(2000);
            TitleField.SendKeys("Blank Test Document");
       
            BrowseButton.Click();

            //Begin of script
            ProcessStartInfo processinfo = new ProcessStartInfo();
            processinfo.FileName = @"C:\Users\ShaneCleary\Documents\FileUpload2.exe";
            processinfo.Arguments = @"C:\Users\ShaneCleary\Documents\TestDocument.docx";

            Process process = Process.Start(processinfo);
            process.WaitForExit();
            process.Close();

            Thread.Sleep(2000);
            //End of script

            //Select Document type
            TypeOfDocumentSelect2.Click();
            Thread.Sleep(500);
            TypeOfDocumentSelect2.FindElement(By.XPath("//span[contains(text(), 'Acceptable Use Policy')]")).Click();

            //Save Modal
            UploadButton.Click();
            
        }


INFO:

//Create new and set the configuration

ProcessStartInfo processinfo = new ProcessStartInfo();

//Set the path of the executable

processinfo.FileName = @”C:\...”

//Set the argument as the file you want to upload

//Starts the executable

Process process = Process.Start(processinfo);

//Make sure that it waits until the executable has finished

Process.WaitForExit();

//Free the resource

Process.Close();

//Await to see the file upload

Thread.Sleep(3000);


Method to upload documents

See code below:



[TestMethod]
        public void TestUpload2(string document)
        {

            ProcessStartInfo processinfo = new ProcessStartInfo();

            processinfo.FileName = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Resources\\FileUpload2.exe";

            //Gets the file path of file to upload and then replaces all spaces with quotes on either end (" ")
            //.Arguments treats spaces (without quotes) as seperate arguments
            string filePath = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\Resources\\" + document + "";    
            string updatedFilePath = filePath.Replace(" ", "\" \"");

            processinfo.Arguments = updatedFilePath; 

            Process process = Process.Start(processinfo);
            process.WaitForExit();
            process.Close();

            Thread.Sleep(500);

        }

Footer