eclipse - I am unable to upload attachment using SeleniumWebDriver -



eclipse - I am unable to upload attachment using SeleniumWebDriver -

this code

driver.findelement(by.id("ctl00_contentplaceholder1_cbpassociationnew_panelassndetailadd_add_photo_browse0")).click(); thread.sleep(2000); stringselection ss= new stringselection("c:\\users\\ns10\\desktop\\download.jpg"); toolkit.getdefaulttoolkit().getsystemclipboard().setcontents(ss, ss); robot robo=new robot(); robo.delay(1000); robo.keypress(keyevent.vk_enter); robo.keyrelease(keyevent.vk_enter); robo.keypress(keyevent.vk_control); robo.keypress(keyevent.vk_v); robo.keyrelease(keyevent.vk_v); robo.keyrelease(keyevent.vk_control); robo.keypress(keyevent.vk_enter); robo.keyrelease(keyevent.vk_enter);

when run above code in eclipse, clicks on file upload; windows pop comes doesn't select file mentioned. idle.

could please help me this.

if possible seek first,

driver.findelement(by.id("ctl00_contentplaceholder1_cbpassociationnew_panelassndetailadd_add_photo_browse0")).sendkeys("c:\\users\\ns10\\desktop\\download.jpg");

this noted way upload file via selenium webdriver of now, don't have bother clicking on file upload button , using robot class upload image. sending path of file upload accomplish upload.

              if still doesn't work, below alternative way using robot , sikuli (combined) accomplish task:-

problem:-

sometimes, robot class types "attachment_path" "file name" field of textbox doesn't presses 'enter key' thereafter file upload.

solution:-

to avert that, i've used sikuli click on open button, infact uploads file then; (note:- have take screenshot of open button in window's dialog box, comes after clicking on "file upload button".

first of download sikuli-api standalone jar. and, add together build path of project. then, add together below code file upload:-

//clicking on file upload button driver.findelement(by.id("ctl00_contentplaceholder1_cbpassociationnew_panelassndetailadd_add_photo_browse0")).click(); //copying path of file clipboard stringselection attachment = new stringselection("c:\\users\\ns10\\desktop\\download.jpg"); //path attachment toolkit.getdefaulttoolkit().getsystemclipboard().setcontents(attachment, null); //pasting contents of clipboard in field "file name" of window pop-up robot robot = new robot(); robot.keypress(keyevent.vk_control); robot.keypress(keyevent.vk_v); robot.keyrelease(keyevent.vk_v); robot.keyrelease(keyevent.vk_control); try{ screenregion screen = new desktopscreenregion(); //screen default screen part corresponds entire screen on default monitor. target target = new imagetarget(new file("<put here path open button's image>")); screenregion screen_web = screen.wait(target,5000); screen_web = screen.find(target); //screen_web holds screen part occupied "open" button found sikuli's image recognition engine. if(screen_web != null) { mouse mouse = new desktopmouse(); // create mouse object mouse.click(screen_web.getcenter()); // utilize mouse object click on center of target region, i.e., open button }else{ throw new throwable(); } }catch(throwable e){ system.err.println("the open file button wasn't clicked!!" + e.getmessage() + "\n---------------------"); }

code uploading in tiny pic.com :-

package pack_ads; import java.awt.awtexception; import java.awt.robot; import java.awt.toolkit; import java.awt.datatransfer.stringselection; import java.awt.event.keyevent; import java.io.file; 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.sikuli.api.desktopscreenregion; import org.sikuli.api.imagetarget; import org.sikuli.api.screenregion; import org.sikuli.api.target; import org.sikuli.api.robot.mouse; import org.sikuli.api.robot.desktop.desktopmouse; public class testing_tinypicupload { public static void main(string[] args) throws awtexception, interruptedexception { webdriver driver = new firefoxdriver(); //opens firefox browser instance driver.manage().timeouts().implicitlywait(30, timeunit.seconds); //timeout of 30 seconds given driver.manage().window().maximize(); //maximizing window //navigating tiny pic site driver.get("http://tinypic.com/"); //uploading photo driver.findelement(by.xpath("//input[@id='the_file']")).click(); //locating attach button //copying path of file clipboard stringselection photo = new stringselection("d:\\\\images\\default.jpg"); //putting path of image upload toolkit.getdefaulttoolkit().getsystemclipboard().setcontents(photo, null); //pasting contents of clipboard in field "file name" of window pop-up thread.sleep(5000); robot robot = new robot(); robot.keypress(keyevent.vk_control); robot.keypress(keyevent.vk_v); robot.keyrelease(keyevent.vk_v); robot.keyrelease(keyevent.vk_control); try{ screenregion screen = new desktopscreenregion(); //screen default screen part corresponds entire screen on default monitor. target target = new imagetarget(new file("tinyimages\\open_file.png")); //made folder in project tinyimages , set image of open_file in there. screenregion screen_web = screen.wait(target,5000); screen_web = screen.find(target); //screen_web holds screen part occupied "open" button found sikuli's image recognition engine. if(screen_web != null) { mouse mouse = new desktopmouse(); // create mouse object mouse.click(screen_web.getcenter()); // utilize mouse object click on center of target region, i.e., open button } else{ throw new throwable(); } system.out.println("attachment done.\n------------------------------\n"); }catch(throwable e){ system.err.println("the open file button wasn't clicked!!" + e.getmessage() + "\n------------------------------"); } } }

note: can download utilize image "open" button here. alter file path accordingly in code target target = new imagetarget(new file("tinyimages\\open_file.png"));

eclipse testing selenium-webdriver

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -