Wednesday 3 April 2013

Uploading a file using Autoit & WebDriver

I have been reading many mails from various selenium groups where people asking for solution on uploading files through desktop in case sendkeys is not working where the input element is not associated with the browse button since a window's pop up is opened which only accepts a stand-alone file.

Some lines from Auto-it website:


AutoIt v3 is a 3rd party freeware BASIC-like scripting language tool designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). 


Here you need to integrate Auto-it to make it work. Auto-it comes with various components and a complete workflow:

Steps to follow:

Step 1: Download Auto-it from here
Step 2: You will get an installer, just double click and run the executable file.
Step 3: Following components will be installed on your machine


             SciTe Script Editor - Use for writing scripts & compiling programs.
             AutoIT window info - Locate class & property of the window's based element etc.





















Step 4: Write the script using the SciTe Script Editor. Copy and paste the below script in the editor and save it with .au3 format and then compile it by clicking on Tools menu --> Compile




Opt("MustDeclareVars", 1);0=no, 1=require pre-declare

Main()


Func Main()
  
    Local Const $dialogTitle = $CmdLine[2]
    Local Const $timeout = 5

    Local $windowFound = WinWait($dialogTitle, "", $timeout)
    
   
    $windowFound = WinWait($dialogTitle, "", $timeout)
    Local $windowHandle

    If $windowFound Then
       
        $windowHandle = WinGetHandle("[LAST]")
        WinActivate($windowHandle)
   
        ControlSetText($windowHandle, "", "[CLASS:Edit; INSTANCE:1]", $CmdLine[1])
        ControlClick($windowHandle, "", "[CLASS:Button; TEXT:&Open]")        
        
    Else
     
        MsgBox(0, "", "Could not find window.")
        Exit(1)
     EndIf
EndFunc    


Step 5: After compile the script you will get a .exe file that is created on the same location where the script is placed



import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class testImageUpload {



public static void main(String[] args) throws IOException {

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.imageshack.us/");

driver.findElement(By.xpath("//*[@id='SWFUpload_0']")).click();

Process process = new ProcessBuilder("C:\\Users\\selenium\\auto.exe",
                "C:\\Users\\selenium\\test.png", "Open").start();   


}

}

Image upload example used for demonstration in this article is at:
http://imageshack.us/




27 comments:

  1. Hi All,
    By using webdriver i am implementing mouse over functionality on my website-tjc.co.uk but it is not working correctly.can anyone help me out.
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;





    public class testMouseovermenu {

    public static void main (String[]args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.tjc.co.uk");
    driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);

    WebElement topmenu = driver.findElement(By.xpath("//*[@id='topnav']"));
    WebElement menu = driver.findElement(By.xpath("//*[@id='topnav']/li[2]/a"));
    Thread.sleep(5000);
    Actions action = new Actions(driver);
    action.moveToElement(menu).perform();

    driver.findElement(By.xpath("//*[@id='topnav']/li[2]/div/ul[1]/li[3]/a")).click();




    }
    }
    This is my code.can anyone tell me where i am wrong.

    ReplyDelete
  2. Hi all,
    I want to run my code on ie9,
    I downloaded the file what next i have to do.please tell me

    Thanks
    Tarun Mehrotra

    ReplyDelete
  3. Try this. It works fine all the time for me:

    System.setProperty("webdriver.ie.driver", "c:/softwares/IEDriverServer.exe");

    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://www.tjc.co.uk");
    driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);

    WebElement menu = driver.findElement(By.xpath("//*[@id='topnav']/li[2]/a"));

    Actions action = new Actions(driver);
    action.moveToElement(menu).perform();

    driver.findElement(By.xpath("//*[@id='topnav']/li[2]/div/ul[1]/li[3]/a")).click();

    ReplyDelete
  4. I am not able to put "if else" condition for Drop down.
    url :https://www.thejewellerychannel.tv/register.aspx
    (Use Drop down of Where did you hear about us)

    suppose i have a dorp down box with multiple option of City name like : delhi,noida and other . here if we choose delhi and noida option in drop down than it move for submit button or when we choose "other" option in drop down then it move on text box for city name

    how can solve this prob with RC by using Xpath

    ReplyDelete
  5. Hi all,
    can anybody help me how to write in google search using webdriver.i tried different ways by xpath ,by name.but not getting any sloution

    Thanks
    Tarun mehrotra

    ReplyDelete
  6. Hi All,
    can anyone tell me how can i find number of pages using webdriver.


    Thanks
    Tarun Mehrotra

    ReplyDelete
  7. Hi All,
    I am getting error in this code.
    I have imported all the jars.
    POi
    and excel jars still ia m not able to run.Can anyone tell me why i am getting error.

    .public static Collection getData(){

    if(excel == null){
    // load the Excel sheet
    excel = new Xls_Reader("D://seleniumtest/Test.xlsx");

    }
    String sheetName="Test";
    int rows = excel.getRowCount(sheetName); // Get Row Count
    int cols = excel.getColumnCount(sheetName); // Get Col Count
    Object data[][] = new Object[rows-1][cols]; //-1

    for(int rowNum = 2 ; rowNum <= rows ; rowNum++){ //2

    for(int colNum=0 ; colNum< cols; colNum++){
    data[rowNum-2][colNum]=excel.getCellData(sheetName, colNum, rowNum); //-2
    }
    }










    ReplyDelete
  8. Hi All,
    when i am running my code by using ant through command prompt at the time of run it is showing me this error"E:\selenium work\testjunit\build.xml:105: The for must inclu
    de junit.jar if not in Ant's own classpath" can anyone help me out why i am getting this error.

    ReplyDelete
  9. hi your code working fine for me
    thanks
    skype : subash.chanra.bose

    ReplyDelete
  10. Hi,
    If in run my script in firefox its working fine.Same script I run IE flickering Problem arises,can any body help me out.Iam using IE version8 and drivers IEDiverServer
    Regards,
    Lavanya

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. Hi,

    when i try this on my computer it worked,
    but when i run this on remote computer it failed with the error

    java.io.IOException: Cannot run program "C:\grid\test1.exe": error=2, No such file or directory

    please help

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. hi i do the same thing in python !!!! could you help me calling the autoit .exe file from python ???

    ReplyDelete
  15. Hi Raman,
    I used the same code which you have posted for file upload,it works fine till capturing path.After capturing the path its not selecting the given file,please let me know how to resolve this issue at the earliest.

    Thanks,
    Pavitra

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Oh, that's Great Post Thanks for sharing with us. This Big data hadoop training in Bangalore Platform helped me to learn advanced concepts of all the trending technologies out there in the current IT market. really worth trying them.

    ReplyDelete

  20. I have read your blog very useful information to everyone.Thanks for sharing and keep updating more.If you are looking for python course visit our website.
    Python Training Institute In Bangalore

    ReplyDelete
  21. Very helpful. Thanks for sharing it

    ReplyDelete
  22. Generally excellent review. I absolutely love this site. Much appreciated!tech updates

    ReplyDelete