基礎(chǔ)操作封裝
conf.config.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
WEB_DRIVER=os.path.join(os.path.dirname(__file__),"../chrome_driver_v79/chromedriver.exe")
test_case.ui.base_ui.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from selenium import webdriver
from conf.config import WEB_DRIVER
# 把selenium常用操作封裝到BaseUI這個(gè)類中,我們再寫用例的時(shí)候豫柬,就可以簡化代碼量了。
class BaseUI():
? ? def start_browser(self):
? ? ? ? driver = webdriver.Chrome(WEB_DRIVER)
? ? ? ? # 窗口最大化
? ? ? ? driver.maximize_window()
? ? ? ? driver.implicitly_wait(20) # 隱式等待
? ? ? ? self.driver = driver
? ? def quit(self):
? ? ? ? self.driver.quit()
? ? def get(self,url):
? ? ? ? self.driver.get(url)
? ? def click(self,xpath):
? ? ? ? el = self.driver.find_element_by_xpath(xpath)
? ? ? ? el.click()
? ? def send_keys(self,xpath,text):
? ? ? ? el = self.driver.find_element_by_xpath(xpath)
? ? ? ? el.clear()
? ? ? ? el.send_keys(text)
test_case.ui.test_demo.py:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from time import sleep
from test_case.ui.base_ui import BaseUI
def test_input():
? ? driver = BaseUI()
? ? driver.start_browser()
? ? driver.get("http://ui.yansl.com/#/input")
? ? driver.send_keys("http://input[@name='t2']","sdfisdjif")
? ? sleep(2)
? ? driver.quit()