Use automated test framework selenium, batch screenshots

Source: Internet
Author: User

Recently, an automated e-mail system is being implemented in which Outlook clients can accept HTML messages.

However, because the report I do uses fusioncharts, the Fusioncharts framework is a JS chart library, all charts are rendered through JavaScript,

Because Outlook cannot support JS, you can only send a screenshot of the finished chart page into an HTML page.

The screenshot of the code is as follows:

From datetime import datetime import JSON import urllib2 to selenium import webdriver from selenium.common.exceptions I Mport timeoutexception from selenium.webdriver.support.wait import webdriverwait def web_spider (logdebug,url= "http:// Www.baidu.com ", enable_proxy=true,proxy_address= ' http://baidu.com:8080 '): # Logdebug=open (Logs_path, ' a ') proxy_h Andler = Urllib2. Proxyhandler ({"http": proxy_address}) Null_proxy_handler = Urllib2. Proxyhandler ({}) if Enable_proxy:opener = Urllib2.build_opener (Proxy_handler) else:opene R = Urllib2.build_opener (null_proxy_handler) content= "" Try:urllib2.install_opener (opener) Hea DERs = {' user-agent ': ' mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6 '} req = Urllib2.
        Request (url =url, headers = headers) # F=open ("Email.txt", ' w+ ') content=urllib2.urlopen (req). Read () Content.decode ("Utf-8", ' ignore '). Encode (' UTF-8 ') # f.write (content) # f.close () except Exception,e:logdebug.write ("%s:web spider ex ception,%s\n "% (DateTime.Now (). Strftime ('%y%m%d-%h%m%s '), e)) Pass return content def get_screenshot ( Browser,logdebug,url,filename= ' Screenshot.png '): Flag=false try:browser.get (URL) Wait=webdriverwa 
                            It (browser,60) waitflag=false try:waitflag=wait.until (Lambda browser: ' Completed ' in browser.find_element_by_id (' Load_flag '). Get_attribute ("value"), ' wait Tim Eout exception ') except Timeoutexception,e:print e logdebug.write ('%s:timeoutexception:%s \ n '% (DateTime.Now (). Strftime ('%y%m%d-%h%m%s '), e)) print Waitflag if Waitflag and FILENAME:BR Owser.save_screenshot (filename) flag=true # browser.get_screenshot_as_file (' pngfile.png ') exce
        PT Exception,e:Print e logdebug.write ("%s:screenshot exception,%s\n"% (DateTime.Now (). Strftime ('%y%m%d-%h%m%s '), e)) pass
    
    Return Flag def screenshot_batch (logs_path,screenshot_url): Logdebug=open (Logs_path, ' a ') count=0 Report_list=[] Try:content_json=web_spider (logdebug,screenshot_url) decodejson=json.loads (content_js ON) if ' report_list ' in decodejson:report_list=decodejson[' report_list '] else:re Turn except Exception,e:print e logdebug.write ("%s:web spider or json decode exception,%s\n"% (Dateti Me.now (). Strftime ('%y%m%d-%h%m%s '), e)) return count try:if decodejson:browser = Web Driver. Firefox () Browser.maximize_window () for the item in Report_list:report_u rl=item[' Report_url '].strip () if item[' Report_url '] else ' img_path=item[' Img_path '].strip () if item[' img
 _path '] Else '               Screenshot_flag=false if report_url!= ' and img_path!= ': SCREENSHOT_FL Ag=get_screenshot (Browser,logdebug,report_url,img_path) if screenshot_flag:count+
        =1 browser.close () browser.quit () except exception,e:
  
    Print e logdebug.write ("%s:open Firefox batch exception,%s\n"% (DateTime.Now (). Strftime ('%y%m%d-%h%m%s '), E)) Logdebug.close () return count


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.