0

I'm trying to build some automated test for a mailbox application and I'm trying to attach a file. I've read all the documentation from previous post and was able to come up with this:

public void I_attach_a_file_that_exceeds_the_limit() throws Throwable {

    WebElement attachFile = driver.findElement(By.id("attachment"));
    File f = new File("C:\\coop-provider-swm-specs\\src\\test\\resources\\attachments\\20481kb.txt");
    attachFile.sendKeys(f.getCanonicalPath());

}

The problem with this is that the file that it attaches is not the real file. The file that is attached is blank (not sure how that works). The file that I need to attach is a big file and I need to do this in order the authenticate that the user does not exceed the limit for attachments that is allowed.

6
  • What happens when you try generating this code by recording a macro? Commented Feb 10, 2014 at 20:44
  • Why did you create the File object if you only needed the path? Commented Feb 10, 2014 at 20:52
  • admdrew, these automated tests are developed using java for the selenium webdriver, I don't believe recording a macro will apply (correct me if I'm wrong). Commented Feb 10, 2014 at 21:02
  • BarbiePylon the file object was to point to the new file, but I believe I understand where you are coming from, feel free to elaborate. Commented Feb 10, 2014 at 21:04
  • Well, if you're pointing to a file that is already on the file system then it doesn't seem like you need to create a new File especially if you're just grabbing the path anyway. Just put the path in the sendKeys() method...If you're creating a new File period then you need to write something to it first otherwise it's going to be blank. Commented Feb 10, 2014 at 21:24

1 Answer 1

1

Change:

attachFile.sendKeys(f.getCanonicalPath());

To:

attachFile.sendKeys(f.getCanonicalPath()).submit();
Sign up to request clarification or add additional context in comments.

4 Comments

I was able to attach the file using this line: attachFile.sendKeys("C:\\workspace-ecweb\\coop-provider-swm-specs\\src\\main\\resources\\attachments\\20481kb.txt"); but I was trying to resolve a relative path and what you suggested above isn't working.
So why don't you simply use getAbsolutePath instead of getCanonicalPath then?
The tests will be distributed to other users on a server who will not necessarily have a c:// drive.
Calling getAbsolutePath will just yield a different path in that case, so where is the problem then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.