對於Selenium的基本元素的操作,就自己的瞭解做了一個基本的介紹,這篇直接上程式碼,針對一個頁面如何操作寫了些基本的操作指令碼,希望對初學者有一定的幫助,也希望通過這些總結讓自己有一些清晰的認識和了解:
Demo檔案下載地址:http://pan.baidu.com/s/1sjECS5B
Demo 的樣圖:
Java Code:
import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; public class BasicUIExercise { WebDriver driver; public void openBrowser() { System.setProperty("webdriver.chrome.driver", "E:\\JavaCodeWorkSpace\\JProject0610\\ChromeDriver\\chromedriver.exe"); driver = new ChromeDriver(); } // 退出所有瀏覽器 public void quitBrowser() { driver.quit(); } // 退出當前的單個瀏覽器 public void closeBrowser() { driver.close(); } public void goToTarget(String URL) { driver.navigate().to(URL); driver.navigate().refresh(); // driver.get(URL); } public void backToTarget() { driver.navigate().back(); } public void waitTime() { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } public void maxWindow() { driver.manage().window().maximize(); } // 拖動頁面滾動條到最低部 public void executeJS(String script) { // String js = "var q=document.documentElement.scrollTop=10000"; JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(script); } public void testInput(String value) { WebElement element = driver.findElement(By .xpath(".//*[@id='input']/input")); // element.clear(); element.sendKeys(value); String inputValue = element.getAttribute("value"); System.out.println(inputValue); } public void testClickLink() throws InterruptedException { WebElement element = driver.findElement(By.className("baidu")); System.out.println("The Link is: " + element.getAttribute("href")); element.click(); System.out.println("Current Page Title is:" + driver.getTitle()); Thread.sleep(3000); } public void testSelect(int index) { WebElement element = driver.findElement(By.name("select")); Select select = new Select(element); select.selectByIndex(index); String selectText = select.getFirstSelectedOption().getText(); System.out.println("Curretn Select is: " + selectText); } public void testRadioBox(int index) { List<WebElement> elements = driver.findElements(By.name("identity")); WebElement element = elements.get(index); element.click(); boolean flag = element.isSelected(); getClass(); System.out.println("the radioBox is Select:" + flag); } public void testMultipleBox() { List<WebElement> elements = driver.findElements(By .xpath(".//*[@id='checkbox']/input")); for (WebElement element : elements) { element.click(); boolean flag = element.isSelected(); System.out.println("the CheckBox is select:" + flag); } } public void testBtn() { WebElement element = driver.findElement(By.className("button")); element.click(); String btnText = element.getAttribute("value"); System.out.println(btnText); } public void testAlert() throws InterruptedException { WebElement element = driver.findElement(By .xpath(".//*[@id='alert']/input")); element.click(); Thread.sleep(1000); Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); System.out.println("ALert Text is:" + alertText); alert.accept(); } // Selenium是無法識別Windows對話方塊的,所以在上傳檔案的過程中根據瀏覽器的不同,需要使用到AutoIT這個工具來獲取Windows對話方塊實現上傳的功能 public void testUploadFile(String filePath) { WebElement element = driver.findElement(By.id("load")); element.sendKeys(filePath); } public void testOpneNewWindow() { WebElement element = driver.findElement(By.xpath(".//*[@id='open']/a")); element.click(); String currentHandle = driver.getWindowHandle(); Set<String> handles = driver.getWindowHandles(); Iterator<String> iterators = handles.iterator(); while (iterators.hasNext()) { String handle = iterators.next(); if (!(currentHandle.equals(handle))) { driver.switchTo().window(handle); System.out.println("testOpneNewWindow Current Web Title: " + driver.getTitle()); this.closeBrowser(); } } driver.switchTo().window(currentHandle); } // 將滑鼠移動到該Btn下面,就會有對應的資訊顯示 public void testAction() { WebElement element = driver.findElement(By.className("over")); Actions actions = new Actions(driver); actions.moveToElement(element).build().perform(); String actionText = driver.findElement(By.id("over")).getText(); System.out.println("The Action Text is: " + actionText); } //testAction彈出的資訊得首先處理掉,不然testElementDisplayedWait無法定位到當前的頁面,聚焦點依然在testAction上面的資訊上面 public void testActionJavaScript() { JavascriptExecutor js2 = (JavascriptExecutor) driver; js2.executeScript("alert('Hello Selenium Web Test!')"); Alert alert = driver.switchTo().alert(); String text = alert.getText(); System.out.println(text); alert.accept(); } public void testElementDisplayedWait() { WebElement element = driver.findElement(By.className("wait")); element.click(); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver2) { return driver2.findElement(By.className("red")).isDisplayed(); } }); System.out.println("The context is:" + driver.findElement(By.className("red")).getText()); } public static void main(String[] args) throws InterruptedException { BasicUIExercise bue = new BasicUIExercise(); bue.openBrowser(); bue.maxWindow(); bue.waitTime(); bue.goToTarget("E:\\JavaCodeWorkSpace\\JProject0610\\WebAddress\\demo2.html"); bue.testInput("Test WebDriver Study"); bue.testClickLink(); bue.backToTarget(); bue.testSelect(4); bue.testRadioBox(5); bue.testMultipleBox(); bue.testBtn(); bue.testAlert(); bue.executeJS("var q=document.documentElement.scrollTop=10000"); bue.testUploadFile("E:\\JavaCodeWorkSpace\\JProject0610\\WebAddress\\demo2.html"); bue.testOpneNewWindow(); bue.testAction(); bue.testActionJavaScript(); bue.testElementDisplayedWait(); Thread.sleep(1000); bue.quitBrowser(); } }