Question

I'm using Sahi for Test Automation of web application. I have to write a script for sahi for uploading a file. But unfortunately I don't know the way. Can anybody please help me?

Was it helpful?

Solution

File upload can be a complex thing depending upon any validations you do on the upload. For a starter, you can try out the following:

Synatx:

_setFile(element, filePath [, actionURL])

eg: _setFile(_file("id"), "C:\abc\efg.jpg", "formSubmit.jsp");

If there are javascript validations on the file field, you can try this hack. Before submitting the file, change the field’s type to “text”, and then set its value. Eg.

// set the file
_setFile(_file("file"), "scripts/demo/uploadme.txt");
// Change the "type" attribute of file field
if (_isIE()){
_call(_file("file").outerHTML = _file("file").outerHTML.replace(/type=['"]?file['"]?/, "type=text"));
}else{
_call(_file("file").type = "text");
}
// Set the value into the textbox
_setValue(_textbox("file"), "scripts/demo/uploadme.txt");

This works for most of the cases. If you still get any error, you can post it here.

Thanks, Vivek

OTHER TIPS

You can use the following

_setFile(_file("id"), "C:\\abc\\efg.jpg");

Not sure if you need anything more complex?

Note that as of Sahi 4.3, there is a _setFile2 function that automatically handles js validations and does this input type conversion.

I've solved using the function setFile2, with internally changes the type of field to text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top