浏览器是系统下比较常用的软件,在linux系统比较常用的是firefox、chrome.如何评估浏览器的基础性能?有很多评测网站进行了这边的测试。 如 CSS acid3 v8test octane html5 dromaeo

考虑到每次测试都需要点击这些网站和取测试结果,决定采用selenium去实现自动测试。 测试代码如下: browser-run.py:

```python

#encoding = utf-8
import logging
import runtest
import re
import sys  
from subprocess import call
from selenium import webdriver
reload(sys)
sys.setdefaultencoding('utf8')

# create logger with 'spam_application'
logger = logging.getLogger('browser-test')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('browser.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

css4 = {'name':'css4', 'url':'http://css4-selectors.com/browser-selector-test/',
       'click':'start-browser-selector-test', 'result':'diagram'}
#do_type2test
acid3 = {'name': 'acid3', 'url': 'http://acid3.acidtests.org/', 'result': 'result'}
v8test = {'name': 'v8test', 'url': 'http://chrome.360.cn/test/v8/run.html', 'result': 'status'}
octane = {'name': 'octane', 'url': 'http://chrome.2345.com/labs/test/octanebenchmark.htm', 'result': 'main-banner'}
#do_type3test
html5 = {'name': 'html5', 'url': 'http://html5test.com/', 'result': '//*[@id=\"score\"]/div[1]/h2/strong'}
#do_type4test
dromaeo = {'name': 'dromaeo', 'url': 'http://dromaeo.com/', 'result': '//*[@id=\'timebar\']/span/strong',
          'testxpath': '//*[@id=\"tests\"]/li[8]/a', 'runid': 'pause', 'resultid': 'left'}

def result(Initialresults):
    p = re.compile(r'\d+')
    return p.findall(Initialresults)


def chrome():
    try:
        retcode = call("which chromium-browser", shell=True)
        if retcode > 0:
            pass
        else:
            csstest = runtest.Do_type2test(css4)
            cssresult = csstest.do_test(webdriver.Chrome())
            print("css4 result is %s" % result(cssresult)[3])
            acidtest = runtest.Do_type1test(acid3)
            acidresult = acidtest.do_test(webdriver.Chrome())
            print("acid3 result is %s" % acidresult)
            vtest = runtest.Do_type1test(v8test)
            vresult = vtest.do_test(webdriver.Chrome())
            print("V8test result is %s" % vresult)
            octanetest = runtest.Do_type1test(octane)
            octaneresult = octanetest.do_test(webdriver.Chrome())
            print("octane result is %s" % octaneresult)
            htmltest = runtest.Do_type3test(html5)
            htmlresult = htmltest.do_test(webdriver.Chrome())
            print("html5test result is %s" % htmlresult)
            dromaeotest = runtest.Do_type4test(dromaeo)
            dromaeotestresult = dromaeotest.do_test(webdriver.Chrome())
            print("dromaeotest result is %s" % dromaeotestresult)
            print("test finish")

    except OSError as e:
        logger.ERROR("Execution failed:%s" % e)


def firefox():
    try:
        retcode = call("which firefox", shell=True)
        if retcode > 0:
            pass
        else:
            csstest = runtest.Do_type2test(css4)
            cssresult = csstest.do_test(webdriver.Firefox())
            print("css4 result is %s" % result(cssresult)[3])
            acidtest = runtest.Do_type1test(acid3)
            acidresult = acidtest.do_test(webdriver.Firefox())
            print("acid3 result is %s" % acidresult)
            vtest = runtest.Do_type1test(v8test)
            vresult = vtest.do_test(webdriver.Firefox())
            print("V8test result is %s" % vresult)
            octanetest = runtest.Do_type1test(octane)
            octaneresult = octanetest.do_test(webdriver.Firefox())
            print("octane result is %s" % octaneresult)
            htmltest = runtest.Do_type3test(html5)
            htmlresult = htmltest.do_test(webdriver.Firefox())
            print("html5test result is %s" % htmlresult)
            dromaeotest = runtest.Do_type4test(dromaeo)
            dromaeotestresult = dromaeotest.do_test(webdriver.Firefox())
            print("dromaeotest result is %s" % dromaeotestresult)
            print("test finish")
    except OSError as e:
        logger.ERROR("Execution failed:%s" % e)
if __name__ == "__main__":
    chrome()
    firefox()

runtest.py:

import time
import logging
from selenium import webdriver
from subprocess import call

module_logger = logging.getLogger('browser-test.runtest')

def check_xpath(testname='dromaetest', checkxpath="//*[@id='tests']/li[3]/b/a"):
    '''check the xpath ready or not'''
    while True:
        try:
            drop = testname.find_element_by_xpath("%s" % checkxpath)
            break
        except:
            module_logger.info("%s is  not ok" % checkxpath)


def check_idon(testname='dromaeotest', checkid='pause'):
    while True:
        try:
            drop = testname.find_element_by_id("%s" % checkid)
            break
        except:
            time.sleep(1)


def check_idoff(testname='dromaeotest', checkid='left'):
    while True:
        try:
            drop = testname.find_element_by_id("%s" % checkid)
            time.sleep(1)
        except:
            break

class Do_type1test(object):
    def __init__(self, testcmds):
        self.logger = logging.getLogger('browser-test.runtest.Do_type1test')
        self.cmds = testcmds
        self.logger.info("%s test startting " % self.cmds['name'])

    def do_test(self, test):
        test.get(self.cmds['url'])
        time.sleep(60)
        while True:
            try:
                drop = test.find_element_by_id(self.cmds['result'])
                break
            except:
                self.logger.info("There are some errors about this test")
                test.quit()
                exit()
        result = test.find_element_by_id(self.cmds['result']).text
        test.quit()
        return result


class Do_type2test(object):
    def __init__(self, testcmds):
        self.logger = logging.getLogger('browser-test.runtest.Do_type2test')
        self.cmds = testcmds
        self.logger.info("%s test startting " % self.cmds['name'])

    def do_test(self, test):
        test.get(self.cmds['url'])
        test.find_element_by_id(self.cmds['click']).click()
        time.sleep(20)
        result = test.find_element_by_id(self.cmds['result']).text
        test.quit()
        return result


class Do_type3test(object):
    def __init__(self, testcmds):
        self.logger = logging.getLogger('browser-test.runtest.Do_type3test')
        self.cmds = testcmds
        self.logger.info("%s test startting " % self.cmds['name'])

    def do_test(self, test):
        test.get(self.cmds['url'])
        time.sleep(60)
        result = test.find_element_by_xpath(self.cmds['result']).text
        test.quit()
        return result


class Do_type4test(object):
    def __init__(self, testcmds):
        self.logger = logging.getLogger('browser-test.runtest.Do_type4test')
        self.cmds = testcmds
        self.logger.info("%s test startting " % self.cmds['name'])

    def do_test(self, test):
        test.get(self.cmds['url'])
        time.sleep(30)
        check_xpath(testname=test, checkxpath=self.cmds['testxpath'])
        test.find_element_by_xpath(self.cmds['testxpath']).click()
        time.sleep(30)
        check_idon(testname=test, checkid=self.cmds['runid'])
        test.find_element_by_id(self.cmds['runid']).click()
        check_idoff(testname=test, checkid=self.cmds['resultid'])
        result = test.find_element_by_xpath(self.cmds['result']).text
        test.quit()

```

by 李鹏