用junit4+selenium+Java的时候,发现Assert.assertEquals的问题,请高人指点,谢谢!
用junit4+selenium+Java的时候,发现Assert.assertEquals函数一旦fail,case就中断执行了,怎么让他继续执行呢?就算其中一个点fail了,比如 Assert.assertEquals("not equal",2, lwo_username.size());,我还是想让他继续执行。代码如下:
TestScript.Java
package Test;
import java.io.FileInputStream;
import java.util.List;
import org.junit.Assert;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class TestScript {
WebDriver driver;
public Object GetData(String excelInput, String SheetName,int rownum,int cellnum){
Object result = null;
System.out.println(excelInput);
try{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelInput));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
Sheet sheet=workbook.getSheet(SheetName);
Row row=sheet.getRow(rownum);
Cell cell=row.getCell(cellnum);
//cell.getStringCellValue();
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
CellValue cellValue = evaluator.evaluate(cell);
//System.out.println(cellValue.getCellType());
if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
result = cellValue.getBooleanValue();
} else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
result = cellValue.getNumberValue();
} else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
result = cellValue.getStringValue();
}
}
catch(Exception e){
e.printStackTrace();
}
return result;
}
public void OpenIE(String URL) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "G:\\Selenium\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get(URL);
Thread.sleep(2000);
}
public void login(Object username2, Object password2){
WebElement username;
WebElement password;
WebElement Enterbutton;
WebElement Text;
List<WebElement> lwo_username;
lwo_username=driver.findElements(By.xpath("//Input[@name='j_username']"));
Assert.assertEquals("not equal",2, lwo_username.size());
username=driver.findElement(By.xpath("//Input[@name='j_username']"));
username.sendKeys(username2.toString());
password=driver.findElement(By.xpath("//Input[@name='j_password']"));
password.sendKeys(password2.toString());
Enterbutton=driver.findElement(By.xpath("//Input[@value='Enter']"));
Enterbutton.click();
Text=driver.findElement(By.xpath("//Span"));
Assert.assertNotNull("Pass", Text);
}
}
TestScriptTest.java
package Test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestScriptTest{
public static TestScript a= new TestScript();
String URL="http://172.20.88.99";
Object Username=a.GetData("G:/Selenium/Test1.xls", "Login", 1, 1);
Object Password=a.GetData("G:/Selenium/Test1.xls", "Login", 1, 2);
@Before
public void setUp() throws Exception {
try {
a.OpenIE(URL);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
}
@Test
public void testLogin() {
a.login(Username, Password);
}
} 断言是这样的,出错就中断执行,你这样的写检查点不是个好的策略 joykao 发表于 2015-7-6 13:33
断言是这样的,出错就中断执行,你这样的写检查点不是个好的策略
谢谢,那如果要写几个检查点用什么方法比较好呢? donkey0702 发表于 2015-7-6 13:47
谢谢,那如果要写几个检查点用什么方法比较好呢?
我是这么想的,你可以把登录抽象成公用方法,然后呢要测试登录直接call,把检查点设在具体的测试用例中,如果你的检查点是随着动态变化的尽量不要用完全匹配,可以参考下http://www.51testing.com/html/02/241902-849343.html
页:
[1]