0

When looking at this post: I got most of my solution but the VK_ENTER at the end did not take me back to the web page which leads to another issue of getting back into view of the page and not the cropper tool where the image is placed.

Can someone let me know what I need to do to get this to work?? Thanks!!

Here is the code:

public static void setClipboardData(String string) {
   StringSelection stringSelection = new StringSelection(string);
   Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
And that is what i do, just after opening the "open" window:

setClipboardData("C:\\path to file\\example.jpg");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Here is a link to a screen recording of how this is supposed to work(resizing and moving is not relevant to the issue)Upload profile picture]1

One of the issues is that the VK_ENTER is never done to leave file explorer and return to the web page. If I do it manually while the test is running the Save button is never found. Here is the error for that:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[12]/form/div/div[1]/button[1]"}
Command duration or timeout: 30.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_66'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0.4, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b7ceb493-af57-4290-a585-bc237e043848
*** Element info: {Using=xpath, value=/html/body/div[12]/form/div/div[1]/button[1]}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
    at groupPhotoUploadTest.groupPhotoUploadTest.main(groupPhotoUploadTest.java:54)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[12]/form/div/div[1]/button[1]"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_66'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Janet/AppData/Local/Temp/anonymous8445833340663492444webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10659)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Janet/AppData/Local/Temp/anonymous8445833340663492444webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:621)

I have tried the Actions object as well as JavascriptExecutor and neither seem to work.

Here is the complete code:

package groupPhotoUploadTest;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;


public class groupPhotoUploadTest {
  private static WebDriver driver;
  private static String baseUrl;

public static void main(String[] args) throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://loadtest1.staging.brio.viddler.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get(baseUrl + "/");
    driver.findElement(By.linkText("Login")).click();
    driver.findElement(By.name("email_address")).clear();
    driver.findElement(By.name("email_address")).sendKeys("jfrank@viddler.com");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("Annie0308!");
    driver.findElement(By.cssSelector("button.button")).click();
    driver.findElement(By.linkText("Load Test1")).click();
    driver.findElement(By.linkText("Manage Site")).click();
    driver.findElement(By.linkText("Manage")).click();
    driver.findElement(By.xpath("//div[@id='container']/div/table/tbody/tr[5]/td[6]/a")).click();
    driver.findElement(By.linkText("Upload Logo")).click();
    driver.findElement(By.name("image_file")).clear();
    driver.findElement(By.name("image_file")).click();     
//    driver.findElement(By.name("image_file")).sendKeys("C:\\Users\\Janet\\Pictures\\P8291157.JPG");
    setClipboardData("C:\\Users\\Janet\\Pictures\\P8291157.JPG");
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
 //   robot.delay(5000);
/*  WebElement saveLink = driver.findElement(By.xpath("/html/body/div[12]/form/div/div[1]/button[1]"));
    Actions actions = new Actions(driver);
    actions.moveToElement(saveLink);
    actions.click();
    actions.build().perform(); */
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    WebElement saveLink = driver.findElement(By.xpath("/html/body/div[12]/form/div/div[1]/button[1]"));
    jse.executeScript("arguments[0].click();", saveLink);
 //   driver.findElement(By.cssSelector("button.submit.button")).click();
  }

public static void setClipboardData(String string) {
       StringSelection stringSelection = new StringSelection(string);
       Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

  public void tearDown() throws Exception {
    driver.quit();
  }
}
Würgspaß
  • 4,660
  • 2
  • 25
  • 41
Janet
  • 251
  • 1
  • 2
  • 11
  • You are mixing `java.awt.Robot` and `org.openqa.selenium.WebDriver`. Those are two different approaches and mixing them is brittle code, obviously. Why don't you upload using plain WebDriver methods? Have a look at this: http://stackoverflow.com/questions/16896685/how-to-upload-files-using-selenium-webdriver-in-java – Würgspaß Jan 19 '16 at 13:59
  • did you tried with some Thread.sleep(3000) before and after enter key event? – murali selenium Jan 19 '16 at 15:26
  • Hey I looked at the link you provided and would like to try the autotoit tool. I am not sure what I need to put in for the upload window name though, If you watch the screen recording I sent you will see that there is not one (I think). Thanks!! – Janet Jan 19 '16 at 17:48
  • The video shows that the page does not have a textbox to place a file name. Therefore I have to open up Windows Explorer to get to a point where I can put a file name. I tried just sending the filename after clicking the upload button but that does not work. Please watch the link to the screen recording – Janet Jan 19 '16 at 17:53
  • Here is the link again: http://screencast.com/t/IgHJJcXa – Janet Jan 19 '16 at 17:55
  • The Thread.sleep worked. But I still need to find a way to get the focus back to the web page so that I can then click the save button. Thoughts?? – Janet Jan 19 '16 at 20:48

1 Answers1

0

Finally have it working. Here is the complete code. Had to use the cssSelector and not the xpath:

  Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER); 
    Thread.sleep(3000);
    robot.keyRelease(KeyEvent.VK_ENTER); 
    Thread.sleep(3000);
    Thread.sleep(5000);
    WebElement saveLink = driver.findElement(By.cssSelector("button.submit.button"));
    Actions actions = new Actions(driver);
    actions.moveToElement(saveLink);
    actions.click();
    actions.build().perform();  
    Thread.sleep(5000);
    WebElement submitLink = driver.findElement(By.cssSelector("input.button"));
    actions.moveToElement(submitLink);
    actions.click();
    actions.build().perform();  
Janet
  • 251
  • 1
  • 2
  • 11
  • Still having a slight issue. If I run a batch of groups to load every so often the VK_ENTER is not executed to upload the file and close file explorer. I need this to run in batch so any input would be appreciated. – Janet Jan 20 '16 at 20:45
  • I set this up to run in task manager but it does not seem to be working. I need it to run in the background. Is it because I am doing other things on the computer as it is running?? Because of the Robot object?? Should I run this when I am away from the computer?? – Janet Jan 21 '16 at 15:07