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/




23 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 information which you provided is very much useful for us.It was very interesting and useful for qa online training.We also providing qa online training institute in USA.

    ReplyDelete
  17. Thanks for your informative post on selenium automation testing tool. Your article helped me a lot in understanding the future of automation testing tool and its career prospects. Selenium Training | Best Selenium training institute in Chennai

    ReplyDelete
  18. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers.
    Regards,
    Software testing training in chennai|testing training chennai|Software testing institutes in chennai

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

    ReplyDelete
  20. The blog gave me idea to Uploading a file using Autoit & WebDriver the steps discussed helped me to understand the process easily thanks for sharing the valuable post
    Selenium Training in Chennai

    ReplyDelete
  21. really you have posted an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
    selenium training in chennai

    ReplyDelete
  22. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  23. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
    Web Design Development Company
    Mobile App Development Company


    ReplyDelete