Python web框架搭建


web自動化的作用就是通過代碼自動化代替人工手動測試,節省人力、時間和精力;那么又是如果進行自動測試呢?思路如下:

下面通過一個例子說明一下:登錄操作


手動測試是直接在輸入框輸入用戶名、密碼、然后點擊登錄按鈕;

那么代碼也同樣道理;

1、先定位到用戶名、密碼的輸入框元素和登錄按鈕元素---pagelocators模塊

pagelocators模塊--login_locator.py(LoginLocator類)

from selenium.webdriver.common.by import By
class LoginLocator:
#定位用戶名輸入框
uer_account=(By.XPATH,'//input[@placeholder="請輸入你的賬號/手機號"]')
#定位密碼輸入框
password=(By.XPATH,'//input[@placeholder="請輸入你的密碼"]')
#定位登錄按鈕
button=(By.XPATH,'//button[@id="loginBtn"]')


 

2、然后進行輸入用戶名、密碼、點擊登錄按鈕操作(前提准備好測試用例--testdatas模塊)---pageprojects模塊

pageprojects模塊---login_page.py(定義LoginPage類):

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PageLocators.login_locator import LoginLocator as loc
from Commons.BasePage import BasePage
import time

class LoginPage(BasePage):
       #登錄操作
      def login_success(self,account,password):
            name="登錄模塊"
            WebDriverWait(self.driver,30).until(EC.visibility_of_element_located(loc.uer_account))
            self.driver.find_element(*loc.uer_account).clear()
            self.driver.find_element(*loc.uer_account).send_keys(account)
            WebDriverWait(self.driver,30).until(EC.visibility_of_element_located(loc.password))
            self.driver.find_element(*loc.password).clear()
            self.driver.find_element(*loc.password).send_keys(password)
            self.driver.find_element(*loc.button).click()
            self.save_webImgs(model=name)
            time.sleep(1)


 

testdatas模塊--Login_datas.py

#正常場景---正常登錄
success_data={'account':'101****4088','password':'******','check':'登錄成功'}


 

3、進行測試用例操作---testcases模塊

testcases模塊--test_login.py(TestApi類並繼承(unittest.TestCase))

from selenium import webdriver
from TestDatas import common_datas as cd
from TestDatas import Login_datas as ld
from ddt import ddt,data
import unittest
from PageProjects.login_page import LoginPage

def setUp(self):
    #配置WebDriver驅動的環境變量
    chrome_driver=r"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe"
    #打開瀏覽器
    self.driver=webdriver.Chrome(chrome_driver)
    #打開Aos登錄頁面
    self.driver.get("http://*****/cn/login")
    self.driver.maximize_window()
    self.lp=LoginPage(self.driver)

def tearDown(self):
    self.driver.quit()

#成功登錄的情況
def test_login_1_success(self):
     logging.info("*********登錄用例:正常場景:使用正確的用戶名和密碼登陸*********")
     #登錄頁面--登錄功能--輸入用戶名和密碼
     self.lp.login_success(ld.success_data['account'],ld.success_data['password'])
    #斷言
    try:
        act_check=self.lp.get_success_msg()
        self.assertEqual(act_check,ld.success_data['check'])
        logging.info('成功登錄的用戶名{0},密碼為{1}'.format(ld.success_data['account'],ld.success_data['password']))
        logging.info('登錄成功')
    except EnvironmentError as e:
        logging.info("登錄異常{0}".format(e))
        time.sleep(5)


 

4、測試報告模塊

import unittest
from Commons import path_config
import HTMLTestRunnerNew
from TestCases.test_login import TestApi
import logging
from Commons import logger
suite=unittest.TestSuite()
loader=unittest.TestLoader()

# file=open('report.html','wb+')
with open(path_config.report_dir,'wb+') as file:
suite.addTest(loader.loadTestsFromTestCase(TestApi))
runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file, verbosity=2,title='linda測試接口報告',description='Python10_test',tester=None)
runner.run(suite)

 

以上只是做了一個簡單的框架,可以加上日志、截圖等模塊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM