This article uses Selenium's Java version of WebDriver and Chrome browser driver. The former is a Java class library that provides various APIs related to testing. Maven is used in the project to import its Jar package; the latter is a binary executable file that is used to control the browser and is specified in the code The file path.

To read and try to run the examples in this article, you need some Java foundation, such as setting up a basic development environment, using Maven to download Java dependency packages, JUnit unit testing framework, etc., which we can introduce to you in subsequent programming language courses.

  1. Download and install IntelliJ Idea Community Edition (Community) Java IDE from official website
  2. Execute the following command to obtain the source code of the demo project from the Gitee code repository;

    git clone https://gitee.com/ngtesting/ci_test_selenium_simple
  3. Check the version of the Chrome browser on the computer, here , and unzip it to a local directory;
  4. Open the project in Idea and find the SeleniumTest.java file in the src/test/java directory;
  5. Note that in the previously recorded code, System.setProperty is used to specify the path where the driver executable file is located;
    image.png
  6. In the project root directory, execute the mvn compile command to download dependency Jar packages such as Selenium and JUnit;
  7. Right-click the SeleniumTest.java test class file and select the Debug menu item to execute the JUnit unit test.

Here is a complete Java code file. For examples in other languages, please refer to Selenium official sample .

package com.deeptest.sample.selenium;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.*;

public class SeleniumTest {
    public static String driverPath = "/Users/aaron/driver/chromedriver";

    // 类级别的变量
    private WebDriver driver;
    private Map<String, Object> vars;
    JavascriptExecutor js;

    // 使用JUnit的@Before注解,定义在测试执行前,需执行的初始化行为。
    @Before
    public void setUp() {
        // 初始化WebDriver对象
        ChromeOptions options = new ChromeOptions();
        // 指定Chrome驱动的文件路径
        System.setProperty("webdriver.chrome.driver",  driverPath);
        driver = new ChromeDriver(options);

        // 初始化JS执行器对象,用于在浏览器中执行Javascript代码。
        js = (JavascriptExecutor) driver;
    }

    // 使用JUnit的@After注解,定义在测试完成后,需执行的清理动作。
    @After
    public void tearDown() {
        // 退出并清理WebDriver对象
        driver.quit();
    }

    // 使用JUnit的@Test注解,标注此方法为一个测试方法。
    @Test
    public void test1() {
        // 打开百度网站首页
        driver.get("https://www.baidu.com/");

        // 设置浏览器窗口大小为1440x875
        driver.manage().window().setSize(new Dimension(1440, 875));

        // 找到id为kw的(文本框)控件,在里面输入“禅道”文字。
        driver.findElement(By.id("kw")).sendKeys("禅道");

        // 找到id为su的(按钮)控件,并点击。
        driver.findElement(By.id("su")).click();

        // 找到指定内容的链接,并点击。
        driver.findElement(By.partialLinkText("开源项目管理软件")).click();
    }

}

thematic catalog


陈哥聊测试
158 声望3.3k 粉丝

资深敏捷测试顾问,国内知名项目管理软件禅道团队成员。