0


web自动化测试--tpshop商城案例

示例1:(第一次写的代码)

from selenium import webdriver
import time
# 创建浏览器对象
driver=webdriver.Chrome('E:\PyCharmCommunityEdition2022.2\workspaces\works\web自动化测试\chromedriver.exe')
# 访问项目
driver.get('http://127.0.0.1/index.php')
driver.maximize_window()
driver.implicitly_wait(20)
# 点击登录
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[2]/a[1]').click()
# 输入手机号
driver.find_element_by_id('username').send_keys('17657363531')
# 输入密码
driver.find_element_by_id('password').send_keys('xqy001220')
# 输入验证码
driver.find_element_by_id('verify_code').send_keys(8888)
# 点击登录
driver.find_element_by_class_name('J-login-submit').click()
# 点击返回商城首页
driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/ul/li[1]/a').click()
# 搜索小米
driver.find_element_by_id('q').send_keys('小米')
# 点击搜索
driver.find_element_by_class_name('ecsc-search-button').click()
# 点击图片
driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div[2]/ul/li[1]/div/div[1]/a/img').click()
# 选择内存
driver.find_element_by_id('goods_spec_a_102').click()
# 点击加入购物车
driver.find_element_by_id('join_cart').click()
# 关闭页面
time.sleep(10)
driver.quit()

示例2:(结合之前学习的面向对象对示例1进行优化)

from selenium import webdriver
import time,unittest
class TestClass(unittest.TestCase):
    def setUp(self) -> None:
        # 创建浏览器对象
        self.driver = webdriver.Chrome('E:\PyCharmCommunityEdition2022.2\workspaces\works\web自动化测试\chromedriver.exe')
        # 访问项目
        self.driver.get('http://127.0.0.1/index.php')
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
    def test(self):
        driver=self.driver
        # 点击登录
        driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[2]/a[1]').click()
        # 输入手机号
        driver.find_element_by_id('username').send_keys('17657363531')
        # 输入密码
        driver.find_element_by_id('password').send_keys('xqy001220')
        # 输入验证码
        driver.find_element_by_id('verify_code').send_keys(8888)
        # 点击登录
        driver.find_element_by_class_name('J-login-submit').click()
        # 点击返回商城首页
        driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/ul/li[1]/a').click()
        # 搜索小米
        driver.find_element_by_id('q').send_keys('小米')
        # 点击搜索
        driver.find_element_by_class_name('ecsc-search-button').click()
        # 点击图片
        driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div[2]/ul/li[1]/div/div[1]/a/img').click()
        # 选择内存
        driver.find_element_by_id('goods_spec_a_102').click()
        # 点击加入购物车
        driver.find_element_by_id('join_cart').click()
    def tearDown(self) -> None:
        # 关闭页面
        time.sleep(10)
        self.driver.quit()
if __name__ == '__main__':
    unittest.main()

示例3:(po模型对示例2进行优化)

前提:

梳理业务,对界面进行拆分

(1)创建basepage包,用来存放公共方法

public.py代码:
from selenium.webdriver.common.action_chains import ActionChains   #鼠标操作

class PublicMthod():
    def __init__(self,driver):
        self.driver=driver
    # 访问项目
    def get_url(self,url):
        self.driver.get(url)
    # 元素定位
    def get_element(self,*method):
        return self.driver.find_element(*method)
    # 点击元素
    def click_element(self,*method):
        ActionChains(self.driver).click(self.get_element(*method)).perform()
    # 输入数据
    def input_data(self,text,*method):
        self.get_element(*method).send_keys(text)
    # 多窗口切换
    def switch_window(self,n):
        h=self.driver.window_handles       #获取句柄
        self.driver.switch_to.window(h[n]) #切换窗口
        # self.driver.switch_to.window(self.driver.window_handles[n])
    # 表单切换
    def switch_frame(self,*method):
        self.driver.switch_to.frame(self.driver.get_element(*method))

(2)创建page包,有几个界面就有几个page页

pageone.py代码:

from selenium.webdriver.common.by import By
from works.web自动化测试.tpshop商城.po模型.basepage.public import PublicMthod
class PageOneClass(PublicMthod):
    def __init__(self,driver):
        PublicMthod.__init__(self, driver)
    # 访问项目
    def enterurl(self,url):
        self.get_url(url)
    # 点击登录
    def clicklogin(self):
        self.click_element(By.XPATH,'/html/body/div[1]/div[1]/div/div/div[2]/a[1]')

pagetwo.py代码:

from selenium.webdriver.common.by import By
from works.web自动化测试.tpshop商城.po模型.basepage.public import PublicMthod
class PageTwoClass(PublicMthod):
    def __init__(self,driver):
        PublicMthod.__init__(self, driver)
    # 输入手机号
    def phone(self,text):
        self.input_data(text,By.ID,'username')
    # 输入密码
    def password(self,text):
        self.input_data(text,By.ID,'password')
    # 输入验证码
    def code(self,text):
        self.input_data(text,By.ID,'verify_code')
    # 点击登录
    def clicklodin(self):
        self.click_element(By.CLASS_NAME,'J-login-submit')

pagethree.py代码:

from selenium.webdriver.common.by import By
from works.web自动化测试.tpshop商城.po模型.basepage.public import PublicMthod
class PageThreeClass(PublicMthod):
    def __init__(self,driver):
        PublicMthod.__init__(self, driver)
    # 点击返回商城首页
    def clickhome(self):
        self.click_element(By.XPATH,'/html/body/div[2]/div/div[3]/ul/li[1]/a')

pagefour.py代码:

from selenium.webdriver.common.by import By
from works.web自动化测试.tpshop商城.po模型.basepage.public import PublicMthod
class PageFourClass(PublicMthod):
    def __init__(self,driver):
        PublicMthod.__init__(self, driver)
    # 输入数据
    def input(self,text):
        self.input_data(text,By.ID,'q')
    # 点击搜索
    def clicksearch(self):
        self.click_element(By.CLASS_NAME,'ecsc-search-button')
    # 点击图片
    def clickphoto(self):
        self.click_element(By.XPATH,'/html/body/div[4]/div/div[2]/div[2]/ul/li[1]/div/div[1]/a/img')

pagefive.py代码:

from selenium.webdriver.common.by import By
from works.web自动化测试.tpshop商城.po模型.basepage.public import PublicMthod
class PageFiveClass(PublicMthod):
    def __init__(self,driver):
        PublicMthod.__init__(self, driver)
    # 选择内存
    def clickmemory(self):
        self.click_element(By.ID,'goods_spec_a_102')
    # 点击加入购物车
    def click_shopcar(self):
        self.click_element(By.ID,'join_cart')

(3)创建testcase包,编写测试用例

test.py代码:

import unittest,time
from selenium import webdriver
from works.web自动化测试.tpshop商城.po模型.pages.pageone import PageOneClass
from works.web自动化测试.tpshop商城.po模型.pages.pagetwo import PageTwoClass
from works.web自动化测试.tpshop商城.po模型.pages.pagethree import PageThreeClass
from works.web自动化测试.tpshop商城.po模型.pages.pagefour import PageFourClass
from works.web自动化测试.tpshop商城.po模型.pages.pagefive import PageFiveClass
class TPshopClass(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.driver=webdriver.Chrome('E:\PyCharmCommunityEdition2022.2\workspaces\works\web自动化测试\chromedriver.exe')
        cls.driver.maximize_window()
        cls.driver.implicitly_wait(30)
    def test001(self):
        pageone=PageOneClass(self.driver)
        pageone.enterurl('http://127.0.0.1')
        pageone.clicklogin()
    def test002(self):
        pagetwo=PageTwoClass(self.driver)
        pagetwo.phone('17657363531')
        pagetwo.password('xqy001220')
        pagetwo.code(8888)
        pagetwo.clicklodin()
    def test003(self):
        pagethree=PageThreeClass(self.driver)
        pagethree.clickhome()
    def test004(self):
        pagefour=PageFourClass(self.driver)
        pagefour.input('小米')
        pagefour.clicksearch()
        pagefour.clickphoto()
    def test005(self):
        pagefive=PageFiveClass(self.driver)
        pagefive.clickmemory()
        pagefive.click_shopcar()
    @classmethod
    def tearDownClass(cls) -> None:
        time.sleep(5)
        cls.driver.quit()

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

(4)结合unittest生成测试报告

import unittest
from works.web自动化测试.tpshop商城.po模型.common.HTMLTestRunner import HTMLTestRunner
from works.web自动化测试.tpshop商城.po模型.testcase.test import TPshopClass
class TestReport():
    def test_report(self):
        # 创建测试套件
        suite=unittest.TestSuite()
        list=['test001','test002','test003','test004','test005']
        for i in list:
            suite.addTest(TPshopClass(i))
        with open('../poreport.html','wb') as f:
            HTMLTestRunner(
                stream=f,
                verbosity=2,
                title='PO模型测试报告',
                description='tpshop的测试报告'
            ).run(suite)
if __name__ == '__main__':
    unittest.main()

报告展示:


本文转载自: https://blog.csdn.net/qq_44954371/article/details/126632630
版权归原作者 got Ideal 所有, 如有侵权,请联系我们删除。

“web自动化测试--tpshop商城案例”的评论:

还没有评论