一、查看Firefox的版本,安裝對應的SeleniumIDE
從中看到firefox的版本號,它的版本號就是 42.0
安裝SeleniumIDE:
二、使用SeleniumIDE錄制腳本
1、安裝seleniumIDE后,在瀏覽器右上角就會有相應的標志按鈕
2、點擊此按鈕就會出現selenium IDE操作界面
3、打開一個頁面之后,打開selenium IDE使其保持錄制狀態,就可以操作了,完成操作之后,對其停止,腳本錄制完成。
三、使用SeleniumIDE導出腳本
1、文件→ETCA→JAVA
2、把文件保存在桌面,命名為baidu_test
3、桌面上的文件為:
4、用記事本打開,代碼如下
package com.example.tests;
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 BaiduTest {
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 testBaidu() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("天氣");
driver.findElement(By.id("su")).click();
driver.findElement(By.xpath("//div[@id='1']/h3/a/em[3]")).click();
}
@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;
}
}
}