Selenium+Java配置Web自动化测试环境

  1. 环境搭建
    Mac+IDEA+Java8+Selenium
    官网:https://www.selenium.dev/
    Selenium中文教程网站: http://www.selenium.org.cn/

    IDEA搭建以Springboot框架的项目

    官方配置参考文档:https://www.selenium.dev/documentation/en/selenium_installation/installing_selenium_libraries/

  2. pom加入selenium-java依赖

     <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <optional>true</optional>
    </dependency>
    
  3. Selenium-java自动运行支持所有selenium支持的浏览器;如果你只想测试特殊的浏览器比如Chrome,pom引入Chrome依赖

    	<dependency>
        	<groupId>org.seleniumhq.selenium</groupId>
        	<artifactId>selenium-chrome-driver</artifactId>
    	</dependency>
    

    其他浏览器同理;比如火狐

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-firefox-driver</artifactId>
      <version>3.X</version>
    </dependency>
    

    下图引用的浏览器驱动可以看到支持的浏览器chrome、edge、firefox…
    在这里插入图片描述

  4. pom添加selenium-server依赖

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-server</artifactId>
    </dependency>
    
  5. 下载WebDriver
    选择浏览器WebDriver
    https://www.selenium.dev/documentation/en/webdriver/driver_requirements/#quick-reference

    我选择的是chrome,对应下载地址
    https://chromedriver.storage.googleapis.com/index.html
    我的电脑是mac64;选择chromed浏览器相应的版本;我的浏览器版本是80.0.3987.163
    https://chromedriver.storage.googleapis.com/index.html?path=80.0.3987.106/

    叫下载的chromedriver加入到环境变量;mac在环境变量添加如下

    export PATH="$PATH:/path/to/chromedriver"
    说明:/path/to/chromedriver换成自己的地址

    运行如下图显示成功在这里插入图片描述

  6. 测试

    public class Test {
    
        public static void main(String[] args) {
            // chromedriver服务,/path/to/chromedriver自己chromedriver地址
            System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
            // 新建ChromeDriver的驱动
            WebDriver driver = new ChromeDriver();
            String url = "http://www.baidu.com";
            // 打开指定的网站
            driver.get(url); 
            
    		// 登录功能
    		// 输入账号accout
        	driver.findElement(By.id("account")).sendKeys("accout");
       		// 输入密码111111
        	driver.findElement(By.id("password")).sendKeys("111111");
        	// 通过元素的类名定位登录
        	WebElement login = driver.findElement(By.className("antd-pro-components-login-index-submit"));
        	// 点击登录
        	login.click();
        }
    }
    

    运行就可以自动在在浏览器打开
    在这里插入图片描述
    更多的文档请参照:https://www.selenium.dev/documentation/en/webdriver/driver_requirements/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章