selenium+testN数据驱动

来源:互联网 发布:知乎关键指标kpi 编辑:程序博客网 时间:2024/06/11 22:54
我用testng自带的数据驱动功能做参数化,发现这种方法也很方便,但是数据要写在代码中,不免有点不好维护,于是我继承FeedTest这个类,然后把数据写入到Excel,直接读Excel,发现脚本和数据分离,而且
也不需要用JAVA写读取Excel的代码,相当方便,两个代码如下:
用Testng自带的做参数化:

public class testSchoolRoom {
        public WebDriver driver;
        public String baseUrl = "http://wuhan.eduyun.cn";
        public String LinkTest;
        public String LinkTestPassWord;

        public void startUrl() throws Exception {
                driver = new FirefoxDriver();
                driver.get(baseUrl);
                // driver.manage().window().maximize();
        }
       
        @DataProvider(name = "testData")
        public Object[][] testNetHomeWrok_data() {
                return new Object[][] { { "300522624", "1234", "f","密码长度在6-20之间" },
                                { "300522624", "123456789","f","密码输入有误" },
                                { "300522624", "1234qwer", "t","王丽娟的工作空间" }};
        }


        @Test(dataProvider = "testData")
        public void testTcShortPasswordLg(String userName, String passWord,String 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 == "t"){
                          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();
        }  
}

用读取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();
        }  
}
不过,下面一种 方式需要导入Feed4testng相关的包
0 0