Selenium+Python自動化UI測試搭建小試

最近更博速度歷史新低,年底忙得焦頭爛額。。。

今天記錄一下自動化測試的流程:Selenium相關配置看這篇https://www.jianshu.com/p/b8bb08229904

補充可能出現的驅動問題:

1、chrome驅動鏈接:http://chromedriver.storage.googleapis.com/index.html(請下載對應驅動)

2、將解壓後文件chromedriver.exe複製到python的Scripts安裝目錄下(我的C:\Users\LC\AppData\Local\Programs\Python\Python38\Scripts\),並且添加到path環境變量

3、將目錄chrome的安裝目錄添加到path環境變量(我的C:\Program Files (x86)\Google\Chrome\Application\chrome.exe)

=======================正文==========================

從經驗上來說,之前做過用Appium和Java寫的App端的自動化開發,道理是用Appium抓頁面,Java代碼執行點擊事件,一步一步的走通點擊後的UI跳轉頁面或者點擊效果

那麼Selenium和Python(人生苦短,我用Python,其他語言Selenium也會支持,這裏不多說)也是一樣的道理,容我盜張圖:

這裏我們就簡單的模擬一個小場景:打開瀏覽器-->輸入百度的地址-->獲取輸入框id-->輸入想要查找內容-->展示結果

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest
print("--test start--")
class TestCase(unittest.TestCase):
  @classmethod
  def setUpClass(cls):   #打開管道
      cls.driver = webdriver.Chrome()
  #@classmethod  #這是關閉管道,爲了看效果所以暫時不關閉,實際上使用按規範要關閉
  #def tearDownClass(cls):
      #cls.driver.close()
  def setUp(self):
      self.driver.get("https://www.baidu.com/")
  def tearDown(self):
      pass
  def testCase01(self):
      inputE = self.driver.find_element_by_id("kw")
      inputE.send_keys("hallo word")

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

標紅色的就是關鍵步驟了,F12可以找到百度輸入框的id,這個做過前端的都懂

python知識可以看菜鳥教程:https://www.runoob.com/python3/python3-tutorial.html

最後結果截圖如下,注意力別被性感小嘴吸引哦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章