webdriver: testing file download

rather than using complicated functions to try and test file downloads, we can use simple logic like if the download is successful, we should see title A and if download fails, we should see title B. Surely, our system wouldn’t spit out the same page title even if the file doesn’t exists. So using phpwebdriver for example, say I am trying to check if the download link is working in the thankyou page:


// thankyou page
$this->assertEquals(self::$session->element('xpath', '//td[@class="product-name"]/small/a')->text(), "Download file now");
// now click on the download file link.
 self::$session->element('xpath', '//td[@class="product-name"]/small/a')->click();
// wait for a 2 secs for the download now prompt.
sleep(2);
// if file exists, then I should see the "Order Received" as part of the page title header.
$this->assertRegExp('/Order Received/', self::$session->title());

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.