下面就说一下如何用Python进行自动化测试咯
1、安装语言包
pip install selenium
2、在selenium网站下载对应浏览器的driver
http://www.seleniumhq.org/download/
3、改一下我写的这个文件,将driver路径改成正确的路径
NeoSelenium.py
#!C:\Languages\Python\Python27\python.exe # -*- coding: utf-8 -*- ''' Created on 2017-04-07 @author: Hansen NeoSelenium ''' import os from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities NPath = None x86Path = "C:\NeoTest\TestSelenium\\trunk\\bin\\x86;" x64Path = "C:\NeoTest\TestSelenium\\trunk\\bin\\x64;" def initIE32(): global x86Path global NPath os.environ["PATH"] = x86Path+NPath #print(os.environ["PATH"]) engine = webdriver.Ie() return engine def initIE64(): global x64Path global NPath os.environ["PATH"] = x64Path+NPath #print(os.environ["PATH"]) engine = webdriver.Ie() return engine def initChrome32(): global x86Path global NPath os.environ["PATH"] = x86Path+NPath engine = webdriver.Chrome() return engine def initFirfox32(): global x86Path global NPath os.environ["PATH"] = x86Path+NPath #print(os.environ["PATH"]) engine = webdriver.Firefox() return engine def initFirfox64(): global x64Path global NPath os.environ["PATH"] = x64Path+NPath #print(os.environ["PATH"]) engine = webdriver.Firefox() return engine ''' selenium engine init ''' def initEngine(engineType): global NPath if(NPath is None): NPath = os.environ["PATH"] if(cmp('IE32',engineType.upper())==0): return initIE32() elif(cmp('IE64',engineType.upper())==0): return initIE64() elif(cmp('CHROME',engineType.upper())==0 or cmp('CHROME32',engineType.upper())==0): return initChrome32() elif(cmp('FIREFOX',engineType.upper()) or cmp('FIREFOX32',engineType.upper()) or cmp('FF',engineType.upper())==0 or cmp('FF32',engineType.upper())==0): return initFirfox32() elif(cmp('FIREFOX64',engineType.upper()) or cmp('FF64',engineType.upper())==0): return initFirfox64() else: print("engin type: "+engineType.upper()+" not supported!") return None ''' selenium remote engine init ''' def initEngineRemote(): engine = webdriver.Remote(command_executor="http://localhost:4444/wd/hub", desired_capabilities=DesiredCapabilities.FIREFOX) return engine ''' selenium engine deInit ''' def deInitEngine(engine): engine.close() engine.quit()
4、第一个自动化测试脚本
TestHelloWorld.py
# !C:\Languages\Python\Python27\python.exe # -*- coding: utf-8 -*- ''' Created on 2016-10-22 @author: Hansen HelloWorld sample for NeoSelenium ''' from NeoSelenium import initEngine from NeoSelenium import deInitEngine #一个简单的查询测试 def neohope_search_test(): try: #myEngine = initEngine('ie32') #myEngine = initEngine('ie64') #myEngine = initEngine('chrome') myEngine = initEngine('ff32') #myEngine = initEngine('ff64') base_url = "https://www.neohope.com" myEngine.get(base_url + "/") myEngine.find_element_by_name("s").clear() myEngine.find_element_by_name("s").send_keys("Metabase") myEngine.find_element_by_css_selector("button.search-submit").click() myEngine.implicitly_wait(1000) #print(myEngine.find_elements_by_xpath("//div[@id='content']/article")) queryResultLenght = len(myEngine.find_elements_by_xpath("//div[@id='content']/article")) #print(queryResultLenght) #应该是1但现在是10 assert queryResultLenght==1 finally: #deInitEngine(myEngine) print("test end") #start here neohope_search_test()
5、运行脚本
PS:
IE驱动抛出异常解决方案
"Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones."
解决方法:
打开IE->Internet options->Security->Enable Protected Mode 这个框,在四个域下面要是一致的(要么都勾上,要么都不勾上)