>[info]根據前面章節的學習,這里開始編寫自己的第一個自動化用例
>
## 實戰要求:
用例一:
* 打開移動端網站 `http://a.4399en.com/`
* 登錄,賬號/密碼為是: `gfc@qq.com`/`123456`
* 登錄后,斷言是否已經處于登錄狀態
* 關閉瀏覽器
用例二:
* 打開移動端網站 `http://a.4399en.com/`
* 登錄,賬號/密碼為是: `gfc@123.com`/`123456`
* 斷言是否彈窗提示“User does not exist!”
* 關閉彈窗
* 關閉瀏覽器
## 或許,有些朋友會寫成像以下這樣
用例一,編寫腳本 `login_success.py`
```python
#!/usr/bin/python
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 創建Chrome驅動實例,這里創建driver時,傳入chrome_options參數,告訴服務器,我是用移動端瀏覽器訪問的。
options = webdriver.ChromeOptions()
options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
driver = webdriver.Chrome(chrome_options=options)
# 設置瀏覽器大小,讓它看起來跟手機的樣式差不多。
driver.set_window_size("380", "680")
# 設置一個全局的等待超時時間 10s
driver.implicitly_wait(10)
# 打開首頁
driver.get("http://a.4399en.com/")
# 點擊右上角登錄按鈕打開登錄窗口
driver.find_element_by_class_name("CNlogin").click()
# 在用戶名輸入框中輸入正確的用戶名
wait = WebDriverWait(driver,20,0.5)
account = wait.until(EC.visibility_of_element_located((By.ID,"modify-account")))
# account = driver.find_element_by_id("modify-account")
account.clear()
account.send_keys("gfc@qq.com")
# 在密碼輸入框中輸入正確的密碼
password = driver.find_element_by_id("modify-password")
password.clear()
password.send_keys("123456")
# 點擊登錄按鈕
driver.find_element_by_xpath("//input[@type='submit'][@value='Login']").click()
# 休息3s
time.sleep(3)
# 斷言頁面已經登錄,已經登錄的頁面中,會顯示用戶賬號信息。
assert "gfc@qq.com" in driver.page_source
# 關閉瀏覽器
driver.quit()
```
用例二,編寫腳本 `login_fail.py`:
```python
#!/usr/bin/python
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 創建Chrome驅動實例,這里創建driver時,傳入chrome_options參數,告訴服務器,我是用移動端瀏覽器訪問的。
options = webdriver.ChromeOptions()
options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
driver = webdriver.Chrome(chrome_options=options)
# 設置瀏覽器大小,讓它看起來跟手機的樣式差不多。
driver.set_window_size("380", "680")
# 設置一個全局的等待超時時間 10s
driver.implicitly_wait(10)
# 打開首頁
driver.get("http://a.4399en.com/")
# 點擊右上角登錄按鈕打開登錄窗口
driver.find_element_by_class_name("CNlogin").click()
# 在用戶名輸入框中輸入正確的用戶名
wait = WebDriverWait(driver, 20, 0.5)
account = wait.until(EC.visibility_of_element_located((By.ID, "modify-account")))
# account = driver.find_element_by_id("modify-account")
account.clear()
account.send_keys("gfc@123.com")
# 在密碼輸入框中輸入正確的密碼
password = driver.find_element_by_id("modify-password")
password.clear()
password.send_keys("123456")
# 點擊登錄按鈕
driver.find_element_by_xpath("//input[@type='submit'][@value='Login']").click()
# 休息3s
time.sleep(3)
# 切換到彈窗窗口
alert = driver.switch_to.alert
# 斷言頁面是否有彈窗,彈窗信息為"User does not exist!"
assert "User does not exist!" in alert.text
# 確定彈窗
alert.accept()
# 關閉瀏覽器
driver.quit()
```
然后分別去運行`login_success.py` 和 `login_fail.py`。
## 你有沒有覺得,這樣子很挫?
* 代碼中寫死了測試數據,如何換個賬號,要修改用例代碼?
* 代碼中寫死了頁面元素定位信息,如果多個位置定位相同的元素,一旦元素定位信息發生改變,是不是需要到處去修改?
* 沒有用例組織,一個用例一個文件,很多代碼冗余。
* 缺少用例報告。
如果你也意識到這些問題,那么請繼續往下看,后面的章節會告訴你如何去優化這個實戰的代碼。
<hr style="margin-top:50px">
<section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 10px;box-sizing: border-box;"><section class="" style="display: inline-block;width: 100%;border-width: 5px;border-style: double;border-color: rgb(23, 22, 24);padding: 10px;border-radius: 2px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(20px, 0px, 0px);-webkit-transform: translate3d(20px, 0px, 0px);-moz-transform: translate3d(20px, 0px, 0px);-o-transform: translate3d(20px, 0px, 0px);font-size: 11px;margin: -50px 0% 0px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;width: 7em;height: 7em;display: inline-block;vertical-align: bottom;border-radius: 100%;border-width: 4px;border-style: double;border-color: rgb(23, 22, 24);background-position: center center;background-repeat: no-repeat;background-size: cover;background-image: url("http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n");"><section class="" style="width: 100%;height: 100%;overflow: hidden;box-sizing: border-box;"><img class="" data-ratio="0.6012024" data-w="499" data-src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" style="opacity: 0; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" src="http://pav7h2emv.bkt.clouddn.com/FnD-fHkNDLN1-b02XmnMvsz6ld-n" data-fail="0"></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 30px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: 40px 0% 0px;box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">微信公眾號:</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;">python測試開發圈</p><p style="margin: 0px;padding: 0px;box-sizing: border-box;"><br style="box-sizing: border-box;"></p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="text-align: center;margin: 10px 0% 0px;box-sizing: border-box;"><section class="" style="max-width: 100%;vertical-align: middle;display: inline-block;border-width: 0px;border-radius: 0px;box-shadow: rgb(0, 0, 0) 0px 0px 0px;width: 90%;overflow: hidden !important;box-sizing: border-box;"><img data-ratio="1" data-w="430" data-src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" style="vertical-align: middle; max-width: 100%; box-sizing: border-box; width: 100% !important; height: auto !important; visibility: visible !important;" width="100%" data-type="jpeg" _width="100%" class="" src="http://pav7h2emv.bkt.clouddn.com/FibGgIJSMfHtehzeWOOzjdQKSMx5" data-fail="0"></section></section></section></section></section></section><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="margin: -30px 0% 0px;box-sizing: border-box;"><section class="" style="display: inline-block;vertical-align: top;width: 61.8%;padding: 0px 15px;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(5px, 0px, 0px);-webkit-transform: translate3d(5px, 0px, 0px);-moz-transform: translate3d(5px, 0px, 0px);-o-transform: translate3d(5px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">一起分享學習與成長路線</p></section></section></section></section><section class="" style="display: inline-block;vertical-align: top;width: 38.2%;box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section class="" style="transform: translate3d(10px, 0px, 0px);-webkit-transform: translate3d(10px, 0px, 0px);-moz-transform: translate3d(10px, 0px, 0px);-o-transform: translate3d(10px, 0px, 0px);box-sizing: border-box;"><section class="" style="color: rgb(160, 160, 160);font-size: 14px;box-sizing: border-box;"><p style="margin: 0px;padding: 0px;box-sizing: border-box;">長按(或掃描)二維碼關注</p></section></section></section></section></section></section></section></section></section>