如何更换从数据提供商的硬编码值(how to replace the hardcoded value

2019-10-30 02:05发布

我在寻找去除(更换)从@dataProvider公共静态对象的硬编码的浏览器/操作系统/版本值[] [] 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中/詹金斯/ Saucelabs

Answer 1:

这样做的最简单的方法是将组合(浏览器/操作系统/版)移动到像Excel电子表格或CSV或一个JSON / YAML / XML数据源,让你的@DataProvider从那里阅读。

更新

  • 如何阅读JSON文件: http://tutorials.jenkov.com/java-json/gson-jsonparser.html
  • 如何读取XML文件: https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/


Answer 2:

您可以利用QAF TestNG的扩展 。 它提供不同的内置数据的提供从像XML,JSON,EXCEL,属性,数据库不同的数据源加载测试数据。 随着QAF可以如下设置不同的数据源:

@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")

此外,您可以切换数据提供商无需更改代码。 此外,它有很多的功能,为网络的黑盒测试,移动(使用硒,appium)和Web服务。



文章来源: how to replace the hardcoded values from data provider