通过定义系统关键特征与预期特征比较,判断功能测试是否通过。伴随测试Build不断更新,这些关键特征可以发生变化,需要修改测试脚本,使用Git保存不同版本的测试脚本。
登录用例验证:
登录系统的标题通常唯一,可以通过标题与预期标题比对,如果一致说明系统正常使用。
driver.get("http://localhost:6060/");
String title = driver.getTitle();
assertEquals(title,"古方红糖财务管理系统");
登录成功验证,系统登录后会将登录用户显示在页面上,可以根据页面是否存在登录用户判断是否登录成功。
//定位登录用户DIV
String result = driver.findElement(By.id("userdiv")).getText();
System.out.println("result==="+result);
assertEquals(result,"当前用户:admin");
修改预期用户名
//定位登录用户DIV
String result = driver.findElement(By.id("userdiv")).getText();
System.out.println("result==="+result);
assertEquals(result,"当前用户:admin2");
登录脚本
package com.gf;
//import org.junit.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class LoginTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void test(){
driver.get("http://localhost:6060/");
String title = driver.getTitle();
assertEquals(title,"古方红糖财务管理系统");
driver.manage().window().setSize(new Dimension(1382, 744));
driver.findElement(By.id("loginId")).click();
driver.findElement(By.id("loginId")).sendKeys("admin");
driver.findElement(By.id("pwd")).click();
driver.findElement(By.id("pwd")).sendKeys("1");
driver.findElement(By.id("loginbtn")).click();
String result = driver.findElement(By.id("userdiv")).getText();
System.out.println("result==="+result);
assertEquals(result,"当前用户:admin");
try {
Thread.sleep(3000); //让线程等待3秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.cssSelector(".easyui-linkbutton .l-btn-text")).click();
}
}
使用Selenium-IDE录制添加组织结构测试用例,并导出为Java单元测试类
package com.gf;
//import org.junit.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class AddOrgTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void test(){
driver.get("http://localhost:6060/");
driver.manage().window().setSize(new Dimension(1382, 744));
driver.findElement(By.id("loginId")).click();
driver.findElement(By.id("loginId")).sendKeys("admin");
driver.findElement(By.id("pwd")).click();
driver.findElement(By.id("pwd")).sendKeys("1");
driver.findElement(By.id("loginbtn")).click();
driver.findElement(By.cssSelector(".accordion-expand")).click();
driver.findElement(By.cssSelector("#\\_easyui_tree_4 > .tree-title")).click();
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".tree-hit")).click();
driver.findElement(By.cssSelector(".tree-collapsed")).click();
driver.findElement(By.cssSelector(".tree-title:nth-child(4)")).click();
driver.findElement(By.id("_easyui_textbox_input1")).clear();
driver.findElement(By.id("_easyui_textbox_input1")).sendKeys("Test3");
driver.findElement(By.id("_easyui_textbox_input2")).clear();
driver.findElement(By.id("_easyui_textbox_input2")).sendKeys("1");
driver.findElement(By.xpath("//a[@id=\'btn\']/span/span")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.cssSelector(".tree-hit")).click();
driver.findElement(By.cssSelector(".tree-collapsed")).click();
}
}
添加部门,需要执行Ajax,最好根据Ajax的返回值判断是否添加成功,但现在还需要探索如何抓取Ajax的返回,现在还没有好的方法解决。
driver.findElement(By.xpath("//a[@id=\'btn\']/span/span")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}