1、 先安装火狐浏览器Firefox
2、 在火狐浏览器Firefox输入栏,
输入https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,按回车键,显示如下图:
3、 点击按钮【Add to Firefox】,弹出下图
4、 点击按钮【安装】,安装结束后,重启浏览器,就可以在开发者菜单看到Selenium IDE选项,如下图:
5、 打开Selenium IDE,界面如下图:
6、打开火狐浏览器,输入测试访问地址http://192.168.199.110/chadmin/backend/web/index.php,登录操作
7、点击文件,选择Export Test Case As,选择Java/TestNG/WebDriver,导出脚本。
8、 导出脚本如下:
package com.example.tests;
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;
}
}
}