掌握Selenium中元素缓存技巧,提高测试效率

我们有讲探索Selenium中的Page Factory及其使用方法,优化测试代码这篇文章,知道了可以在Selenium中使用注解来操作元素定位,本篇文章我们再来看下如何在Selenium中使用缓存

页面对象模型UI自动化测试中的一种很好的设计模式,我们使用@FindBy@FindAll注释来标记Page Object中的WebElement

本次要讲的@CacheLookup是一个非常重要但被忽视的注释,它可以帮助我们使测试运行得更快。

Selenium中,CacheLookup是一个注解,用于标记页面对象模型(Page Object Model)中的元素。当使用CacheLookup注解时,Selenium会在第一次查找元素时将其缓存起来,以便后续的查找操作可以直接使用缓存的元素,而不需要再次查找页面上的元素。

举个常规的例子 / EXAMPLE

首先让我们了解一下SeleniumPageObject模型中什么时候调用FindElement。每当使用页面对象中的WebElement执行某些操作时,都会触发FindElement从网页中查找WebElement的最新版本。这个查找基本上是一个FindElement REST请求到浏览器的Web驱动程序。这个查找是代码中最耗时的部分之一。

我们用Page Object模式来创建一个demo

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.How;


/**

 * @author 

 * @version 创建时间:2023/12/10

 * 类说明:PO

 */

public class TesterRoadPO {

    @FindBy(how = How.NAME, using = "firstname")

    public WebElement firsName;


    @FindBy(how = How.NAME, using = "lastname")

    public WebElement lastName;


}
import io.github.bonigarcia.wdm.WebDriverManager;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.support.PageFactory;

import org.testng.annotations.Test;


/**

 * @author 

 * @version 创建时间:2023/12/10

 * 类说明:demo

 */

public class TesterRoadDemo {


    @Test

    public void testerRoadDemo() {


        ChromeOptions chromeOptions = new ChromeOptions();

        chromeOptions.addArguments("--remote-allow-origins=*");

        // 为了理解使用PageObjects对web元素的操作是如何工作的,

        // 我们将保存chrome驱动的所有日志。下面的语句对我们有所帮助

        // 将所有日志保存在名为TesterRoadLog.txt的文件中

        System.setProperty("webdriver.chrome.logfile", "src/main/resources/TesterRoadLog.txt");

        WebDriverManager.chromedriver().setup();

        WebDriver driver = new ChromeDriver(chromeOptions);

        driver.get("http://127.0.0.1:8080/");


        // 初始化Page object

        TesterRoadPO pageObject = PageFactory.initElements(driver, TesterRoadPO.class);


        pageObject.firsName.sendKeys("Road");

        pageObject.lastName.sendKeys("Tester");


        pageObject.firsName.getText();

        pageObject.lastName.getText();


        driver.close();

        driver.quit();

    }

}

在上述代码中,调用了四次FindBy,查找元素和获取文本,我们来看下日志文件

[12.654][INFO]: COMMAND FindElement {

   "using": "name",

   "value": "firstname"

}

[12.654][INFO]: Waiting for pending navigations...

[12.717][INFO]: Done waiting for pending navigations. Status: ok

[12.851][INFO]: Waiting for pending navigations...

[12.854][INFO]: Done waiting for pending navigations. Status: ok

[12.854][INFO]: RESPONSE FindElement {

   "ELEMENT": "0.8984444413515806-1"

}


[12.860][INFO]: COMMAND TypeElement {

   "id": "0.8984444413515806-1",

   "value": [ "Road" ]

}

[12.860][INFO]: Waiting for pending navigations...

[12.861][INFO]: Done waiting for pending navigations. Status: ok

[13.045][INFO]: Waiting for pending navigations...

[13.050][INFO]: Done waiting for pending navigations. Status: ok

[13.050][INFO]: RESPONSE TypeElement


[13.053][INFO]: COMMAND FindElement {

   "using": "name",

   "value": "lastname"

}

[13.053][INFO]: Waiting for pending navigations...

[13.054][INFO]: Done waiting for pending navigations. Status: ok

[13.074][INFO]: Waiting for pending navigations...

[13.082][INFO]: Done waiting for pending navigations. Status: ok

[13.082][INFO]: RESPONSE FindElement {

   "ELEMENT": "0.8984444413515806-2"

}

[13.086][INFO]: COMMAND TypeElement {

   "id": "0.8984444413515806-2",

   "value": [ "Tester" ]

}

在日志中,我们可以看到对于每个语句都有对FindElementTypeElement调用。对日志文件的进一步调查将显示,对于每个getText调用,都将有一个FindElementGetElementText调用。

我们在WebElement上执行的每一个操作都会进行调用,那么无疑将会花费更多的时间。那么我们如何才能使的测试更快,花费更少的时间?

CacheLookup 注解/ @CacheLookup

@CacheLookup可以帮助我们控制何时缓存WebElement,何时不缓存。当这个注释应用于WebElement时,它会让Selenium保留WebElement的缓存,而不是每次都从网页中搜索WebElement。可以节省大量的时间。

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.CacheLookup;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.How;


/**

 * @author 作者:测试工程师成长之路

 * @version 创建时间:2023/12/10

 * 类说明:PO中使用CacheLookup缓存元素

 */

public class TesterRoadPO {

    @FindBy(how = How.NAME, using = "firstname")

    public WebElement firsName;


    @FindBy(how = How.NAME, using = "firstname")

    @CacheLookup

    public WebElement firsNameCached;

}
import io.github.bonigarcia.wdm.WebDriverManager;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.support.PageFactory;

import org.testng.annotations.Test;


/**

 * @author 作者:测试工程师成长之路

 * @version 创建时间:2023/12/10

 * 类说明:未使用CacheLookup和使用CacheLookup对比

 */

public class TesterRoadDemo {


    @Test

    public void testerRoadDemo() {


        ChromeOptions chromeOptions = new ChromeOptions();

        chromeOptions.addArguments("--remote-allow-origins=*");

        // 为了理解使用PageObjects对web元素的操作是如何工作的,

        // 我们将保存chrome驱动的所有日志。下面的语句对我们有所帮助

        // 将所有日志保存在名为TesterRoadLog.txt的文件中

        System.setProperty("webdriver.chrome.logfile", "src/main/resources/TesterRoadLog.txt");

        WebDriverManager.chromedriver().setup();

        WebDriver driver = new ChromeDriver(chromeOptions);

        driver.get("http://127.0.0.1:8080/");


        // 初始化Page object

        TesterRoadPO pageObject = PageFactory.initElements(driver, TesterRoadPO.class);


        pageObject.firsName.sendKeys("TesterRoad");

        // 首先尝试从未缓存的WebElement获取文本。

        // 查看执行1000个getText操作的时间

        long withoutCacheStartTime = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {

            pageObject.firsName.getText();

        }

        long withoutCacheEndTime = System.currentTimeMillis();

        System.out.println("未使用缓存花费的时间: " + ((withoutCacheEndTime - withoutCacheStartTime) / 1000));


        // 现在我们在缓存的元素上重复同样的操作

        // 查看执行相同操作1000次所需的时间

        long withCacheStartTime = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {

            pageObject.firsNameCached.getText();

        }

        long withCacheEndTime = System.currentTimeMillis();

        System.out.println("使用缓存花费的时间: " + ((withCacheEndTime - withCacheStartTime) / 1000));


        driver.close();

        driver.quit();

    }

}

图片

在输出中,我们可以清楚地看到,与非缓存版本相比,缓存版本的WebElement执行相同操作的时间减少了50%

写在后面 / WRITE LATER

既然使用CacheLookup可以提高50%的效率,那么我们在项目中全部使用CacheLookup岂不美哉,NONONO,那到不尽然,比如像动态元素(像id之类,很多都是动态的)就不要使用CacheLookup了,我们要在合适的场景下去合理使用CacheLookup

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

​​​软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值