ui自动化框架

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def get_driver(drivername="chrome"):
    if drivername == "chrome":
        # user_data_dir = r"--user-data-dir=C:\Users\01397622\AppData\Local\Google\Chrome\User Data"
        # options = webdriver.ChromeOptions()
        # options.add_argument(user_data_dir)
        # driver = webdriver.Chrome(options=options)
        driver = webdriver.Chrome()
    elif drivername == "firefox":
        driver = webdriver.Firefox()
    elif drivername == "ie":
        driver = webdriver.Ie()
    else:
        driver = None
    return driver


class Base:
    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)
        self.driver.maximize_window()
        sleep(2)

    def close(self):
        sleep(2)
        self.driver.quit()

    def refresh(self):
        self.driver.refresh()

    def back(self):
        self.driver.back()

    def forward(self):
        self.driver.forward()

    def get_element(self, locator, timeout=20):
        try:
            element = WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
        except Exception as e:
            print(e)
            element = None
        return element

    def get_elements(self, locator, timeout=10):
        try:
            elements = WebDriverWait(self.driver, timeout).until(EC.presence_of_all_elements_located(locator))
        except Exception as e:
            print(e)
            elements = None
        return elements

    def is_text_in_element(self, locator, text, timeout=20):
        try:
            if WebDriverWait(self.driver, timeout).until(EC.text_to_be_present_in_element(locator, text)):
                return True
            else:
                return False
        except Exception as e:
            print(e)
            return False

    def is_value_in_element(self, locator, value, timeout=10):
        try:
            if WebDriverWait(self.driver, timeout).until(EC.text_to_be_present_in_element_value(locator, value)):
                return True
            else:
                return False
        except Exception as e:
            print(e)
            return False

    def click(self, locator):
        try:
            self.get_element(locator).click()
            sleep(3)
        except Exception as e:
            print(e)

    def clear(self, locator):
        try:
            self.get_element(locator).clear()
        except Exception as e:
            print(e)

    def send_keys(self, locator, text):
        try:
            self.clear(locator)
            self.get_element(locator).send_keys(text)
            sleep(2)
        except Exception as e:
            print(e)

    def click_checkBox(self, locator):
        try:
            element = self.get_element(locator)
            if element.is_selected():
                pass
            else:
                self.click(locator)
        except Exception as e:
            print(e)

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


if __name__ == '__main__':
    driver = get_driver("chrome")
    base = Base(driver)
    base.get_url(r"C:\Users\01397622\PycharmProjects\Selenium-study\练习页面\注册A.html")  # r转义
    userA_loc = ("id", "userA")
    telA_loc = ("id", "telA")
    apple_loc = ("id", "pga")
    base.send_keys(userA_loc, "lm")
    base.send_keys(telA_loc, "110")
    base.click_checkBox(apple_loc)
    base.close()
from common.base import Base
from common.base import get_driver

page_url = "https://www.baidu.com/"


class Page(Base):
    login_loc = ("link text", "登录")
    usernamelogin_loc = ("css selector", "#TANGRAM__PSP_11__footerULoginBtn")
    username_loc = ("name", "userName")
    password_loc = ("name", "password")
    submit_loc = ("id", "TANGRAM__PSP_11__submit")
    success_username_loc = ("css selector", "#s-top-username > span.user-name.c-font-normal.c-color-t")

    def get(self):
        self.get_url(page_url)

    def click_login(self):
        self.click(self.login_loc)

    def click_usernamelogin(self):
        self.click(self.usernamelogin_loc)

    def input_username(self, text):
        self.send_keys(self.username_loc, text)

    def input_password(self, text):
        self.send_keys(self.password_loc, text)

    def click_submit(self):
        self.click(self.submit_loc)

    def judge_success(self, name):
        if self.is_text_in_element(self.success_username_loc, name):
            return True
        else:
            return False


if __name__ == '__main__':
    driver = get_driver("chrome")
    p = Page(driver)
    p.get()
    p.click_login()
    p.click_usernamelogin()
    p.input_username("13065175265")
    p.input_password("lm18379695606")
    p.click_submit()
    p.judge_success()
from common.base import get_driver
from page.page import Page
import unittest
import ddt

data = [{"username": "13065175265", "password": "lm18379695606","judgename":"妮可Alex", "expect": True},
        {"username": "18866665555", "password": "lm18379695606", "judgename":"妮可Bob","expect": False}
        ]
"""
ddt数据驱动  测试数据单独操作
"""
# data的这部分测试数据可以放一个excel表格中存储,python的xlrd库可以操作 #
@ddt.ddt
class Test_login(unittest.TestCase):
    def setUp(self):
        self.driver = get_driver()
        self.p = Page(self.driver)
        self.p.get()

    def tearDown(self):
        self.p.close()

    @ddt.data(*data)
    def test_login(self, data):
        """百度登录参数化"""
        self.p.click_login()
        self.p.click_usernamelogin()
        self.p.input_username(data['username'])
        self.p.input_password(data['password'])
        self.p.click_submit()
        result = self.p.judge_success(data['judgename'])
        self.assertEqual(data['expect'], result, msg="预期不一致,登录失败")


if __name__ == '__main__':
    unittest.main(verbosity=2)
"""
  参数化  断言 操作
  unittest 中自己写参数化还是挺麻烦的,跟 pytest 的 pytest.mark.parametrize 比差太多。
有两个辅助的库,专门解决 unittest 的参数化问题:ddt 和 parameterized。
  """
"""
pip install BeautifulReport -i https://pypi.tuna.tsinghua.edu.cn/simple/
必须是run**.py文件


"""
import unittest   #单元测试模块
from script.script import Test_login
from BeautifulReport import BeautifulReport   #导入BeautifulReport模块,这个模块也是生成报告的模块,但是比HTMLTestRunner模板好看

suite = unittest.TestSuite()  # 定义一个测试集合
suite.addTest(unittest.makeSuite(Test_login))  # 把写的用例加进来(将Test_login类)加进来
run = BeautifulReport(suite) # 实例化BeautifulReport模块
run.report(filename='report',description='百度登录自动化报告',report_dir='report')  # (报告名称 , 报告描述) :必填项
# 报告存放路径:report目录与run_case.py文件同级 , 故不能../report,直接report
# report和/report 是不同的地址

"""
F.  # 一个成功,一个失败
测试已全部完成, 可打开 C:\\Users\01397622\PycharmProjects\Selenium-study\SF-tech\report.html 查看报告
"""


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值