首先要有:
1、eclipse+jdk
2、selenium-IDE(http://release.seleniumhq.org/)
3、Firefox
4、selenium-java-2.53.1.jar(好像不是必須的)
5、selenium-server-standalone-2.53.1.jar
selenium-IDE使用方法、selenium代碼轉換java及eclipse建立我另一篇部落格有提及:http://blog.csdn.net/songjiaping/article/details/49507177
selenium下載地址:
https://github.com/SeleniumHQ/selenium/releases
http://selenium-release.storage.googleapis.com/index.html
Java/JUnit 4/Remote Control
之前在selenium-IDE轉換的代碼格式為:Java/JUnit4/Remote Control
該格式在eclipse中運行需在build path中至少添加selenium-server-standalone-2.53.1.jar
且在運行test時需要運行selenium-server-standalone-2.53.1.jar才能進行測試
java -jar selenium-server-standalone-2.53.1.jar
Java/JUnit 4/WebDriver
使用以上格式的java代碼在eclipse中運行需要在build path中添加selenium-server-standalone-2.53.1.jar
但是在運行test時不需要運行jar包,直接點擊Run就可以了
想要查看selenium的各種方法,可以添加對應版本的source code:
下載地址:https://github.com/SeleniumHQ/selenium/releases/tag/2.53.1
具體方法可以參考:http://blog.csdn.net/songjiaping/article/details/49585639
1、使用selenium-IDE編輯指令碼
以下為開啟baidu首頁,輸入“selenium”,點擊進行查詢,最後驗證一下有沒有期望顯示的文字“Selenium - Web Browser Automation”
2、轉換指令碼格式-Java/JUnit 4/WebDriver
3、複製產生的程式碼至eclipse建立的同名java檔案中
eclipse路徑如下:
DriverTest.java為剛建立的java檔案,selenium-server-standalone-2.53.1.jar必須添加到build path
以下為複製到DriverTest.java的代碼,需要修改package和class名
package selenium.test;import java.util.regex.Pattern;import java.util.concurrent.TimeUnit;import org.junit.*;import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.Select;public class DriverTest { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "https://www.baidu.com/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testUntitled2() throws Exception { driver.get("https://www.baidu.com"); // ERROR: Caught exception [ERROR: Unsupported command [setSpeed | 500 | ]] driver.findElement(By.id("kw")).clear(); driver.findElement(By.id("kw")).sendKeys("selenium"); driver.findElement(By.id("su")).click(); try { assertEquals("Selenium - Web Browser Automation", driver.findElement(By.xpath("//div[@id='1']/h3/a[1]")).getText()); } catch (Error e) { verificationErrors.append(e.toString()); } } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } }}
直接點擊Run執行