Selenium Page Object Model-FindAll annotation

可以用FindBy注释来代替driver.findelement(By x).

//定义一个element
@FindAll({@FindBy(xpath = "yourxpath")})
public WebElement; resultElements;
//一次定义多个elements
@FindAll({@FindBy(xpath = "yourfirstxpath"),@FindBy(xpath = "yoursecondxpath"),@FindBy(xpath = "yourThirddxpath")})
public List<WebElement> resultElements;
//一个项目中的例子
package com.perficient.test.cat.com.reg.basepages;

import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class Augers{

    //Labels for New Products and Rental Products
    @FindBy(xpath = "//*[@class='filters']/li/input")
    public List<WebElement> listOfLabelsOfBrowserInventory;

    //Augers title
    @FindBy(xpath="//div/h2")
    public WebElement titleOfAugers;

    //currency dropdown
    @FindBy(xpath="//div/select[@name='currency']")
    public WebElement dropdownOfCurrency;

    //MSRP disclaimer
    @FindBy(xpath="//div/select[@name='currency']")
    public WebElement MSRPDisclaimer;

    //Availability Statement
    @FindBy(xpath="//div[@class='availability-statement']")
    public WebElement Availabity;
}

FindBy的10种定位元素的方法。

这里写图片描述

Selenium Page Object Model(POM)是一种设计模式,主要用于自动化测试中的页面管理和操作。它将页面元素和相关的交互逻辑封装到独立的类中,让测试代码更清晰、易读且易于维护。以下是使用POM的一个简单实例: ```python # 定义Page对象,代表一个具体的页面 class LoginPage: def __init__(self, driver): self.driver = driver self.username_input = driver.find_element_by_id("username") self.password_input = driver.find_element_by_name("password") self.login_button = driver.find_element_by_css_selector(".login-button") # 登录方法 def login(self, username, password): self.username_input.clear() self.username_input.send_keys(username) self.password_input.clear() self.password_input.send_keys(password) self.login_button.click() # 在测试中使用Page对象 def test_login(): driver = webdriver.Chrome() # 初始化浏览器驱动 page = LoginPage(driver) # 创建LoginPage对象 # 执行登录操作 page.login("test_user", "test_password") # 输入用户名和密码,然后点击登录 # 检查登录后的状态... assert "Welcome" in driver.page_source driver.quit() # 关闭浏览器 # 这样,每次想要登录的时候,只需要调用page.login方法即可,不需要关心底层的DOM查找 ``` 在这个例子中,`LoginPage`类包含了对登录页所有重要元素的操作方法(如`clear`、`send_keys`和`click`)。当实际进行测试时,我们只需要关注如何使用这些方法,而无需反复寻找元素位置。这使得测试代码更具模块性和复用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值