SeleniumIDE录制脚本和导出脚本

一、查看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;
}
}
}

 

posted @ 2017-03-26 10:33  changLF  阅读(20887)  评论(1编辑  收藏  举报