web自动化测试

import HTMLTestRunner
from selenium import webdriver
import unittest
import time
from parameterized import parameterized

class test_web(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver=webdriver.Chrome()
        cls.driver.maximize_window()

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()

    def setUp(self):
        self.addCleanup(self.cleanup)

    def cleanup(self):
        time.sleep(2)

    def page_scroll(self,type,name,scroll_value):
        js=''
        try:
            if 'body'==type:#整个页面滑动
                js="var q=document."+name+".scrollTop="+scroll_value+""#滑动至底部
                #js="var q=document.body.scrollTop=100000"#滑动至顶部
            elif 'no body'==type:
                js="var "+name+"=document.documentElement.scrollTop="+scroll_value+""#滑动至底部,火狐浏览器
            elif type==name:
                if type=='window':
                    js="$("+name+").scrollTop("+scroll_value+");" #滑动至底部,name值为window;谷歌浏览器
                else:
                    js="$('"+name+"').scrollTop("+scroll_value+");" #滑动至底部,name是类名,也可以是其他
                    #js="$('.tipinfos').scrollTop(0);" #滑动至底部,.tipinfos是类名,也可以是其他
            print(js)
            self.driver.execute_script(js)
        except BaseException as e:
            print('error scroll {0}'.format(e))

    def ui_highlight(self,types,element):
        js=''
        try:
            if 'id'==types:
                js="var q=document.getElementById('"+element+"');q.style.border='3px solid red';"
            elif 'css'==types:
                js="var q=document.querySelector('"+element+"');q.style.border='3px solid red';"
            self.driver.implicitly_wait(10)
            self.driver.execute_script(js)
            #time.sleep(3)
        except Exception as e:
            print('light_element message:{0}'.format(e))

    def test_1(self):
        self.driver.get('http://www.baidu.com')
        self.driver.find_element_by_id('kw').send_keys('china')
        self.ui_highlight('id','kw')
        self.driver.find_element_by_id('su').click()
        self.ui_highlight('id','su')
        time.sleep(2)
        self.page_scroll('window','window','10000')
        self.assertEqual(1,1)

    def test_2(self):
        self.driver.get('http://www.baidu.com')
        self.ui_highlight('id','kw')
        self.driver.find_element_by_id('kw').send_keys(u'中国')
        self.ui_highlight('id','su')
        self.driver.find_element_by_id('su').click()
        time.sleep(2)
        self.page_scroll('window','window','10000')
        time.sleep(1)
        self.assertEqual(1,2)

    def test_3(self):
        self.driver.get('http://www.baidu.com')
        self.ui_highlight('id','kw')
        self.driver.find_element_by_id('kw').send_keys('china')
        self.ui_highlight('id','su')
        self.driver.find_element_by_id('su').click()
        time.sleep(2)
        self.assertEqual(1,2)

    def test_4(self):
        self.driver.get('http://www.baidu.com')
        self.ui_highlight('id','kw')
        self.driver.find_element_by_id('kw').send_keys(u'中国')
        self.ui_highlight('id','su')
        self.driver.find_element_by_id('su').click()
        time.sleep(2)
        self.assertEqual(1,1)

if __name__=='__main__':
    suite=unittest.TestSuite()
    suite.addTest(test_web('test_1'))
    suite.addTest(test_web('test_2'))
    suite.addTest(test_web('test_3'))
    suite.addTest(test_web('test_4'))
    with open('newhtmltestrunner_report.html','wb')as f:
        runner=HTMLTestRunner.HTMLTestRunner(stream=f,verbosity=2,title=u'HTMLtestrunner测试',description=u'新版HTMLtestrunner测试')
        runner.run(suite)

;