用phantomjs進行web界面自動化測試的幾個注意點

貌似我之前說過很多界面自動化測試的壞話,哈哈。最近接觸了phantomjs,發現用它進行web界面測試也挺有意思的,下面舉幾個我使用過程當中發現的注意點。css

一、須要指定phantomjs位置,不然使用時會報錯html

driver = webdriver.PhantomJS(executable_path=r'D:\phantomjs-2.1.1-windows\bin\phantomjs.exe')#在windows中使用phatomjs,須要在這裏指定可執行文件的位置,或者修改系統宏也行

二、須要指定虛擬窗口的分辨率,不然截圖時可能會報錯:Element is not currently visible and may not be manipulated exception。詳細討論看:https://github.com/ariya/phantomjs/issues/11637python

driver.set_window_size(1024, 768)

三、須要修改源碼service.py的send_remote_shutdown_command,不然driver.quit() 時會報錯。詳細討論看:http://stackoverflow.com/questions/36153007/permission-error-if-to-use-phantomjsgit

def send_remote_shutdown_command(self):
        try:
            if self._cookie_temp_file:
                os.remove(self._cookie_temp_file)
        except Exception,info:
            pass

四、有時候定位失敗多是由於網頁還未加載完,能夠加個sleep或者WebDriverWait試試github

不管是web仍是gui,界面測試最重要的就是定位控件,再進行操做。phantomjs提供的定位控件方法以下:web

def find_element_by_id(self, id_):
def find_element_by_xpath(self, xpath):
def find_element_by_link_text(self, link_text):
def find_element_by_partial_link_text(self, link_text):
def find_element_by_name(self, name):
def find_element_by_tag_name(self, name):
def find_element_by_class_name(self, name):
def find_element_by_css_selector(self, css_selector):

還有一類是find_elements的,含義和上面的相同,只不過是尋找出目標網頁中具備相同條件的一批控件罷了,這樣就能夠同時對多個控件進行一樣的操做了。chrome

說到操做,定位控件以後,接着就是對控件進行操做,常見的控件操做以下:windows

.click 點擊
.send_keys 按鍵輸入
.text  獲取控件的文本
.get_attribute  得到控件指定的屬性值

 

使用phantomjs時最好結合chrome的f12(dev_tool),這樣對於不懂html的同窗(如我)來講事半功倍。cookie

最後,再舉幾個phantomjs的簡單使用例子:測試

url = r'https://www.so.com/'
driver.get(url)
input_by_id = driver.find_element_by_id('input')
input_by_id.send_keys(u'測試find_element_by_id')
button_by_id = driver.find_element_by_id('search-button')
button_by_id.click()
time.sleep(1)
driver.save_screenshot('byid.png')


url = r'https://www.so.com/'
driver.get(url)
input_by_name = driver.find_element_by_name('q')
input_by_name.send_keys(u'測試find_element_by_name')
button_by_class_name = driver.find_element_by_class_name('skin-search-button')
button_by_class_name.click()
time.sleep(1)
driver.save_screenshot('byname.png')


url = r'https://www.so.com/'
driver.get(url)
input_by_class_name= driver.find_element_by_class_name('placeholder')
input_by_class_name.send_keys(u'測試find_element_by_class_name')
button_by_class_name = driver.find_element_by_class_name('skin-search-button')
button_by_class_name.click()
time.sleep(1)
driver.save_screenshot('byclassname.png')


url = r'https://www.so.com/'
driver.get(url)
button_by_link_text = driver.find_element_by_link_text('問答')
button_by_link_text.click()
time.sleep(1)
driver.save_screenshot('bylinktext.png')


url = r'https://www.so.com/'
driver.get(url)
button_by_partial_link_text = driver.find_element_by_partial_link_text('問')
button_by_partial_link_text.click()
time.sleep(1)
driver.save_screenshot('bypartiallinktext.png')


url = r'https://www.so.com/'
driver.get(url)
input_by_xpath = driver.find_element_by_xpath('//*[@id="input"]')
input_by_xpath.send_keys(u'測試find_element_by_xpath')
button_by_id = driver.find_element_by_id('search-button')
button_by_id.click()
time.sleep(1)
driver.save_screenshot('byxpath.png')


url = r'https://www.so.com/'
driver.get(url)
input_by_css_selector = driver.find_element_by_css_selector('#input')
input_by_css_selector.send_keys(u'測試find_element_by_css_selector')
button_by_id = driver.find_element_by_id('search-button')
button_by_id.click()
time.sleep(1)
driver.save_screenshot('bycssselector.png')


url = r'https://www.so.com/'
driver.get(url)
input_by_tag_name = driver.find_elements_by_tag_name('input')#tag表示標籤,當網頁存在同標籤名的控件時,通常用for進行定位比較好
for a in input_by_tag_name:
    if a.get_attribute('type') == 'text':
        a.send_keys(u'測試find_element_by_tag_name')
button_by_tag_name= driver.find_element_by_id('search-button')
button_by_tag_name.click()
time.sleep(1)
driver.save_screenshot('bytagname.png')
相關文章
相關標籤/搜索