提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 一、web自動化
- 1.xpath定位
- 1.屬性定位
- 2.屬性與邏輯結合
- 3.屬性和層級結合
- 2.常見元素定位方法(面試題)
- 3.常見元素定位失敗原因
- 4.cookie
- 1.驗證碼處理方案
- 2.認識cookie
- 3.cookie跳過登錄案例
- 5.pytest
- 1.pytest介紹及安裝
- 2.定義用例
- 3.執行測試用例
- 1.命令行運行
- 2.PyTest配置文件運行
- 3.項目配置文件config.py
- 4.參數化
- 5.斷言
- 6.allure報告
一、web自動化
1.xpath定位
1.屬性定位
- 說明:利用元素的屬性來進行定位
- 示例://*[@xalue=‘提交’]
#導包
from selenium.webdriver.common.by import By
from tools.chromeDriver import get_driver, quit_driver
#需求:打開注冊頁面。完成以下操作:
#1). 利用元素的屬性信息精準定位用戶名輸入框,并輸入:admin
#項目地址:http://121.43.169.97:8848/pageA.html
driver = get_driver(“http://121.43.169.97:8848/pageA.html”)
driver.find_element(By.XPATH, “//*[@id=‘userA’]”).send_keys(“admin”)
#退出瀏覽器
quit_driver(driver)
2.屬性與邏輯結合
- 說明:利用元素的多個屬性來進行定位
- 示例://input[@value='提交’and@class=‘banana’]\
#導包
from selenium.webdriver.common.by import By
from tools.chromeDriver import get_driver, quit_driver
#需求:打開注冊頁面。完成以下操作:
#1). 利用屬性與邏輯結合在test1輸入框輸入:admin
#項目地址:http://121.43.169.97:8848/pageA.html
driver = get_driver(“http://121.43.169.97:8848/pageA.html”)
driver.find_element(By.XPATH, “//*[@name=‘user’ and
@class=‘login’]”).send_keys(“admin”)
#退出瀏覽器
quit_driver(driver)
3.屬性和層級結合
- 說明:先定位到其父級元素,然后在找到該元素
- 示例://div[@id=‘test1’]/input[@value=‘提交’]
#導包
from selenium.webdriver.common.by import By
from tools.chromeDriver import get_driver, quit_driver
#需求:打開注冊頁面。完成以下操作:
#1). 利用屬性與邏輯結合在test1輸入框輸入:admin
#項目地址:http://121.43.169.97:8848/pageA.html
driver = get_driver(“http://121.43.169.97:8848/pageA.html”)
#driver.find_element(By.XPATH, “//[@name=‘user’ and
@class=‘login’]").send_keys(“admin”)
driver.find_element(By.XPATH, "//[@id=‘p1’]/input”).send_keys(“admin”)
#退出瀏覽器
quit_driver(driver)
2.常見元素定位方法(面試題)
3.常見元素定位失敗原因
4.cookie
1.驗證碼處理方案
- 去掉驗證碼、萬能驗證碼
- 驗證碼識別技術
- 記錄cookie
2.認識cookie
3.cookie跳過登錄案例
- 需求:使用cookie實現跳過登錄
1). 手動登錄商城,獲取cookie
2). 使用獲取到的cookie,達到登錄目的,然后就可以執行登錄之后的操作
#導包
import time
from selenium import webdriver
#打開瀏覽器
driver = webdriver.Chrome()
#打開頁面
driver.get(“https://hmshop-test.itheima.net/Home/User/index.html”)
time.sleep(2)
#繞過登錄
cookie_data = {
“name”: “PHPSESSID”,
“value”: “h0lrprhei3p3v16pr8sb2ehrm3”
}
driver.add_cookie(cookie_data)
time.sleep(2)
driver.refresh() # 刷新
#退出瀏覽器
time.sleep(3)
driver.quit()
5.pytest
1.pytest介紹及安裝
- pytest:python中的一種單元測試框架。
- 為什么學習pytest?
能阻止多個用例去執行
方便實現參數化
能夠生成測試報告 - 安裝
安裝: pip install pytest
驗證: pip show pytest
2.定義用例
#導包
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestLogin:
def setup_class(self):
#打開瀏覽器
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def setup(self):
#打開頁面
self.driver.get(“https://hmshop-test.itheima.net/Home/user/login.html”)
def test_login(self):
#case02:登錄失敗(用戶名為空)
self.driver.find_element(By.ID, “username”).send_keys(“”)
self.driver.find_element(By.ID, “password”).send_keys(“123456”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def test_login2(self):
#case03:登錄失敗(密碼為空)
self.driver.find_element(By.ID, “username”).send_keys(“13488888888”)
self.driver.find_element(By.ID, “password”).send_keys(“”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def test_login3(self):
#case01:登錄成功
self.driver.find_element(By.ID, “username”).send_keys(“13488888888”)
self.driver.find_element(By.ID, “password”).send_keys(“123456”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def teardown(self):
time.sleep(2)
def teardown_class(self):
#退出瀏覽器
time.sleep(3)
self.driver.quit()
3.執行測試用例
1.命令行運行
#導包
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestLogin:
def setup_class(self):
#打開瀏覽器
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def setup_method(self):
#打開頁面
self.driver.get(“https://hmshop-test.itheima.net/Home/user/login.html”)
def test_login(self):
#case02:登錄失敗(用戶名為空)
self.driver.find_element(By.ID, “username”).send_keys(“”)
self.driver.find_element(By.ID, “password”).send_keys(“123456”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def test_login2(self):
#case03:登錄失敗(密碼為空)
self.driver.find_element(By.ID, “username”).send_keys(“13488888888”)
self.driver.find_element(By.ID, “password”).send_keys(“”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def test_login3(self):
#case01:登錄成功
self.driver.find_element(By.ID, “username”).send_keys(“13488888888”)
self.driver.find_element(By.ID, “password”).send_keys(“123456”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def teardown_method(self):
time.sleep(2)
def teardown_class(self):
#退出瀏覽器
time.sleep(3)
self.driver.quit()
2.PyTest配置文件運行
[pytest]
addopts = -s
testpaths = ./scripts
python_files = test*.py
python_classes = Test*
python_functions = test*
3.項目配置文件config.py
- 技術點: 參數化(數據驅動)
- 作用:將測試數據和測試腳本分離,后期代碼維護焦點放在數據
- 使用:裝飾器@(不改變方法內部的代碼邏輯 新增功能)
@pytest.mark.parametrize==》循環遍歷測試數據 調用測試腳本 - 步驟:
① 準備測試數據,格式:[(),(),()]
② 在被測試方法前面引入裝飾器
@pytest.mark.parametrize(“保存數據的變量,注意變量個數=(中數據的個數)”, 測試數據)
def test_login(self, 直接復制裝飾器中保存數據的一組變量名即可):
pass
③ 修改測試方法代碼 引用變量中的數據完成測試
#導包
import time
import config
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
test_data = [
(“”, “123456”, “8888”),
(“13488888888”, “”, “8888”),
(“13488887799”, “123456”, “8888”),
(“13488888888”, “123457”, “8888”),
(“13488888888”, “123456”, “8888”)
]
class TestLogin:
#類前置處理
def setup_class(self):
#打開瀏覽器
service = Service(executable_path=config.BASE_PATH +
‘/chromedriver.exe’)
self.driver = webdriver.Chrome(service=service)
#driver = webdriver.Chrome()
#窗口最大化
self.driver.maximize_window()
#類后置處理
def teardown_class(self):
#退出瀏覽器
self.driver.quit()
#方法前置處理
def setup_method(self):
#打開頁面
self.driver.get(“https://hmshop-
test.itheima.net/index.php/Home/user/login.html”)
#方法后置處理
def teardown_method(self):
#等待3秒
time.sleep(3)
@pytest.mark.parametrize(“username, password, code”, test_data)
def test_login(self, username, password, code):
#頁面定位+操作
self.driver.find_element(By.ID, “username”).send_keys(username)
self.driver.find_element(By.ID, “password”).send_keys(password)
self.driver.find_element(By.ID, “verify_code”).send_keys(code)
self.driver.find_element(By.NAME, “sbtbutton”).click()
4.參數化
- 說明:通過參數的方式來傳遞數據,從而實現數據和腳本分離。并且可以實現用例的重復執行
#導包
import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
test_data = [(“”, “123456”, “8888”),
(“13488888888”, “”, “8888”),
(“13488888888”, “123456”, “8888”)]
class TestLogin:
def setup_class(self):
#打開瀏覽器
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def setup_method(self):
#打開頁面
self.driver.get(“https://hmshop-test.itheima.net/Home/user/login.html”)
@pytest.mark.parametrize(“username, password, code”, test_data)
def test_login(self, username, password, code):
self.driver.find_element(By.ID, “username”).send_keys(username)
self.driver.find_element(By.ID, “password”).send_keys(password)
self.driver.find_element(By.ID, “verify_code”).send_keys(code)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click()
def teardown_method(self):
time.sleep(2)
def teardown_class(self):
#退出瀏覽器
time.sleep(3)
self.driver.quit()
5.斷言
- 說明:讓程序代替人為判斷測試程序執行結果是否符合預期結果的過程
- 案例:斷言登錄成功后的用戶信息
#導包
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestLogin:
def setup_class(self):
#打開瀏覽器
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def setup_method(self):
#打開頁面
self.driver.get(“https://hmshop-test.itheima.net/Home/user/login.html”)
def test_login(self):
#case01:登錄成功
self.driver.find_element(By.ID, “username”).send_keys(“13488888888”)
self.driver.find_element(By.ID, “password”).send_keys(“123456”)
self.driver.find_element(By.ID, “verify_code”).send_keys(“8888”)
self.driver.find_element(By.LINK_TEXT, “登 錄”).click(
time.sleep(3)
assert “13488888888” == self.driver.find_element(By.CLASS_NAME,
“userinfo”).text
def teardown_method(self):
time.sleep(2)
def teardown_class(self):
#退出瀏覽器
time.sleep(3)
self.driver.quit()
6.allure報告
- 安裝: pip install allure-pytest
- 驗證: pip show allure-pytest
- 下載: https://github.com/allure-framework/allure2/releases
- 解壓
- allure配置
- pytest.ini配置文件
[pytest]
addopts = -s --alluredir report
testpaths = ./scripts
python_files = test*.py
python_classes = Test*
python_functions = test*
- 生成報告:
① 運行測試腳本:pytest
② 生成測試報告:allure serve report