Java 中的簡單 Selenium 測試
下面的程式碼是使用 selenium 的簡單 java 程式。以下程式碼的旅程是
- 開啟 Firefox 瀏覽器
- 開啟谷歌頁面
- 列印 Google 頁面的標題
- 找到搜尋框位置
- 將值作為 Selenium 傳遞到搜尋框中
- 提交表格
- 關閉瀏覽器
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time
// when trying to find an element or elements if they are not immediately available.
// The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Maximize the browser window to fit into screen
driver.manage().window().maximize();
// Visit Google
driver.get("http://www.google.com");
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Selenium!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
//Close the browser
driver.quit();
}
}