导入Feed4testng相关的包, 继承FeedTest这个类,然后把数据写入到Excel,直接读Excel,脚本和数据分离,而且也不需要用JAVA写读取Excel的代码,相当方便
用读取Excel的方式 :
import org.testng.annotations.Test; import org.testng.annotations.BeforeSuite; import static org.testng.AssertJUnit.assertEquals; import java.io.File; import java.util.Vector; import org.databene.benerator.anno.Source; import org.databene.feed4testng.FeedTest; import org.junit.AfterClass; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeSuite; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.opera.core.systems.scope.protos.ExecProtos.ActionList.Action; public class testSchoolRoom extends FeedTest { public WebDriver driver; private String LinkTest; public String baseUrl = "http://wuhan.eduyun.cn"; public void startUrl() throws Exception { driver = new FirefoxDriver(); driver.get(baseUrl); // driver.manage().window().maximize(); } @Test(dataProvider = "feeder") @Source("test.xls") public void testTcShortPasswordLg(String userName, String passWord,boolean flag, String excepted) throws Exception { startUrl(); driver.findElement(By.id("info_username")).clear(); driver.findElement(By.id("info_username")).sendKeys(userName); driver.findElement(By.id("info_password")).clear(); driver.findElement(By.id("info_password")).sendKeys(passWord); driver.findElement(By.id("info_submit")).click(); if(flag){ LinkTest = driver.findElement(By.xpath("//li[contains(concat(' ', @class, ' '), ' jykj_blue ')]//strong")).getText().trim(); assertEquals(excepted, LinkTest); LinkTest = null; Thread.sleep(2000); }else{ LinkTest = driver.findElement(By.xpath("//p[contains(concat(' ', @class, ' '), ' warnmsg ')] ")) .getText().trim(); assertEquals(excepted, LinkTest); LinkTest = null; Thread.sleep(2000); } driver.quit(); } }