how to replace the hardcoded values from data provider

时光总嘲笑我的痴心妄想 提交于 2019-12-11 15:48:11

问题


I'm looking for the recommendation of removing (replacing) the hard-coded browser/OS/Version values from @DataProvider public static Object[][] sauceBrowserDataProvider. The code example:

    @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();

    }

This is a selenium project on Java/ TestNg/ Jenkins/Saucelabs


回答1:


The easiest way of doing this would be to Move the combinations(browser/OS/Version) to a data source like an excel spreadsheet or a csv or a json/yaml/xml and have your @DataProvider read them from there.

Update

  • How to read a JSON file : http://tutorials.jenkov.com/java-json/gson-jsonparser.html
  • How to read an XML file : https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/



回答2:


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.



来源:https://stackoverflow.com/questions/51143354/how-to-replace-the-hardcoded-values-from-data-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!