A bit of trouble with uploading files

213 views
Skip to first unread message

mugursm...@gmail.com

unread,
Aug 30, 2017, 10:16:11 AM8/30/17
to Serenity BDD Users Group
Hello,

I read about 8.7. Uploading files from the Serenity Reference Manual and I am not sure I am using the method described there as I should seeing as how my test is not behaving as I expect it too.

I have the following element that opens a browse for file window

@FindBy(id="importBtn")
private WebElementFacade modalImportButton;

and the method

public void loadFileFrom(String filename){
upload(filename).to(modalImportButton);
}

used in the step

@Step
public void load_file_from_location(String filename){
nomenclatorClientiPage.loadFileFrom(filename);
}

which is then used in the test
nomenclatorClientiSteps.load_file_from_location("C:\\SmartBill\\Serenity Auto\\sbc-tests\\src\\test\\java\\smartbill\\automation\\com\\utils\\test documents\\Clienti.xls");


When I run the test, all that happens is that the page behaves as if the modalImportButton is pressed, the browse for file window is opened and it remains like that. Also worth mentioning is that in the background I get taken to another page.

In the serenity reference manual, under the uploading files sections there was also this

    public NewCompanyPage(WebDriver driver) {
        super(driver);
    }

but I am unsure if I need it too (I had code errors when I tried to use it so I do not know...)


Thanks in advance,
Mugur

John Smart

unread,
Aug 30, 2017, 2:27:01 PM8/30/17
to mugursm...@gmail.com, Serenity BDD Users Group
What HTML type is the button?

--
You received this message because you are subscribed to the Google Groups "Serenity BDD Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thucydides-users+unsubscribe@googlegroups.com.
To post to this group, send email to thucydides-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
___________________________________________________
John Smart | Wakaleo Consulting  |  +44 7398 832273
Making smart teams collaborate better
http://johnfergusonsmart.com  |  john....@wakaleo.com
___________________________________________________

We love breaking down silos and helping smart teams collaborate better! Ask about our tailored on-site workshops in Agile Product Planning, BDD Requirements Discovery,  BDD, TDD and Clean Coding, and Advanced BDD Test Automation.

Need some help with Serenity BDD? Check out our Serenity BDD training and support packages here.
___________________________________________________

mugursm...@gmail.com

unread,
Aug 31, 2017, 2:58:04 AM8/31/17
to Serenity BDD Users Group, mugursm...@gmail.com
I also asked one of our developers and it appears I was sending it to a button when in fact I should be sending it to an input of type file but it still doesn't work. The dev suggested I might have to submit the form after uploading the file, but the method described in the Serenity manual doesn't seem to do that.

So basically I was trying to use the method on importBtn from this html:

<button type="reset" id="importBtn" class="btn btn-secondary" name="">
                        <i class="fa fa-upload"></i>
                        Incarca fisier
                    </button>

and now I am trying it for id_Filedata from this html:

<input name="Filedata" id="id_Filedata" type="file">

Worth noting is that the id_Filedata appears to be hidden and located inside an iframe which is also hidden

<iframe id="upload_frame" name="upload_frame" src="/core/importa_clienti/" width="500" height="60"></iframe>

So I've tried writing the method as follows (where uploadIFrame = "upload_frame"):

public void loadFileFrom(String filename){
getDriver().switchTo().frame(uploadIFrame);
upload(filename).to(fileImportInputId);
getDriver().switchTo().defaultContent();
}

but it still fails so I used a try/catch to see what happens and I get: Element not available for uploadIFrame

public void loadFileFrom(String filename){
try {
getDriver().switchTo().frame(uploadIFrame);
}catch (Exception e) {
System.out.println(e);
}
upload(filename).to(fileImportInputId);
getDriver().switchTo().defaultContent();
}


mugursm...@gmail.com

unread,
Sep 5, 2017, 10:02:50 AM9/5/17
to Serenity BDD Users Group, mugursm...@gmail.com
Hello, any help with my last reply, please?

mugursm...@gmail.com

unread,
Sep 6, 2017, 10:27:19 AM9/6/17
to Serenity BDD Users Group, mugursm...@gmail.com
Managed to find a solution with help from Dev and further digging in the reference manual:

public WebElement jQueryFind(String selector) {
return (WebElement) evaluateJavascript("return $('" + selector + "').get(0)");
}

public void loadFileFrom(String filename){
WebElement frame = jQueryFind("#upload_frame");
getDriver().switchTo().frame(frame);
WebElement fileInput = jQueryFind("#id_Filedata");
upload(filename).to(fileInput);
getDriver().switchTo().defaultContent();
}


Thanks and I hope it will help someone else in the future,
Mugur

SeleniumWhat?

unread,
Sep 6, 2017, 10:06:08 PM9/6/17
to Serenity BDD Users Group
@mugursm!

Nice solution.  I did not even know the method "upload(<filename>)" exists :-).  Cool deal! .
Thanks for sharing the tips.
J.N

SeleniumWhat?

unread,
Sep 6, 2017, 10:27:50 PM9/6/17
to Serenity BDD Users Group
@mugursm!
  
Questions for you about the file and its location.  In the Manual, John advised that your put the file in the "class path", but I'm not sure what he meant.  Since you have successfully managed to get this to work, I'd like to ask you to clarify what John meant:
1 - Where did you put your target file?  If you placed the file in the "class path" as John suggested, how did you do that? (step-by-step instruction is very helpful).
2 - Based on your code, I can see that you pass the "filename" string to the "loadFileFrom()" method.  How does this method knows the location of the file?  Or the "filename" string does actually contain the absolute directory path and filename as well?
3 - Is the upload screen a window pop-up?  Or is it  a component of html?  It seems to me that your code only works where the upload screen is part of html....Am I correct? If the upload screen is a window pop-up,  what is your solution?
  Thanks!
J.N

mugursm...@gmail.com

unread,
Sep 7, 2017, 2:15:25 AM9/7/17
to Serenity BDD Users Group
Hello J.N,

I am not sure if I did exactly what John advised in the manual concerning the file, but regardless, here is what I did:
  • in the src -> test -> java -> <project> -> automation -> com (folder where we have the features, pages, steps.serenity folders) I also have a utils folder in which I created another folder named test documents and there I stored the file (so that I can deploy it on github and then when Team City runs the tests it can always find the file on the server); so in short this is the folder structure: src -> test -> java -> <project folder> -> automation -> com -> utils -> test documents -> Clienti.xls file
  • in my previous comment I pasted the methods I use in the nomenclatorClientiPage page
  • here is how the step looks in the steps page:

  • @Step
    public void load_file_from_location(String filename){
    nomenclatorClientiPage.loadFileFrom(filename);
    }
  • here is how the step looks in the tests page:
    nomenclatorClientiSteps.load_file_from_location("C:\\...\\src\\test\\java\\project\\automation\\com\\utils\\test documents\\Clienti.xls");
  • As for the upload screen itself: when the user uploads the file by hand they have to click a button (let's call it button 1), then a modal opens (or pop-up is correct too I guess), in the modal they have to click another button (button 2) which opens a browse for file window (and Selenium cannot interact with it because it is a windows window or browser window--I believe both terms are correct) ; when I run the auto-tests I have to click button 1 and then when the modal opens I use the method described in my previous comment in order to "upload" the path to the hidden-within-an-iframe file manager (so I no longer press button 2 and the browse for file window does not open)
Hope I explained well enough.

Cheers,
Mugur

SeleniumWhat?

unread,
Sep 7, 2017, 7:19:16 PM9/7/17
to Serenity BDD Users Group
Mugur,
  Thanks for your reply on my inquiries.  I got the first answer, but the second one is till bit confusing... Why goes your automation requires human interaction when you run the test?  Does it defeat the purpose of "automation"?
  J.N

mugursm...@gmail.com

unread,
Sep 8, 2017, 1:34:15 AM9/8/17
to Serenity BDD Users Group
As for the upload screen itself:
  • Not automated: when the user uploads the file by hand they have to click a button (let's call it button 1), then a modal opens (or pop-up is correct too I guess), in the modal they have to click another button (button 2) which opens a browse for file window (Selenium cannot interact with windows of this type because they are windows window or browser window--I believe both terms are correct)
  • Automated: when I run the auto-tests the routine (the program/selenium/serenity etc) clicks button 1 and then when the modal opens it uses the method described in my previous comment in order to "upload" the path to the hidden-within-an-iframe file manager (so it no longer has to press button 2 as well, and the browse for file window does not open... the file will be auto-uploaded)

I've rewrote the part that might have given you the impression that I use manual testing combined with auto-testing... I never do that because indeed it would defeat the purpose of automation ;)


Hope everything is clear now.


Cheers,

Mugur

SeleniumWhat?

unread,
Sep 8, 2017, 9:11:17 AM9/8/17
to Serenity BDD Users Group
@Mugur..
  Now that explains it :-).  Sorry for misunderstanding your descriptions!
  Thanks
  J.N
Reply all
Reply to author
Forward
0 new messages