selenium+junit4 参数化测试

来源:互联网 发布:淘宝零食店铺知乎 编辑:程序博客网 时间:2024/06/04 19:22


基本使用方法

  @RunWith 注解

  当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不是JUnit默认的运行器。

1、指定运行器(runner)为Parameterized.class。

2、参数集用放入一个返回类型Collection的容器中,用@Parameters注解。

3、在提供数据的方法上加上一个@Parameters注解,这个方法必须是静态static的,并且返回一个集合Collection 且必须是public static类型。

4、需要声明相应类型的变量,在构造方法中取回指定参数。

5、具体用例使用@Test注解,且不需要传入形参。

6、最后编写测试类,它会根据参数的组数来运行测试多次


实例:

import java.util.Arrays;
import java.util.List;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/*@RunWith:注解
 * 1.使用RunWith注解:可以指定JUnit的单元测执行类,这里就指定的是SpringJUnit4ClassRunner.class;
 */
@RunWith(Parameterized.class)
public class TestClass {
    public WebDriver chrome;
    String url = "https://www.baidu.com/";
    
    @Before
    public void Indexfunc() throws Exception {
        System.setProperty("webdriver.chrome.driver","toosFiles/chromedriver.exe");
        chrome = new ChromeDriver();
        chrome.get(url);
    }
    @After
    public void Endfunc() throws Exception{
        chrome.quit();
    }
        
     /**
        * 这里的返回至少是二维数组
        * @return
        */
       @Parameters
       public static List<String[]> getParams()
       {
           return Arrays.asList(new String[][]{{"admin","123123"}, 
                           {"测试一","123456"}, 
                           {"测试二","987654"}});
       }
      
       private String loginName;    //用户名
       private String passWd;        //密码 
       
       public TestClass(String loginName,String passWd)
       {
           this.loginName=loginName;
           this.passWd=passWd;
       }
       
       @Test
        public void LoginModule() throws Exception {
           System.out.print(loginName);
           System.out.print(passWd);
       }
      
}  

0 0