|
用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[contains (text(),'Your last successful sign in was on')]"));
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);
}
} |
|