如何替换数据提供者的硬编码值

时间:2018-07-02 20:31:31

标签: java selenium testng dataprovider

我正在寻找从以下位置删除(替换)硬编码的浏览器/ OS /版本值的建议: @DataProvider公共静态Object [] [] sauceBrowserDataProvider。代码示例:

    @DataProvider(name = "hardCodedBrowsers", parallel = true)
    public static Object[][] sauceBrowserDataProvider(Method testMethod) {
    return new Object[][]{

            **new Object[]{"firefox", "55.0", "Windows 10"},
            new Object[]{"chrome", "65.0", "Windows 10"},**

    };}


@Test( dataProvider = "hardCodedBrowsers")

    public void createDriver(String browser, String version, String os)
        throws Exception {
        Class<? extends TestRemote> SLclass = this.getClass();

        DesiredCapabilities capabilities = new DesiredCapabilities();

        // set desired capabilities to launch appropriate browser on Sauce
        capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
        capabilities.setCapability(CapabilityType.VERSION, version);
        capabilities.setCapability(CapabilityType.PLATFORM_NAME, os);    

        capabilities.setCapability("seleniumVersion", "3.8.1");
        capabilities.setCapability("name", SLclass.getSimpleName());
        capabilities.setCapability("screenResolution", "1920x1080");

        driver = (new RemoteWebDriver(new URL("http://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"), capabilities));

       configRead = new ConfigFileReader();

       propertyRead = new PropertyLoader();

       baseUrl = propertyRead.getProperty("site.url");

       app = new Application(driver);

       getURL();

    }

这是Java / TestNg / Jenkins / Saucelabs上的硒项目

2 个答案:

答案 0 :(得分:1)

最简单的方法是将组合(浏览器/ OS /版本)移动到excel电子表格或csv或json / yaml / xml之类的数据源,并让您的@DataProvider阅读它们从那里。

更新

答案 1 :(得分:0)

You can utilize QAF TestNG extension. It providers different inbuilt data-providers to load test data from different data source like xml, json, excel, properties, database. With QAF you can set different data source as below:

@QAFDataProvider (dataFile = "resources/data/logintestdata.csv")
@QAFDataProvider(dataFile = "resources/data/testdata.xls")
@QAFDataProvider(dataFile = "resources/data/testdata.xls", sheetName="login")
@QAFDataProvider(dataFile = "resources/data/testdata.xls", key="login") //excel with data table
@QAFDataProvider(dataFile = "resources/data/logintestdata.json")
@QAFDataProvider(key="login.data") //xml key
@QAFDataProvider(sqlQuery = "select col1, col2 from tbl") // database
@QAFDataProvider(dataFile = "resources/data/logintestdata.txt")

Moreover you can switch data-provider without change in code. In addition to that it has lots of features for black box testing of web, mobile (using selenium, appium) and web-services.