1. 程式人生 > 實用技巧 >Python3+Selenium Web自動化測試案例分享⑷——登入頁面類方法

Python3+Selenium Web自動化測試案例分享⑷——登入頁面類方法

本章節以網易企業郵箱登入頁面為案例,封裝的方法主要有:定位元素、輸入賬號、輸入密碼、登入成功、登入失敗等登入相關的步驟,loginPage是繼承了basePage類,相當於中間層,後期頁面元素變動,主要也是修改loginPage。

詳情如下:

一、loginPage.py

# _*_ coding:utf-8 _*_
from selenium.webdriver.common.by import By
from Page.basePage import BasePage                          #匯入基本類
from Public import log

log_info 
=log.logger #log方法 class Login_page(BasePage): """ 登入頁面操作步驟 """ #元素定位器 login_way =(By.ID,"switchNormalCtrl") username = (By.ID, "accname") password = (By.ID, "accpwd") login_btn = (By.XPATH, "//*[@id='loginBlock']/div[2]/form[1]/div[5]/button
") logout = (By.XPATH, "//*[@id='top']/div/div[2]/div[3]/div/a[5]") login_msg = (By.ID, 'msgpid') user_air=(By.XPATH,"//*[@id='loginBlock']/div[2]/form[1]/div[1]/div") pw_air=(By.XPATH,"//*[@id='loginBlock']/div[2]/form[1]/div[2]/div") #開啟頁面 def open(self, url, pagetitle): self._open(url, pagetitle)
#登入方式選擇 def click_loginway(self): log_info.info("點選密碼登入") self.find_element(*self.login_way).click() #輸入賬號 def input_username(self, username): log_info.info("輸入賬號") self.find_element(*self.username).clear() self.find_element(*self.username).send_keys(username) #輸入密碼 def input_password(self, password): log_info.info("輸入密碼") self.find_element(*self.password).clear() self.find_element(*self.password).send_keys(password) #點選登入 def click_login(self): log_info.info("點選登入") self.find_element(*self.login_btn).click() #登入成功文字 def show_userid(self): exit_text=self.find_element(*self.logout).text log_info.info("登入成功") return exit_text #賬號登出 def click_exit(self): self.find_element(*self.logout).click() log_info.info("賬號登出") #登入失敗提示 def Mismatch(self): msg_text=self.find_element(*self.login_msg).text #log_info.info(msg_text) return msg_text #帳號為空 def username_air(self): user_air=self.find_element(*self.user_air).text #log_info.info(user_air) return user_air #密碼為空 def password_air(self): password_text=self.find_element(*self.pw_air).text #log_info.info(password_text) return password_text