Tuesday, 21 July 2015

How to upload file using AutoIT and robot Keys

AutoIT:

1) Download autoIT and extract
2) In 'SciTE Script Editor' write the below code and save(save with .au3 format) in specific location.

ControlFocus ( "File Upload", "", "Edit1" )
Sleep(1000)
ControlSetText("File Upload", "", "Edit1","C:\Users\pabbu\Desktop\logo11w.png") (image/file location)
Sleep(2000)
ControlClick("File Upload", "","Button1");

3) Compile the script, and it will generate .exe application.

Eclips: write the below code in eclipse
@Test
public void uploadAutoIT() throws IOException
{
d.get("http://www.toolsqa.com/automation-practice-form/");
d.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
d.findElement(By.xpath("//*[@id='photo']")).click();
Runtime.getRuntime().exec("D:\\Automation_workspace\\Rought_importFindings\\export\\File_upload_1");
//give the path of .exe from step 3.
}

Output:
It will upload the file

Robot keys:

package All_Types_Run;

import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.*;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Upload_File {

@Test
public void Fileupload() throws AWTException, InterruptedException{

 WebDriver driver = new FirefoxDriver();
 driver.manage().window().maximize();
       
        // Specify the file location with extension
 StringSelection sel = new StringSelection("C:\\Users\\Desktop\\1.doc");

        // Copy to clipboard
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
 System.out.println("selection" +sel);

 driver.get("http://my.monsterindia.com/create_account.html");
 Thread.sleep(2000);


 JavascriptExecutor js = (JavascriptExecutor)driver;
 js.executeScript("scroll(0,350)");
 Thread.sleep(5000);
 driver.findElement(By.id("wordresume")).click();
 Thread.sleep(2000);
 System.out.println("Browse button clicked");

        // Create object of Robot class
 Robot robot = new Robot();
 Thread.sleep(1000);
      
        // Press Enter
 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);

        // Press CTRL+V
 robot.keyPress(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_V);
 robot.keyRelease(KeyEvent.VK_CONTROL);
 robot.keyRelease(KeyEvent.VK_V);
 Thread.sleep(1000);
       

 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);

}

}
++++++Another Example +++++
package abcpack;

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

public class FileUpload_AutoIT {
public WebDriver driver;

@BeforeMethod
public void setUp() throws Exception
{
    driver= new FirefoxDriver();
    driver.manage().window().maximize();
}
@Test
public void testFileupload() throws Exception
{
    driver.get("http://www.2shared.com/");
    driver.findElement(By.id("upField")).click();
    Thread.sleep(2000);
    Process p = new ProcessBuilder("F:\\Selenium_Scripts_July13\\AutoIt Examples\\FileUpload.exe").start();
    Thread.sleep(3000);
    driver.findElement(By.xpath("//input[@title='Upload file']")).click();
    Thread.sleep(5000);
}

@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}

++++++++++++
@Test
public void handlingWindowPopups() throws AWTException, InterruptedException, IOException
{
    String file="C:\\Users\\pravanjan.reddy\\Desktop\\stf.txt";
    driver.get("http://toolsqa.com/automation-practice-form/?firstname=&lastname=&sex=Female&photo=&continents=Asia&submit=");
    driver.findElement(By.xpath("//*[@id='photo']")).click();
   
    //way 1 using Robot class
     StringSelection ss = new StringSelection("C:\\Users\\pravanjan.reddy\\Desktop\\stf.txt");
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    //imitate mouse events like ENTER, CTRL+C, CTRL+V
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
}





No comments:

Post a Comment