简单学习Selenium的PageFactory模式

xiaoxiao2021-02-28  44

笃信好学,自然宽和

PageFactory是一种页面设计模式;主要技术点是@FindBy注解;

从以下对比来看看使用PageFactory,与不使用有什么区别;

百度首页为例;

1、不用PageFactory模式:

String url = "https://www.baidu.com"; System.setProperty("webdriver.gecko.driver", "F:\\eclipse_workspace\selenium3.12test\\geckodriver-v0.20.0-win64\\geckodriver.exe"); System.setProperty("webdriver.firefox.bin", "E:/Program Files/Mozilla Firefox/firefox.exe"); WebDriver driver = new FirefoxDriver(); driver.get(url); //主要的变化是以下这两行; driver.findElement(By.id("kw")).sendKeys("selenium 3.12.0"); driver.findElement(By.id("su")).click();

2、使用PageFactory模式:

WebDriver driver ; String url = "https://www.baidu.com"; @FindBy(id="kw") public WebElement kw; @FindBy(id="su") public WebElement su; driver.get(url); PageFactory.initElements(driver, this); kw.sendKeys("selenium 3.12.0"); su.click(); ......原来需要通过一大串方法才能获得的页面元素变得简单了;页面元素获取与逻辑业务操作的分离,使得对页面元素得到更好的维护。

备注:若是@FindBy没有指定查找属性,它会默认的查找id的属性,id没有再查找是name属性,如果还没有找到就会报错。 还支持Xpath,CSS,classname等多种类型。

转载请注明原文地址: https://www.6miu.com/read-2627100.html

最新回复(0)