Sunday 7 June 2015

Upload File Using Selenium WebdDriver in Java using AutoIt

Prerequisite:


  1. Eclipse IDE
  2. Point Position Tool
  3. Autoit V3
  4. AutoIt window info


Selenium is not able to identify the element or "Browse" button to click.
Example:
                 <input name="BROWSECV" id="browsecv" size="37"> </input>
or

 you can go to below link and inspect the browse button.
            Link:      http://my.naukri.com/manager/createacc2.php?othersrcp=11499&wExp=N

or see the screen shot to know more


To handle such situation in Selenium we have to use third Party Tools such as Point Position, Autoit etc...


Below is the example to handle above "Browse" button from above link.

First we can create a file in Autoit v3 to handle browse button.
open the SCiETE Script Editor
write the below code into it.

Code:
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1","D:\Avinash\FileUpload.txt")
ControlClick("Open","","Button1")


  • Save above file named as - NaukriFileUpload.au3
  • Go to file stored location and compile it.
  • it will produces the NaukriFileUpload.exe


The parameters name "Open", "Edit1" , "Button1" in above code has been inspect it by using Autoit Window info tool.
We drag The option "Finder tool" from Autoit window info to "File Name" text box then "open" button to read Title. class &Instance parameter  information of each control on OpenfileDialog Window popup.
because we need these parameter information to AutoIt functions such as ControlFocus, ControlSettext etc...

See below screen shot to know more details:




Next:

Point Position:
     we use point position tool to identify the X & Y coordinate of Browse button from Naukri.com or above link.
then we will click it on that X,Y coordinate by using javas robot class.

See the screen shot, how to use Point Position tool



Next:   

    Open Eclipse IDE
    Create a Java project
    Create a package under Src folder.
    Create a Testcase
write the below code in that TestCase

package fileupload;
import static org.junit.Assert.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUpload 
{
WebDriver driver = null;
WebElement element = null;
JavascriptExecutor js = null;

@Before
public void setUp() throws Exception 
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
}

@Test
public void test() throws AWTException, InterruptedException, IOException 
{
driver.get("http://my.naukri.com/manager/createacc2.php?othersrcp=5424&wExp=N&id=");
Thread.sleep(5000);
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,700)");

int x = 537;
int y = 331;

Robot r = new Robot();

r.mouseMove(x, y);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

r.mouseMove(545, 345);
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(3000);
Runtime.getRuntime().exec("G:\\Autoit\\NaukriFileUpload.exe");

}

@After
public void tearDown() throws Exception 
{
driver.close();
element = null;
}

}



See Also Below Links:



















17 comments:

  1. I can reduce the image size using html but this concept is really new to me.These coding also explain clearly.Thank yo friends..

    Loadrunner Training in Chennai

    ReplyDelete
  2. This post is really nice and informative. The explanation given is really comprehensive and informative..
    SAS Training In Chennai

    ReplyDelete
  3. Greens Technology's. the leading software Training & placement centre Chennai & (Adyar)
    the best sas training in chennai

    ReplyDelete
  4. Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.
    angularjs Training in marathahalli

    angularjs interview questions and answers

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs online Training

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Java training in Bangalore | Java training in Kalyan nagar

    Java training in Bangalore | Java training in Kalyan nagar

    Java training in Bangalore | Java training in Jaya nagar


    ReplyDelete
  6. I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete
  7. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  8. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  9. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  10. thank you for sharing this

    BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

    https://www.acte.in/angular-js-training-in-chennai
    https://www.acte.in/angular-js-training-in-annanagar
    https://www.acte.in/angular-js-training-in-omr
    https://www.acte.in/angular-js-training-in-porur
    https://www.acte.in/angular-js-training-in-tambaram
    https://www.acte.in/angular-js-training-in-velachery

    ReplyDelete
  11. Your technical information related with java programming is very useful and interesting. Also share updated details about java in your website. Thanks for sharing this article.
    Angular js Training in Chennai

    Angular js Training in Velachery

    Angular js Training in Tambaram

    Angular js Training in Porur

    Angular js Training in Omr

    Angular js Training in Annanagar


    ReplyDelete