selenium高级封装

selenium是非常好的UI自动化测试工具,但是如果你不会做二次封装,只会直接使用那这个库并不能给你的测试效率带来方便,相反维护起来是一个累赘,因为你会遇到各种各样的问题,比如页面等待超时,元素定位困难、不准确,脚本编写困难等待。

针对这种情况我制作了一个非常易用的库:selenium-smart。开源地址:https://github.com/jiawade/selenium-smart 还请大家多多点星支持一下,感谢

具体用法:

1 maven项目中添加依赖库:

<dependency>
  <groupId>io.github.jiawade</groupId>
  <artifactId>selenium-smart</artifactId>
  <version>4.8.2</version>
</dependency>

2 易用的元素定位

selenium提供了8中元素定位方式(id, name, tagName, className, cssSelector, lindText, partialLinkText, xpath) 

个人看法,从实际项目中得出的结论,虽然提供了这么多,个人觉得tagName, className, cssSelector, lindText, partialLinkText很是鸡肋,食之无味弃之可惜。推荐用xpath,因为xpath可以代替所有,太多方法各种用法不利于记忆。

比如:

By.id("brush")可以写成By.xpath("//*[@id=\"brush\"]")

By.name("brush")可以写成By.xpath("//*[@name=\"brush\"]")

By.tagName("div")可以写成By.xpath("//div")

By.className("brush")可以写成By.xpath("//*[@class=\"brush\"]")

By.cssSelector("#bursh")可以写成By.xpath(//*[@id=\"brush\"])

By.linkText("brush")可以写成By.xpath("//*[text()=\"brush\"]")

尤其是className, tagName, cssSelector是极不推荐的,cssSelector还不支持定位文本元素

 selenium-smart元素定位方式包括:按界面文本定位,按元素属性定位,按坐标定位,只需简单写法即可实现元素定位,实际也是xpath的高级封装,如下给出几种定位方式的例子:

1 按文本定位:

界面的文本,就可以写成:text->New activity in

2 界面属性定位:

可以写成:@fill->currentColor

 

3 按界面坐标定位(注意界面,左上角为坐标的x=0, y=0点,向下为y轴,向右为x轴)

browser.click(1800, 30),前提是要知道该元素的x点像素以及y点的像素(chrome浏览器可以通过安装一个插件,名称叫做coordinates,只需要把鼠标悬浮到界面的任意位置,即可知道对应的坐标点)

 

3 代码例子

import io.smart.browser.configuration.impls.ChromeConfiguration;
import io.smart.browser.factory.BrowserFactory;
import io.smart.browser.factory.impls.SelenideBrowser;
import io.smart.element.impls.ElementByXpath;
import io.smart.enums.BrowserType;
import io.smart.utils.tools.Helper;
import io.smart.utils.xpath.Xpath;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.v85.network.model.RequestWillBeSent;

import java.io.File;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class Examples {

    public static void main(String[] args) {
        BrowserFactory factory = new SelenideBrowser().setUp(BrowserType.CHROME, buildChromeConf());
        ElementByXpath browser = new ElementByXpath(factory.getDriver());
        browser.get("https://www.selenium.dev/");
        browser.click("text->Documentation");
        browser.clickText("Blog");
        browser.click(Xpath.attribute("data-toggle", "dropdown").build(), "1");
        browser.click(1800, 30);
        browser.input("@class->DocSearch-Input", "selenium-smart");
        Helper.sleep(3000);
        browser.getLogs().forEach(System.out::println);
        factory.getDevTools().register("Network.requestWillBeSent", RequestWillBeSent.class).getData().forEach((k, v)-> System.out.println(k+": "+v));
        browser.closeBrowser();
    }


    private static ChromeConfiguration buildChromeConf() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--no-sandbox", "--disable-extensions", "--disable-dev-shm-usage");
        Map<String, Object> eOptions = new HashMap<>();
        Map<String, Object> infoBar = new HashMap<>();
        //浏览器顶部不显示浏览器被控制等信息
        infoBar.put("profile.password_manager_enabled", false);
        infoBar.put("credentials_enable_service", false);
        eOptions.put("excludeSwitches", new String[]{"enable-automation"});
        eOptions.put("prefs", infoBar);
        return ChromeConfiguration.builder()
                .chromeOptions(options)
                //设置浏览器宽度、高度
                .width(1920)
                .height(1080)
                //设置等待超时时间,单位为秒
                .duration(Duration.ofSeconds(60))
                .experimentalOption(eOptions)
                //设置下载地址
                .downloadDirectory(System.getProperty("user.dir") + File.separator + "target" + File.separator + "downloads")
                //是否最大化窗口
                .maximizeWindow(true)
                //是否禁用GPU加速
                .disableGpu(true)
                //是否禁用插件,禁用后提升测试运行速度
                .disableExtensions(true)
                .disableDevShmUsage(true)
                //启用浏览器log,启用后能查看restful、websocket请求,服务端返回的消息以及状态
                .enablePerformanceLog(true)
                .pageLoadStrategy(PageLoadStrategy.NONE)
                .build();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值