一、 先安装火狐浏览器Firefoxphp
二、 在火狐浏览器Firefox输入栏,java
输入https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,按回车键,显示以下图:web
三、 点击按钮【Add to Firefox】,弹出下图浏览器
四、 点击按钮【安装】,安装结束后,重启浏览器,就能够在开发者菜单看到Selenium IDE选项,以下图:ide
五、 打开Selenium IDE,界面以下图: 测试
六、打开火狐浏览器,输入测试访问地址http://192.168.199.110/chadmin/backend/web/index.php,登陆操做ui
七、点击文件,选择Export Test Case As,选择Java/TestNG/WebDriver,导出脚本。.net
八、 导出脚本以下:firefox
package com.example.tests;ci
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Login {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://192.168.199.110/chadmin/backend/web/index.php";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public void testLogin() throws Exception {
driver.get(baseUrl + "/chadmin/backend/web/index.php");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("login_btn")).click();
}
@AfterClass(alwaysRun = true)
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;
}
}
}