1.新建maven工程

打开IDEA新建maven项目并引入相关依赖,步骤如下:

需要引入的依赖

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jettison</groupId>
      <artifactId>jettison</artifactId>
      <version>1.3.7</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.14.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
    <dependency>
      <groupId>io.appium</groupId>
      <artifactId>java-client</artifactId>
      <version>7.0.0</version>
    </dependency>
  </dependencies>

2.编写代码

代码如下:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestBaidu {
    private WebDriver driver;

    @Before
    public void setUp() throws Exception {
        //System.setProperty("webdriver.chrome.driver", "/Users/ceshi/Driver/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void openEditor() throws InterruptedException {
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
        driver.findElement(By.id("kw")).sendKeys("菜鸟教程");
        Thread.sleep(2000);
        driver.findElement(By.id("su")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.close();
    }
}