python+selenium實現跨瀏覽器兼容性測試


python

https://www.python.org/

python是一種腳本語言, 易學易用,可以助你快速實現業務邏輯,高效集成系統。

----- http://zh.wikipedia.org/zh-cn/Python:

它的語法簡單,與其它大多數程序設計語言使用大括號不一樣,它使用縮進來定義語句塊。

Python經常被用於Web開發。比如,通過mod_wsgi模塊,Apache可以運行用Python編寫的Web程序。使用Python語言編寫的Gunicor

n作為Web服務器,也能夠運行Python語言編寫的Web程序。Python定義了WSGI標准應用接口來協調Http服務器與基於Python的Web程序之間的溝通。一些Web框架,如DjangoPyramidTurboGearsTornadoweb2pyZopeFlask等,可以讓程序員輕松地開發和管理復雜的Web程序。

download: https://www.python.org/downloads/release/python-279/

 selenium python bindings

selenium python bindings 提供一套簡單的API,通過這些API調用了Selenium WebDriver,可以實現功能測試和驗收性測試用例。

可以支持對 Firefox 和 IE 和 chrome的調用。

官網介紹:

http://selenium-python.readthedocs.org/installation.html#introduction

windows安裝

1、 下載安裝python3.4 https://www.python.org/ftp/python/3.4.2/python-3.4.2.msi

2、使用cmd.exe執行如下命令,安裝selenium python bindings

C:\Python34\Scripts\pip.exe install selenium

3、下載IEdriver 和 chromedriver,並放到python安裝目錄下 (C:\Python34)

https://github.com/fanqingsong/daydayup/blob/master/CSoftware/webdriver/IEDriverServer.exe

https://github.com/fanqingsong/daydayup/blob/master/CSoftware/webdriver/chromedriver.exe

firefox不需要驅動

使用方法待研究驅動:

https://github.com/dineshkummarc/operadriver

https://github.com/mfazekas/safaridriver

4、可以運行python的測試腳本了,例如:

C:\Python34\python.exe C:\my_selenium_script.py

官網安裝介紹:

http://selenium-python.readthedocs.org/installation.html#downloading-python-bindings-for-selenium

 

測試腳本

下面腳本實現, 打開baidu網頁,搜索一個關鍵字, 依次執行三個瀏覽器(firefox ie chrome)。

 1 #import os
2 import time 3 from selenium import webdriver 4 from selenium.webdriver.common.keys import Keys 5 6 # test case 7 def testbrowser(driver): 8 driver.get("http://www.baidu.com") 9 driver.find_element_by_id("kw").click() 10 driver.find_element_by_id("kw").clear() 11 driver.find_element_by_id("kw").send_keys("vx") 12 driver.find_element_by_id("su").click() 13 driver.implicitly_wait(30) 14 time.sleep(3) 15 driver.close() 16 driver.quit() 17 return None 18 19 driverfirefox = webdriver.Firefox() 20 testbrowser(driverfirefox) 21 22 driverie = webdriver.Ie() 23 testbrowser(driverie) 24 25 driverchrome = webdriver.Chrome() 26 testbrowser(driverchrome)

 

測試用例testbrowser代碼可以使用 selenium IDE導出的腳本中截取主體部分:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Baidupython(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_baidupython(self):
 driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").click()
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("vx")
        driver.find_element_by_id("su").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

 

也可以自己寫腳本,使用selenium driver的提供的api:

http://selenium-python.readthedocs.org/locating-elements.html

 

python教程:

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013747381369301852037f35874be2b85aa318aad57bda000

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM