使用maven配置web自动化测试依赖

前提:maven作为一个很受欢迎的脚手架工具,可以帮助我们快速配置在编写测试代码时所需的依赖,省去了一大堆配置工作。本篇文章的内容已默认下载并配置了maven工具,如需安装,可阅官方文档:https://maven.apache.org/install.html

 一、pom.xml文件添加selenium依赖,依赖版本根据需要可自行修改:

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>

二、重新加载maven项目,目的是import时可以找到maven本地仓库的selenium依赖

 

三、编写测试代码,并运行成功

        // 设置chromepath的路径
        System.setProperty("webdriver.chrome.driver", BasicSelenium.getProjectAbsolutePath() + CHROME_DRIVER_PATH);
        // 设置chrome浏览器启动程序的路径
        //System.setProperty("webdriver.chrome.bin", "C:\Program Files\Google\Chrome\Application\\chrome.exe");
        //启动浏览器时报:java-selenium 启动时出现 Invalid Status code=403 text=Forbidden
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        Thread.sleep(5000);
        driver.close();

 

高能:maven工具添加第三方依赖的方法很简单,在代码忠import时也会根据groupId、artifactId、version在本地maven仓库找到对应的依赖并导入!!!

posted @ 2024-05-25 17:35  vevian  阅读(116)  评论(0)    收藏  举报