2
回答
testng同时使用@Parameter和DataProvider
华为云实践训练营,热门技术免费实践!>>>   

selenium跑case的时候使用testng驱动,想在testng.xml中配置多个test,对应不同的浏览器,然后在具体的test中使用DataProvider来获取测试数据,但是在运行过程中报参数没有在testng.xml中定义,这个搞不定了。难道是testng不支持这样使用么。报错信息如下:

Parameter 'browserType' is required by @Configuration on method before
but has not been defined in C:\cygwin64\home\victor\workspace\selenium-spoon-sampler\src\test\resources\testng.xml
	at org.testng.internal.Parameters.createParameters(Parameters.java:109)
	at org.testng.internal.Parameters.createParameters(Parameters.java:264)
	at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:69)
	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:135)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:427)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:607)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:874)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.runWorkers(TestRunner.java:689)
	at org.testng.TestRunner.privateRun(TestRunner.java:566)
	at org.testng.TestRunner.run(TestRunner.java:466)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:301)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:296)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:276)
	at org.testng.SuiteRunner.run(SuiteRunner.java:191)
	at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:808)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:776)
	at org.testng.TestNG.run(TestNG.java:701)
	at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:178)




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">

  <test name="Browser Compatibility Test Cases--firefox" thread-count="1" preserve-order="false">
       <parameter name="browserType" value="firefox" />
       <classes>
           <class name="com.yeetrack.selenium.test.ParameterTest" />
       </classes>
  </test>

    <test name="Browser Compatibility Test Cases--chrome" thread-count="1" preserve-order="false">
        <parameter name="browserType" value="chrome" />
        <classes>
            <class name="com.yeetrack.selenium.test.ParameterTest" />
        </classes>
    </test>
</suite>


package com.yeetrack.selenium.test;

import com.yeetrack.selenium.data.KeywordData;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

/**
 * Created by xuemeng.wang on 2014/8/4.
 */
public class ParameterTest {
    @Parameters("browserType")
    @BeforeMethod
    public void before(String browser)
    {
        System.out.println(browser);
    }

    @Test(dataProvider = "KeywordDataProvider", dataProviderClass = KeywordData.class)
    public void test(String keyword)
    {
        System.out.println(keyword);
    }
}



下面是dataprovider:

package com.yeetrack.selenium.data;

import org.testng.annotations.DataProvider;

/**
 * Created by xuemeng.wang on 2014/7/29.
 */
public class KeywordData
{
    @DataProvider(name="KeywordDataProvider")
    public static Object[][] keywordProvider()
    {
        return new Object[][] {{"apache"}, {"tomcat"}};
    }
}




举报
youthflies
发帖于3年前 2回/4K+阅
顶部