1.

How to upload a file?

Answer»

There are two ways you can upload file in selenium WebDriver, one is using SendKey method when there is text box AVAILABLE to enter the file’s name and second is using AutoIT Script tool when there is no text box to enter the file’s name.

  • Upload file using SenKey() method –
    • First, LOCATE the text box and then using SendKey() set the file path and click on the SUBMIT button.

Syntax is –

//locate browse button WebElement browse =driver.findElement(By.id("id value")); //pass the path of the file to be uploaded using Sendkeys method browse.sendKeys("D:\\SoftwareTestingMaterial\\UploadFile.txt"); //click on submit button driver.findelement(By.id(“id value”)).click;
  • Upload using open-source AutoIT Script tool
    • First, download and install the AutoIT Script tool (http://www.autoitscript.com/site/autoit)
    • Open Autoit tool -> SciTE Script Editor
    • Paste below code in editor and save it with .exe extension say test.exe

WinWaitActive("File Upload"); Name of the file upload WINDOW (Windows Popup ame: File Upload)

Send("logo.jpg"); File name Send("{ENTER}");
  • Compile code and then build tools menu of AutoIT editor
  • Paste below code in Java editor and execute test
// open the Upload window using selenium driver.findElement(By.id("Id value")).click(); //WAIT for page to load Thread.sleep("20000"); Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "<filepath>\\test.exe"); // Give  path where the exe is saved.


Discussion

No Comment Found