Wednesday, 8 February 2017

SELENIUM: How to Upload a file using AutoIT:



How to Upload a file using AutoIT:


Step 1:Download Auto-IT
Step 2:install Auto-IT in your machine
Step 3:write the AutoIT  script  in Auto IT SciTE editor
Step 4:save file with .au3 extension
Step 5:compile the AutoIT script ,if your machine is 32-bit then compile  with Compile Script(x86) and your machine is 64-bit then compile  with Compile Script(x64) ,then you will get .exe file.
Step 6: write  your  normal Test Script  java program and call AutoIT .exe file.

Ex: AutoIT script (i.e UploadFile.au3)
ControlFocus("File Upload" , "","Edit1")
ControlSetText("File Upload", "", "Edit1",$CmdLine[1])
ControlClick("File Upload","","Button1")

or
ControlFocus("File Upload" , "","Edit1")
ControlSetText("File Upload", "", "Edit1","C:\Users\ramesh\Desktop\TextFile.txt")
ControlClick("File Upload","","Button1")
  
after compilation we get  UploadFile.exe

Example:
package com.rameshsoft.rameshselenium;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class AutoItFileUploadDemo {
          @Test
          public void demo() throws InterruptedException, IOException
          {
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
                   WebDriver driver=new FirefoxDriver();
                   driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                   driver.manage().deleteAllCookies();                  
                   driver.get("file:///C:/Users/ramesh/Desktop/fileupload.html");
                   Thread.sleep(2000);            
                   driver.findElement(By.xpath("html/body/input")).click();                  
                   Runtime.getRuntime().exec("C:\\Users\\ramesh\\Desktop\\autoitCode.exe"+" "+"C:\\Users\\ramesh\\Desktop\\TextFile.txt");   
(or)

Process p=new ProcessBuilder("C:\\Users\\ramesh\\Desktop\\autoitCode.exe","C:\\Users\\ramesh\\Desktop\\textFile.txt","open").start();

                   //Runtime.getRuntime().exec("C:\\Users\\ramesh\\Desktop\\autoitCode.exe");

driver.quit();

}
}

No comments:

Post a Comment