ideas on how to verify a download

124 views
Skip to first unread message

David Lai

unread,
Mar 29, 2013, 8:26:34 PM3/29/13
to seleniu...@googlegroups.com
I'm trying to automate a page that has a download button.  Normally I would just get the href and do a curl call to validate the URL gets the expected contents.  However, in this case the file download is spawned by a JavaScript call.

The button in question, just looks like, <a title="Download" class="wd-button wd-button-active wd-button-link" id="download-button">Download</a>

Here's a link for reference.  http://wdrv.it/10hqGeg

I was wondering if you guys have any ideas on how I can work around it.  I'd like to get the URL of the download, so I can do a download and run some video/image comparison calls to verify the file is being served at the expected quality from the download link.

Thanks,


David Lai

unread,
Mar 29, 2013, 8:32:53 PM3/29/13
to seleniu...@googlegroups.com
This is what I tried earlier to get the download link from the source, 

        source = self.webdriver.page_source

        download_url = re.findall("\"(http[^\"]+action=download)\"", source)[0].replace("\\","")


This is able to get the download link for the first file, but when the presentation goes to the next file, the download link is updated using JavaScript, so using the page source will no longer work.

Manoj K

unread,
Mar 31, 2013, 2:10:44 PM3/31/13
to seleniu...@googlegroups.com

Mark Collin

unread,
Apr 1, 2013, 6:04:59 AM4/1/13
to seleniu...@googlegroups.com

Talk to your devs and find out what the button is doing. 

 

I suspect it’s performing a POST and the file that is downloaded is the response to the post.  If this is the case you just need to know what the POST criteria are and you can programmatically send a POST yourself.  I have an update to the file downloader code I originally wrote to allow you to select the request type (e.g. GET, POST, PUT, etc.) but I haven’t pushed it yet (I’ll update the thread when I do, the code is on another machine at the moment).

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/aVTStVrN66YJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

David Lai

unread,
Apr 1, 2013, 11:07:58 AM4/1/13
to seleniu...@googlegroups.com
Thanks for the suggestions so far.  More info..

I'm really trying to verify the asset the download button points to, so I can verify that paginating through different videos will serve the file from the correct CDN with the correct asset.  The asset ID i can get through the backend API call, however in this case I want to verify the download button itself to make sure everything is hooked up correctly and that the button is updated with the currently playing video.

As for the implementation, I've talked to the devs.  the button is getting the link url from backbonejs model.  Unfortunately, this model is in a private namespace, so I can't access it from the global context. I was wondering if it was possible to get a dump of the current click listener of the element using Selenium, then from there, figure out how to do some langage processing to extract the value.


Mark Collin

unread,
Apr 1, 2013, 1:07:36 PM4/1/13
to seleniu...@googlegroups.com

Getting hold of listeners is hard, you can do it in jQuery if the listener was registered in jQuery, here’s some groovy code(It should be easy enough to read if you understand Java)

 

I need to remember to turn it into proper Java and stick it on my blog.

 

To get all the listener’s registered on an element (only listeners that have been added using jQuery remember):

 

((JavascriptExecutor) driver).executeScript("return jQuery._data(jQuery(arguments[0]).get(0), 'events')", element) as Map<String, Object>

 

You can then turn this into an additional condition if you want to wait for listeners to be registered (not really applicable to your problem, but possibly useful none the less):

 

public static ExpectedCondition<Boolean> listenerIsRegisteredOnElement(final String listenerType, final WebElement element){

    new ExpectedCondition<Boolean>() {

        @Override

        Boolean apply(WebDriver driver) {

            Map<String, Object> registeredListeners = ((JavascriptExecutor) driver).executeScript("return jQuery._data(jQuery(arguments[0]).get(0), 'events')", element) as Map<String, Object>

            for (Map.Entry<String, Object> listener : registeredListeners.entrySet())

            {

                if(listener.getKey().equals(listenerType)){

                    return true

                }

            }

            return false

        }

    }

}

 

If you aren’t using jQuery to register listeners then all bets are off, there is no generic way to find out what listeners are applied to an element in JavaScript (if I’m wrong please let me know, but I haven’t found one yet, everywhere says it is not possible).

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/fNxrRk2e3VUJ.

David Lai

unread,
Apr 1, 2013, 1:11:44 PM4/1/13
to seleniu...@googlegroups.com
That particular page is done using YUI JS framework.  I'll look into seeing if YUI has something similar.

Nick

unread,
Apr 1, 2013, 6:36:42 PM4/1/13
to seleniu...@googlegroups.com
I've done download tests in different ways. The most general one is to setup firefox profile which downloads files to specific folder without showing any dialog to user. So you just need to click on download link and see if you have correct file downloaded to your folder.

Mark Collin

unread,
Apr 1, 2013, 6:59:55 PM4/1/13
to seleniu...@googlegroups.com

What do you actually do with that file?

How do you know the file has finished downloading?

How do you know the name of the file (especially if one with the expected name already exists, do you cater for this?)?

 

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Nick


Sent: 01 April 2013 23:37
To: seleniu...@googlegroups.com

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/qBeFUOIYwvsJ.

Nick

unread,
Apr 1, 2013, 10:42:49 PM4/1/13
to seleniu...@googlegroups.com
Mark, do we need to help everybody chewing their food?
or
Is it you who don't know how to answer to some of listed questions?

Mark Collin

unread,
Apr 2, 2013, 3:23:49 PM4/2/13
to seleniu...@googlegroups.com

I’m totally aware, that’s why I wrote this blog post:

 

http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

 

What I’m trying to do is make you think about what your test is actually validating if you just set up a FireFox profile to download everything to disk.  If your answers to the three questions I posed are:

 

1.       Nothing.

2.       I just hope it’s finished downloading when I access it.

3.       I assume it is the same as the name of the file in the href I clicked on.

 

Then I would suggest that actually doing the download in the first place is a completely pointless exercise that does not test anything and is only slowing your tests down and wasting disk space.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/eg2HNSZu6OwJ.

David Lai

unread,
Apr 2, 2013, 3:58:44 PM4/2/13
to seleniu...@googlegroups.com
Currently I'm punting on verifying the actual download link right now.  I have a test going that verifies the RSS feed which is pretty much the same data.  I'll have to come back around and revisit this later after I've cherry picked the lower hanging fruits.

Thanks for the help so far.

Nick

unread,
Apr 2, 2013, 8:30:56 PM4/2/13
to seleniu...@googlegroups.com
Tests surely validates that downloaded file is the same as you have on server. Otherwise that's useless test. You can also verify that file has correct name. Which file? New one :)

I would never employ person who can't answer listed questions correctly :) Incorrect answers means person don't understand how software works or (and) has problems with logic.

Sorry, didn't read your post, I can't agree with subject. People should test what they need to. If they need to verify download, let they do that, they just have to understand that they increase complexity of testing environment(and manage that correctly) by requirements of custom firefox profiles for that test.

Mark Collin

unread,
Apr 3, 2013, 1:35:29 AM4/3/13
to seleniu...@googlegroups.com

So my next questions are:

 

·         How are you validating that the file is the same as the one on the server?

·         Why does the filename matter?

·         How do you know you have downloaded a new file?

 

Before disagreeing with the blog post you should read it.  It doesn’t say that you should never do download tests, it questions why you are doing them and tries to get you to think about what your tests are actually testing.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/9XkGKsEI6a0J.

Nick

unread,
Apr 3, 2013, 1:57:08 AM4/3/13
to seleniu...@googlegroups.com
Regarding new questions, they are not specific to Selenium, they can be easily solved with quite simple algorithms which someone would need to implement if they want to test download in a way proposed by me :) If they can't that mean that they need to train a bit more to be a good (testing automation) developers.

I tried to disagree with subject. I wasn't right. Sorry, I should have read article before. I will do that tonight.
Reply all
Reply to author
Forward
0 new messages