from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import Select from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support import expected_conditions as EC class Base(): def __init__(self,driver): self.driver = driver def find(self,locator): ele = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(*locator)) return ele def send(self,locator,text): ele =self.find(locator) ele.send_keys(text) def click(self,locator): self.find(locator).click() def clear(self,locator): self.find(locator).clear() def get_text(self, locator): '''获取元素文本值''' try: ele =self.find(locator).text except: ele ="" return ele def is_element_exist(self,locator): '''判断元素是否存在,结果返回布尔值 ture 或 false''' try: self.find(locator) print("找到元素了!!!!!") return True except: print("找不到元素") return False def text_in_element(self,locator,_text): '''判断元素text属性''' try: r = WebDriverWait(self.driver,30,1).until(EC.text_to_be_present_in_element(locator,_text)) return r except: return False def value_in_element(self,locator,text): '''判断元素value属性''' try: r = WebDriverWait(self.driver,30,1).until(EC.text_to_be_present_in_element(locator,_text)) return r except: return False def move_to_element(self,locator): '''鼠标悬停''' element = self.find(locator) ActionChains(self.driver).move_to_element(element).perform() def select_by_index(self,locator,index=0): '''select下拉框,index索引''' element =self.find(locator) Select(element).select_by_index(index) def select_by_value(self,locator,value): '''select下拉框,value''' element =self.find(locator) Select(element).select_by_index(value) def select_by_text(self,locator,_text): '''select下拉框,文本''' element =self.find(locator) Select(element).select_by_visible_text(_text) def is_alert(self,timeout=3): '''判断是否有alert,没有返回false,有返回alert对象''' try: alert = WebDriverWait(self.driver,timeout,1).until(EC.alert_is_present()) return alert except: return False def js_focus_element(self,locator): '''聚焦元素''' target =self.find(locator) self.driver.execute_scrit("arguments[0].scrollIntoView();",target) def js_focus_top(self,locator): '''滑动到顶部''' js ="window.scrollTo(0,0)" self.driver.execute_scrit(js) def js_focus_end(self,locator): '''滑动到底部''' js ="window.scrollTo(0,document.body.scrollHeight)" self.driver.execute_scrit(js) if __name__ == '__main__': '''自测函数''' from selenium import webdriver driver = webdriver.Firefox() driver.get("http://192.168.43.57/phpwind/admin.php#initiator") loc1 = ("name","admin_name") loc2 = ("name", "admin_pwd") loc3 = ("name", "submit") login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]') b = Base(driver) #实例 b.send(loc1,"admin") b.send(loc2, "123456") b.click(loc3) t = b.get_text(login_xpath) print(t) assert t == "[注销]"
------------------------------------------------
page object model:顾名思义页面对象模式
- 把每个页面(page)当成一个对象(object)
- 封装页面上会用到的操作方法,比如:输入账号,输入密码,点登陆
(一些行为事件,后面用例会用到的)
- 写用例的时候,操作哪个页面,就去调用哪个页面的方法
登录页面操作封装:login_page.py
from t7.base import Base login_url1 = ("http://192.168.43.57/phpwind/admin.php#initiator") login_url2 = ("http://192.168.153.1/phpwind/admin.php#initiator") class Login(Base): loc1 = ("name","admin_name") loc2 = ("name", "admin_pwd") loc3 = ("name", "submit") login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]') def login(self, user="admin", psw="123456"): '''登录页面''' self.send(self.loc1,user) self.send(self.loc2,psw) self.click(self.loc3) def is_login_sucess(self,text): '''判断是否登录成功''' body =("xpath","//body") body_text =self.find(body).text return text in body_text if __name__ == '__main__': from selenium import webdriver driver = webdriver.Firefox() driver.get(login_url1) l =Login(driver) l.login() text =("后台首页") result =l.is_login_sucess(text) print("结果:%s"%result)
------------------
测试用例模块:
import unittest from selenium import webdriver from t7.pages.login_page import Login,login_url1 import ddt class TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.get(login_url1) self.a =Login(self.driver) def tearDown(self): self.driver.quit() def test_login_01(self): '''测试数据正常-登录成功''' self.a.login("admin","123456") result =self.a.is_login_sucess("后台首页") print("登录的实际结果:%s"%result) self.assertTrue(result) if __name__ == '__main__': unittest.main()
标签:14,text,self,Object,driver,locator,POM,login,def From: https://www.cnblogs.com/pingzi66-ww/p/17100293.html