数据驱动-Excel

我们在页面的测试中,会发现很多输入框的测试,这些输入框中我们需要各种不同类型的数据来测试程序是否能正确处理,因此会涉及到一个“参数化”的过程.简单介绍Selenium中如何利用Excel实现“参数化“

导入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();
        }  
}

猜你喜欢

转载自865325772.iteye.com/blog/2051322