简体   繁体   English

使用TestNG DataProvider时,是否可以从SAME Excel工作表中读取和写入参数?

[英]When using TestNG DataProvider is there a way to read and write parameters from the SAME Excel sheet?

I'm still relatively new to both Java and TestNG, so forgive me if I use incorrect technical terms. 我对Java和TestNG还是比较陌生,所以如果我使用了错误的技术术语,请原谅我。 I'll keep an eye out for responses throughout the day in case I need to correct myself or clarify. 如果我需要纠正自己或澄清自己的话,我会全天关注答复。

In the automated test that I am writing, I am passing my parameters through an Excel sheet via Data Provider. 在我正在编写的自动化测试中,我正在通过数据提供程序通过Excel工作表传递参数。 That whole part is fine. 整个部分都很好。 However, I would now like to perform a getText() on an element and copy that String BACK to the excel sheet that Data Provider was reading from. 但是,我现在想对元素执行getText()并将该String Back复制到Data Provider正在读取的excel工作表中。

Here is an example of my intentions: 这是我意图的一个例子:

//Grabs Parameters to be used from Excel Sheet
@DataProvider
public Object[][] getData(){
suiteXls = new Xls_Reader(System.getProperty("user.dir")+"//src//testData.xlsx")
List<String> sheetNames = new ArrayList<String>();
sheetNames.add("TestStrings");
return TestUtilFinal.getData(suiteXls,sheetNames);
}

//Test Begins
@Test(dataProvider="getData")
public void Test1(String Value_1, String Operator, String Value_2, String Output){
// do some work here
Output.saveToCell();

An Issue that I potentially see is that the cell that the value would be saved in would be dynamic. 我可能看到的一个问题是,将值保存在其中的单元格是动态的。

Here is what the excel sheet would look like before the test is run 这是运行测试之前excel工作表的外观

Here is what the same sheet would like AFTER the test is run. 这是运行测试后同一张纸想要的。 Any help or insight would be greatly appreciated! 任何帮助或见识将不胜感激! Thanks 谢谢

  1. I recommend treating your input as immutable. 我建议将您的输入视为不可变的。 I find that when you modify your input to a process that when you repeat that process (as is often done in testing) that it then quickly becomes unclear as to where the current results came from. 我发现,当您修改流程的输入时,当您重复该流程时(如在测试中经常进行的那样),那么很快就不清楚当前结果来自何处。 It is generally much better to create a unique output file that contains some/all of the input data along with the output data. 通常,最好创建一个包含一些/所有输入数据以及输出数据的唯一输出文件。 I often name such output files with a build revision and/or a time-stamp so that each one is unique and referable. 我经常用构建修订版和/或时间戳命名此类输出文件,以便每个文件都是唯一且可引用的。
  2. If you really want to modify your input then you'll need to have a reference to the Excel "Row" object for the test. 如果您确实要修改输入,则需要引用Excel“ Row”对象进行测试。 eg You could add such to your data provided to the test. 例如,您可以将其添加到提供给测试的数据中。
  3. It isn't clear from your getData method but I suspect that you are using Apache POI (either directly or indirectly) to read data from the Excel Worksheet. 从您的getData方法尚不清楚,但我怀疑您正在使用Apache POI(直接或间接)从Excel工作表中读取数据。 There are plenty of examples on StackOverflow and elsewhere for reading and writing rows of data using Apache POI. 关于StackOverflow和其他地方,有很多使用Apache POI读取和写入数据行的示例。 You simply need to maintain a reference to your test case's Row object and then know which column (cellnum) to write your output to. 您只需要维护对测试用例的Row对象的引用,然后知道将输出写入哪一列(单元格)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以将参数传递给TestNG测试,而无需在XML中使用@DataProvider和参数? - Is there a way to pass parameters to TestNG test without using @DataProvider & parameters in XML? 如何使用Apache POI和TestNG dataProvider编写Excel - How to write excel using Apache POI with TestNG dataProvider 如何在testNG中使用@dataprovider从excel读取不同的数据类型 - How can I read different datatypes from excel using @dataprovider in testNG 从 excel 表读取数据并写入同一 excel 表 - Read data from excel sheet and write in the same excel sheet 使用 Apache POI 将多种数据类型数据从 excel 发送到 Testng Dataprovider 的最佳方法是什么 - What is the best way to send multiple data type data from excel to Testng Dataprovider using Apache POI Selenium TestNG从Excel工作表中传入参数 - Selenium TestNG Passing in parameters from Excel sheet 如何为不同行的不同列号的 Excel 工作表编写 TestNG DataProvider 注释 - How to write TestNG DataProvider annotation for Excel sheet with different column numbers for different rows TestNG中的DataProvider使用Java WebDriver从Excel传递数据 - DataProvider in TestNG to pass data from Excel using Java WebDriver 使用 DataProvider 将 lambdas 作为 testng 参数传递? - Passing lambdas as testng parameters using DataProvider? 如何使用TestNG DataProvider传递&gt; 10个参数? - how to pass > 10 parameters using TestNG DataProvider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM