10、Selenium WebDriver二次封装

一、未封装webdriver

原始代码

# coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import unittest


class login(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com/")

    def test_login(self):
        driver = self.driver
        driver.find_element(By.LINK_TEXT, u"登录").click()
        time.sleep(1)
        driver.find_element(By.ID, "TANGRAM__PSP_10__footerULoginBtn").click()
        driver.find_element(By.ID, "TANGRAM__PSP_10__userName").clear()
        driver.find_element(By.ID, "TANGRAM__PSP_10__userName").send_keys("username")
        driver.find_element(By.ID, "TANGRAM__PSP_10__password").clear()
        driver.find_element(By.ID, "TANGRAM__PSP_10__password").send_keys("password")
        driver.find_element(By.ID, "TANGRAM__PSP_10__submit").click()
        time.sleep(3)
        text = driver.find_element(By.ID, "TANGRAM__39__content_msgtext").text
        msg = u"您的帐号可能存在安全风险,为了确保为您本人操作,请先进行安全验证。"
        self.assertEqual(text, msg)

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


if __name__ == '__main__':
    unittest.main()

二、初步封装

# coding=utf-8
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import unittest
import time


class Test(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com/")

    def find_element(self, loc):
        u"""重写find_element方法"""
        try:
            WebDriverWait(self.driver, 15).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except:
            print ("%s 页面中未能找到%s元素" % (self,loc))

    def clear_keys(self, loc):
        u"""重写清除文本内容的方法"""
        time.sleep(1)
        self.find_element(loc).clear()

    def sent_keys(self, loc, value):
        u"""重写输入文本信息"""
        self.clear_keys(loc)
        self.find_element(loc).send_keys(value)

    def click_button(self, loc):
        self.find_element(loc).click()

    def test_case1(self):
        login_button = (By.LINK_TEXT, u'登录')
        choose_login = (By.ID, 'TANGRAM__PSP_10__footerULoginBtn')
        # error_message = (By.ID, 'TANGRAM__PSP_10__error')
        user_loc = (By.ID, 'TANGRAM__PSP_10__userName')
        pwd_loc = (By.ID, 'TANGRAM__PSP_10__password')
        submit = (By.ID, 'TANGRAM__PSP_10__submit')
        verify_message = (By.ID, 'TANGRAM__39__content_msgtext')

        self.click_button(login_button)

        self.find_element(login_button).click()
        self.find_element(choose_login).click()
        self.sent_keys(user_loc, 'username')
        self.sent_keys(pwd_loc, 'password')
        self.find_element(submit).click()
        test_text = self.find_element(verify_message).text
        print (test_text)
        self.assertEqual(test_text, u"您的帐号可能存在安全风险,为了确保为您本人操作,请先进行安全验证。")
        time.sleep(3)

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


if __name__ == '__main__':
    unittest.main(verbosity=2)

看一下find_element方法,这里设置了元素等待。如果元素可见就返回这个元素

    def find_element(self, loc):
        u"""重写find_element方法"""
        try:
            WebDriverWait(self.driver, 15).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except:
            print ("%s 页面中未能找到%s元素" % (self,loc))

三、进一步进行封装

这里为了便于后续的使用将页面元素进行分离,公共方法进行分离

封装API类,这里就是webdriver的封装

# coding=utf-8

from selenium.webdriver.support.wait import WebDriverWait
import time


class api(object):
    u"""selenium二次封装"""

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

    def find_element(self, loc):
        u"""重写find_element方法"""
        try:
            WebDriverWait(self.driver, 15).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except:
            print ("%s 页面中未能找到%s元素" % (self, loc))

    def clear_keys(self, loc):
        u"""重写清除文本内容的方法"""
        time.sleep(1)
        self.find_element(loc).clear()

    def sent_keys(self, loc, value):
        u"""重写输入文本信息"""
        self.clear_keys(loc)
        self.find_element(loc).send_keys(value)

    def click_button(self, loc):
        self.find_element(loc).click()

页面:

# coding=utf-8

from selenium.webdriver.common.by import By


login_button = (By.LINK_TEXT, u'登录')
choose_login = (By.ID, 'TANGRAM__PSP_10__footerULoginBtn')
error_message = (By.ID, 'TANGRAM__PSP_10__error')
user_loc = (By.ID, 'TANGRAM__PSP_10__userName')
pwd_loc = (By.ID, 'TANGRAM__PSP_10__password')
submit = (By.ID, 'TANGRAM__PSP_10__submit')
verify_message = (By.ID, 'TANGRAM__39__content_msgtext')

测试类:

# coding=utf-8

from selenium import webdriver
from api2 import api2
import unittest
import page


class Test(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com")

    def test_api2(self):
        self.api2 = api2(driver=self.driver)
        self.api2.find_element(page.login_button).click()
        self.api2.find_element(page.choose_login).click()
        self.api2.clear_keys(page.user_loc)
        self.api2.sent_keys(page.user_loc, "username")
        self.api2.clear_keys(page.pwd_loc)
        self.api2.sent_keys(page.pwd_loc, "password")
        self.api2.click_button(page.submit)
        text = self.api2.find_element(page.verify_message).text
        msg = u"您的帐号可能存在安全风险,为了确保为您本人操作,请先进行安全验证。"
        self.assertEqual(text, msg)

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


if __name__ == '__main__':
    unittest.main()

总结,这里初步的体现了POM(PageObjectModel)思想,对页面元素进行分离操作。这样做的好处就是我们如果要改变输入的参数直接可以很快的定位到要输入的数据。后面还可以用Excel对数据操作进行管理

猜你喜欢

转载自blog.csdn.net/qq969887453/article/details/89298607