Selenium2+python自動化10-登入案例

來源:互聯網
上載者:User

標籤:使用者名稱   css_   pre   rom   自己   logout   好的   utf-8   可讀性   

前言    

前面幾篇都是講一些基礎的定位方法,沒具體的案例,小夥伴看起來比較枯燥,有不少小夥伴給小編提建議以後多出一些具體的案例。本篇就是拿部落論壇作為測試專案,寫一個簡單的登入測試指令碼。

在寫登入指令檔的時候呢,先要保證流程能跑起來,然後才是去想辦法最佳化代碼,讓自己的指令碼看起來更舒服,具有良好的可讀性。

一、登入

    1.先開啟瀏覽器

    2.開啟論壇首頁:"http://www.hordehome.com/"

    3.尋找元素之前可以先設定元素等待:implicitlywait()

    4.點登入按鈕,彈出登入框

    5.輸入使用者名稱、密碼,然後點登入

# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.hordehome.com")
# 設定隱試等待10秒
driver.implicitly_wait(10)
# 點登入按鈕,輸入帳號密碼後登入
driver.find_element_by_id("ember886").click()
driver.find_element_by_id("login-account-name").send_keys("YOYO")
driver.find_element_by_id("login-account-password").send_keys("xxxooo")
driver.find_element_by_css_selector(".btn.btn-large.btn-primary").click()

 

二、檢查結果

    1.登入完成之後,需要檢查是否登入成功,這裡就需要有個檢查點,我這邊選擇的是查看登入後頭像的屬性

    2.先定位到登入頭像,通過get_attribute()方法擷取到這個對象的title屬性

    3.判斷擷取到的值,與期望結果是否一致

    4.符合預期結果測試通過

    5.不符合預期結果測試不通過

# 擷取登入頭像的title屬性值
t = driver.find_element_by_id("current-user").get_attribute("title")
# 判斷返回結果
if t == u"個人頁面、訊息、書籤和設定":
   print("登入成功")
else:
   print("登入失敗")

 (注意:這裡網站已經更新過了,學下思路就行,不要copy)

 

三、退出登入

    1.測試完之後,別忘了最後退出登入

    2.退出登入後,關閉瀏覽器

# 退出登入
driver.find_element_by_id("current-user").click()
driver.find_element_by_id("ember1097").click()
driver.quit()

 
四、登入函數

    1.雖然上面的代碼能實現登入,但整個代碼跟記流水賬一樣,沒什麼可讀性。如果我想換個帳號登入,這時候還得找到登入的帳號和密碼位置,比較費時。

    2.我們可以把登入和退出寫出兩個函數,這樣看起來更舒服一點。

    3.把登入的帳號和密碼參數化

# coding:utf-8
from selenium import webdriver
def login(user,password):
   driver.get("http://www.hordehome.com")
   driver.implicitly_wait(10)
   driver.find_element_by_id("ember886").click()
   driver.find_element_by_id("login-account-name").send_keys(user)
   driver.find_element_by_id("login-account-password").send_keys(password)
   driver.find_element_by_css_selector(".btn.btn-large.btn-primary").click()
   return driver
def logout():
   driver.find_element_by_id("current-user").click()
   driver.find_element_by_id("ember1097").click()
   driver.quit()

 五、運行用例

    1.先調用登入函數

    2.檢查登入結果

    3.退出登入,並關閉瀏覽器

if __name__ == "__main__":
   driver = webdriver.Firefox()
   driver = login("YOYO", "xxxooo")
   t = driver.find_element_by_id("current-user").get_attribute("title")
   if t == "個人頁面、訊息、書籤和設定":
       print("登入成功")
   else:
       print("登入失敗")
   logout()

以上是登入部落論壇www.hordehome.com的指令碼參考,小夥伴們可以拿論壇作為測試專案實踐下。

Selenium2+python自動化10-登入案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.