62新一代自动化测试神器playwright+python系列课程_aw封装

AW封装

AW也就是ActionWord,在UI自动化测试领域,通俗理解就是在页面上的各种各样的动作,例如点击、输入、获取元素上的文字、点击警告窗口等。像playwright中的click、fill等也是aw,本文要介绍的AW的封装就是在playwright提供的api的基础上进一步封装,例如将元素定位和点击、输入等动作封装起来,可以使得我们写脚本时简化脚本的编写,接下来我们来看几个AW封装的案例:


# '''

# author=测试-老姜   交流微信/QQ:349940839

# 欢迎添加微信或QQ,加入学习群共同学习交流。

# QQ交流群号:877498247

# 西安的朋友欢迎当面交流。

# '''

from playwright.sync_api import Playwright, sync_playwright, expect

def element_click(locator):

    locator.click()

def element_fill(locator,value ):

    locator.clear()

    locator.fill(value )

def get_element_text(locator):

    locator.inner_text()



def locat_element(page,selector):

    #定位元素

    return page.locator(selector)

def locat_frame_element(frame,selector):

    #定位frame框架内的元素

    return frame.locator(selector)

def locat_frame(page,selector):

  #定位frame框架,可以传字符串或列表,传入列表是可以实现多层frame框架的连续定位

    if type(selector) is str:

        return page.frame_locator(selector)

    elif type(selector) is list:

        frame = page.frame_locator(selector)

        for i in range(1,len(selector)):

            frame = frame.frame_locator(selector[i])

        return frame

   

def locat_element_click(page,selector):

    #定位元素,点击

    loc = locat_element(page,selector)

    element_click(loc)

def locat_frame_element_click(page,frame_selector,selector):

     #定位frame框架内元素,点击

    frame = locat_frame(page,frame_selector)

    loc = locat_frame_element(frame,selector)

    element_click(loc)

def locat_element_fill(page,selector,value):

    #定位元素,输入

    loc = locat_element(page,selector)

    element_fill(loc,value)

def locat_frame_element_fill(page,frame_selector,selector,value):

    #定位frame框架内元素,输入

    frame = locat_frame(page,frame_selector)

    loc = locat_frame_element(frame,selector)

    element_fill(loc,value)



def locat_element_get_text(page,selector):

    # 定位元素并获取元素上文本

    loc = locat_element(page,selector)

    get_element_text(loc)

def locat_frame_element_get_text(page,frame_selector,selector):

    #定位frame框架内元素,获取元素上文本

    frame = locat_frame(page,frame_selector)

    loc = locat_frame_element(frame,selector)

    get_element_text(loc)

基于封装的AW,我们在编写脚本时,可以直接使用我们封装的AW来完成脚本编写,我们以修改密码功能为例来看一下我们封装的AW的应用

我们封装的修改密码的脚本,主要过程就是进入修改密码页面,然后完成密码修改,并且返回提示信息,我们可以以提示信息来断言。

实践代码:


# '''

# author=测试-老姜   交流微信/QQ:349940839

# 欢迎添加微信或QQ,加入学习群共同学习交流。

# QQ交流群号:877498247

# 西安的朋友欢迎当面交流。

# '''

import _44_aw as aw

from playwright.sync_api import Page,sync_playwright

class Modify_Password_Page():

    def __init__(self,page:Page) -> None:

        self.page=page

        self.modify_password_link = 'text=密码修改'

        self.Modify_Password_frame = 'iframe'

        self.old_password_input = '#oldpassword'

        self.new_password_input = '#newpassword'

        self.password_again_input = '#passwordagain'

        self.tipe_info_ele = '[style="max-width:440px;"]'

        self.save_button = '[title="保存"]'

    def goto_modify_password_page(self):

        #进入修改密码页面

        aw.locat_element_click(self.page,self.modify_password_link)

   

    def modify_password(self,oldpassword,newpassword,passwordagain):

        # 完成密码修改,并返回保存后的提示信息

        aw.locat_frame_element_fill(self.page,self.Modify_Password_frame,self.old_password_input,oldpassword)

        aw.locat_frame_element_fill(self.page,self.Modify_Password_frame,self.new_password_input,newpassword)

        aw.locat_frame_element_fill(self.page,self.Modify_Password_frame,self.password_again_input,passwordagain)

        aw.locat_frame_element_click(self.page,self.Modify_Password_frame,self.save_button)

        tipe_info =aw.locat_element_get_text(self.page,self.tipe_info_ele)

        return tipe_info

posted @ 2024-03-21 11:08  测试_老姜  阅读(88)  评论(0)    收藏  举报