Skip to content

【实战练习】web自动化测试实战

【实战练习】web 自动化测试实战

实战需求

点击查看实战详情

实现思路

uml diagram

代码实现

PageObject 类实现

封装页面内部实现,用例层仅调用相关方法。

python 实现
class AddMemberPage(BasePage):
    __NAME = (By.ID, 'username')  # 姓名输入框
    __MID = (By.ID, 'memberAdd_acctid')  # 账号输入框
    __PHONE_NUM = (By.ID, 'memberAdd_phone')  # 手机号输入框
    __MID_TIP = (By.CSS_SELECTOR, "#memberAdd_acctid+div")  # 账号提示
    __PHONE_TIP = (By.CSS_SELECTOR, ".ww_telInput+div")  # 手机号提示
    __SAVE_BUTTON = (By.CSS_SELECTOR, ".qui_btn.ww_btn.js_btn_save")  # 保存按钮
    __CANCEL_BUTTON = (By.CSS_SELECTOR, ".qui_btn.ww_btn.js_btn_cancel")  # 取消按钮

    def input_member_info(self, mname, mid, phone_num):
        '''
        填写成员信息
        :return:
        '''
        self.wait_until(expected_conditions.visibility_of_element_located(self.__NAME))
        # 姓名
        self.find_ele(self.__NAME).send_keys(mname)
        # 账号
        self.find_ele(self.__MID).send_keys(mid)
        # 手机号
        self.find_ele(self.__PHONE_NUM).send_keys(phone_num)
        return self

    def save_info_success(self):
        '''
        点击保存成功,进入通讯录页面
        :return:
        '''
        # 点击保存
        self.find_eles(self.__SAVE_BUTTON)[0].click()
        from web_po.page.contact_page import ContactPage
        return ContactPage(self.driver)

    def save_info_fail(self):
        '''
        点击保存失败,停留在本页
        :return:
        '''
        # 点击保存
        self.driver.find_elements(By.CSS_SELECTOR, '.qui_btn.ww_btn.js_btn_save')[0].click()
        return self

    def cancel_info(self):
        '''
        点击取消,返回通讯录列表
        :return:
        '''
        # 点击取消按钮
        self.find_eles(self.__CANCEL_BUTTON)[0].click()
        from web_po.page.contact_page import ContactPage
        return ContactPage(self.driver)

    def get_error_tip_info(self):
        '''
        获取页面错误信息
        :return:
        '''
        # 显示等待错误提示
        self.wait_until(expected_conditions.visibility_of_element_located(
            (By.CSS_SELECTOR, '.member_edit_item_right.ww_inputWithTips_WithErr')))
        error_info = self.find_eles(By.CSS_SELECTOR, '.member_edit_item_right.ww_inputWithTips_WithErr')
        infos = []
        # 存储错误 tips 到列表
        for error in error_info:
            infos.append(error.text)
        return infos
java 实现
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

@Slf4j
public class MemberPage extends BasePage {
    private By usernameInput = By.name("username");
    private By accountInput = By.id("memberAdd_acctid");
    private By phoneInput = By.xpath("//input[@name=\"mobile\"]");
    private By saveBtn = By.xpath("//a[text()=\"保存\"]");
    private By repeatTips = By.cssSelector(".member_edit_item_Account .ww_inputWithTips_tips");
    private By cancelBtn = By.linkText("取消");
    @Override
    public void loadPage() {
        // 等待元素可见
        wait.until(ExpectedConditions.visibilityOfElementLocated(saveBtn));
    }

    public MemberPage(WebDriver driver) {
        super(driver);
    }

    public void inputMember(String username, String account, String phone) {
        log.info("添加成员数据username:{},account:{},phone:{}", username, account, phone);
        find(usernameInput).sendKeys(username);
        find(accountInput).sendKeys(account);
        find(phoneInput).sendKeys(phone);
    }

    public ConcatPage clickSaveBtnToConcatPage() {
        log.info("点击保存后,跳转回通讯录页面");
        find(saveBtn).click();
        return new ConcatPage(driver);
    }

    /**
     * 点击保存按钮,保存失败
     * @return
     */
    public void clickSaveBtnFail() {
        log.info("点击保存后,跳转回通讯录页面");
        find(saveBtn).click();
    }

    /**
     * 点击取消按钮,跳转回成员页面
     */
    public ConcatPage clickCancelBtnToMemberPage(){
        log.info("点击取消按钮,跳转回成员页面");
        find(cancelBtn).click();
        return new ConcatPage(driver);
    }

    /**
     * 获取提示账号重复文本
     */
    public String getRepeatTipsText(){
        log.info("获取提示账号重复文本");
        return find(repeatTips).getText();
    }
}

用例实现

通过调用PageObject方法完成测试用例

python 实现
@pytest.mark.parametrize('name,mid,phone', [['小王01', 'xiaowang01', '13612345001']])
def test_add_member_fail(self, name, mid, phone):
    '''
    用例步骤
    1. 添加成员成功
        信息:name,mid,phone
    2. 添加成员失败
        信息:new name,mid,new phone
    断言添加成员成功
    断言提示账号重复信息
    断言通讯录列表不存在添加失败成员
    :return:
    '''
    # 添加成员成功
    names_success = self.main.login(). \
        goto_contact(). \
        goto_add_member(). \
        input_member_info(name, mid, phone). \
        save_info_success(). \
        get_member_info()
    # 准备数据
    new_name = name + 'new'
    new_phone = phone[0:5] + time.strftime('%H%M%S')
    # 添加成员失败
    result = self.main.login(). \
        goto_contact(). \
        goto_add_member(). \
        input_member_info(new_name, mid, new_phone). \
        save_info_fail()
    # 获取提示信息
    tips = result.get_error_tip_info()
    # 获取成员列表
    names_fail = result.cancel_info().get_member_info()
    assert name in names_success
    assert f'该账号已被“{name}”占有' in tips
    assert new_name not in names_fail
java 实现
    /**
     * 添加重复成员失败功能
     * 1. 进入添加成员页面
     * 2. 输入成员信息点击保存
     * 3. 检查成员列表页面
     * 4. 进入添加成员页面
     * 5. 输入重复的账号
     * 6. 点击保存
     * 7. 检查成员列表是否出现重复的账号信息
     */
    @DisplayName("添加重复成员失败功能")
    @Test
    void addMemberRepeatTest() {
        // 进入添加成员页面
        Allure.step("进入添加成员页面");
        ConcatPage concatPage = mainPage.toConcatPage();
        MemberPage memberPage = concatPage.toMemberPage();

        //输入成员信息点击保存
        Allure.step("输入成员信息点击保存");
        String username = "李四";
        String account = Long.toString(System.currentTimeMillis());
        String phone = account.substring(0, 11);
        memberPage.inputMember(username,account,phone);
        concatPage = memberPage.clickSaveBtnToConcatPage();

        //检查成员列表页面
        Allure.step("检查成员列表页面");
        List<String> memberTexts = concatPage.getMemberTexts();

        assertThat(memberTexts, hasItems(username));
        assertThat(memberTexts, hasItems(phone));


        memberPage = concatPage.toMemberPage();
        String new_username = "王五";
        String repeat_account = account;
        String new_phone = account.substring(1, 12);
        memberPage.inputMember(new_username,repeat_account,new_phone);
        Allure.step("断言是否出现重复账号提示");
        assertThat(memberPage.getRepeatTipsText(),equalTo("该账号已被“"+username+"”占有"));
        memberPage.clickSaveBtnFail();
        concatPage  = memberPage.clickCancelBtnToMemberPage();
        // 断言添加成员信息是否包含在成员列表中
        Allure.step("断言添加成员信息是否包含在成员列表中");
        List<String> new_memberTexts = concatPage.getMemberTexts();
        // 判断断言列表数据是否正常
        assertThat(new_memberTexts, not(hasItems(new_phone)));
    }