Sunday 7 June 2015

Upload file in Selenium WebDriver in java using Robot Class


In this  example the selenium is not it self capable to handling the "Choose File" button which is located on link: http://my.naukri.com/manager/createacc2.php?othersrcp=11499&wExp=N

So we can use Point Position Tool to read the X & Y coordinate of "choose file" button. Then we will pass these X & Y coordinate to Robot Class in java & remaining  part will taken care by Robot class.

To Download Point Position toll click on: http://freewareapp.com/point-position_download/ this click.


Requirements:


  • Eclipse IDE
  • JUnit
  • Selenium Standlone_2.40.0_server.jar
  • Point Position Tool / Software

First we use the Point position Tool to compute the X & Y coordinate of "Choose file" button. Because  selenium WebDriver is not capable to handle this button or even identify. Because  its attribute type="file".

See the screenshot of it.


below is the screen shot for How to use the Point Position tool. To compute X & Y coordinate of choose File button on Naukri portal.

See Screen shot for It.

Next:

Open Eclipse IDE
Create a java Project
Create a package under the SRC Folder
Create a TestCase under the package
Write the below code in currently created TestCase.

Code:


package naukri;

import static org.junit.Assert.*;

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
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.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.server.browserlaunchers.FirefoxLauncher;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class FileUploadOnNaukri 
{
WebDriver driver = null;
WebElement element = null;
    
        Robot robot = null;
int x = 0;
int y= 0;
@Before
public void setUp()
{
File file = new File("G:\\Selenium\\All_Jars\\chromeDriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void testChooseFileButton() throws Exception
{
driver.get("http://my.naukri.com/manager/createacc2.php?othersrcp=11499&wExp=N");
Thread.sleep(5000);
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,700);");
robot = new Robot();
x = 529;  // X coordinates of choosefile control,Values read from Point Position tool 
y = 339;  // v coordinates of choosefile control,Values read from Point Position tool
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot = null;
Thread.sleep(5000);
//copy the file path in clipboard
StringSelection ss = new StringSelection("d:\\Avinash.jpg");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,null);
   
        robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER); //Press Enter key in file name text box
robot.keyRelease(KeyEvent.VK_ENTER); // Release Enter key
                robot.keyPress(KeyEvent.VK_CONTROL); // Press Control Key
                robot.keyPress(KeyEvent.VK_V);       // Press V- button i.e. paste
                robot.keyRelease(KeyEvent.VK_V);     // Release V- Key 
                robot.keyRelease(KeyEvent.VK_CONTROL); //Release Control Key 
                robot.keyPress(KeyEvent.VK_ENTER);     // Press final Enter Key
                robot.keyRelease(KeyEvent.VK_ENTER);   //Release Enter Key
        Thread.sleep(7000);
}
        @After
public void tearDown()
{
driver.close();
element = null;
}
}

1 comment: