Webdriver(Selenium)之最佳實踐

1、測試用例的組成css

3A法則:jquery

  Arrangement——數據準備web

  Action——步驟測試

  Assert——斷言ui

 

2、表單元素定位最佳實踐spa

name優先code

id其次blog

css selector事件

class + indexip

xpath或其餘

 

3、富文本賦值

應用js方法:

  首先訪問iframe

  再訪問iframe中的contentWindow

  再訪問body

  使用innerText屬性進行賦值(innterHtml)

 

1 def set_content(self, content): 2         js = "document.getElementById('content_ifr').contentWindow.document.body.innerHTML = '%s'" %(content) 3         self.dr.execute_script(js)

 

4、封裝方法——更容易閱讀

 1 # -*- coding=utf-8 -*-
 2 from selenium import webdriver  3 import unittest  4 
 5 class Test(unittest.TestCase):  6     def setUp(self):  7         dr = webdriver.Chrome()  8         dr.get("http://www.baidu.com")  9 
10 
11     def by_id(self, the_id): 12         return self.dr.find_element_by_id(the_id) 13 
14     def by_css(self, css): 15         return self.dr.find_element_by_css_selector(css) 16 
17     def by_name(self, name): 18         return self.dr.find_element_by_name(name) 19     
20     def by_js(self, js): 21         return self.dr.execute_script(js) 22 
23     def tearDown(self): #每一個用例執行以後
24         self.dr.quit()

 

5、js裏的jquery事件

 

js = "document.querySelectorAll('#submit')[0].click()"
driver.execute_script(js)

 

點擊id=submit

相關文章
相關標籤/搜索