1. 程式人生 > >java+selenium webUI自動化測試入門demo

java+selenium webUI自動化測試入門demo

pom.xml


<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.9.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>

    <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

java

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

import static org.hamcrest.core.StringContains.containsString;

public class SeleniumFirst {

    public static void main(String[] args) {
        //chromedriver服務地址,需要手動下載
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //目標URL
        driver.get("https://www.baidu.com");

        //獲取標題
        String title = driver.getTitle();
        System.out.printf(title);

        //獲取輸入框
        WebElement element = driver.findElement(By.xpath("//input[@id='kw']"));
        System.out.println("1=="+element.getTagName());
        //清空輸入框
        element.clear();
        
//      WebElement element1 = driver.findElement(By.id("kw"));
//      System.out.println("2=="+element1.getTagName());
//      element1.clear();

        //隱式等待2秒,TimeUnit.SECONDS表示單位秒
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        //在百度的輸入框中輸入lao4j
        element.sendKeys("lao4j");

        //點選確定按鈕
        driver.findElement(By.xpath("//input[@id='su']")).click();
        //隱式等待2秒,TimeUnit.SECONDS表示單位秒
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        //獲取搜尋出來的第一條資訊的文字值
        String text = driver.findElement(By.xpath("//div[@id='content_left']/div[@id='1']/h3[1]/a")).getText();

        //判斷獲取的資訊是否包含lao4j的部落格 - CSDN部落格
        Assert.assertThat( text, containsString( "lao4j的部落格 - CSDN部落格" ));

        //關閉瀏覽器
        driver.close();

        // 瀏覽器最大化
        //driver.manage().window().maximize();

        // 設定瀏覽器的高度為800畫素,寬度為480畫素
        //driver.manage().window().setSize(new Dimension(800, 600))

        // 隱式等待
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // 瀏覽器後退
        //driver.navigate().back();

        // 瀏覽器前進
        //driver.navigate().forward()

    }

}