简体   繁体   中英

How to LowerCase a List created from an Excel File

I have been stuck on this for days. I don't understand the reason this is failing. I'm using Katalon and I'm trying to use Groovy as main language but for some functions I use Java.

What I'm trying to do is, first I read an Excel file getting values from a column, then I store it in a List of Strings and finally I want to lowercase that list.

  • Getting the list from excel works well.
  • Changing list to lowercase works well.
  • But for some reason, toLowerCase() refuses to work on the list I got from excel file. On any other list it works fine.

This is the main Script (test case)

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import static java.util.stream.Collectors.*
import java.lang.String as String

List<String> lst_ListFromExcel = new ArrayList()

//this works fine
lst_ListFromExcel = CustomKeywords.'com.utils.ExcelFiles.getCellValuesList'(xlFilePath, 0, 3)

//list printed correctly
WebUI.comment("lst_ListFromExcel = $lst_ListFromExcel")

//Copying list I got from excel to a new one. The first time it worked
List<String> resultsList = new ArrayList<String>(lst_ListFromExcel)

WebUI.comment("resultsList = $resultsList")

//Here is where I got the error. However, with any other list it works fine.
resultsList = resultsList.collect{it.toLowerCase()}

WebUI.comment("resultsList = $resultsList")

This is the Custom Keyword I made to get the values of a column and store them in a list of strings:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.xssf.usermodel.XSSFWorkbook

    // xlFilePath in this format C:\folder\folder\file.xlsx
    @Keyword
    def getCellValuesList(String xlFilePath, int sheetIndex, int columnIndex) {
        List<String> rsltValuesList = new ArrayList<>()
        def count = 0
        int column_index_1 = 0;
        File file = new File(xlFilePath);
        Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        column_index_1 = columnIndex
        Row row = sheet.getRow(0);
        for (Row r: sheet) {
            if (r.getRowNum() == 0) continue; //hearders
            Cell c_1 = r.getCell(column_index_1);
            if (c_1 != null && c_1.getCellType() != Cell.CELL_TYPE_BLANK) {
                System.out.print("  " + c_1 + "\n");
                rsltValuesList.add(c_1)
                count++
            }
        }
        WebUI.comment("Values count = $count")
        WebUI.comment("rsltValuesList = $rsltValuesList")
        return rsltValuesList
    }

Finally, the error I'm getting is this one. I'm only getting this error when using the list I got from the excel file. With any other list this works fine.

10-09-2022 08:33:27 PM resultsList = resultsList.collect({ -> ... })

Elapsed time: 0.058s

Test Cases/Temp/Temp-ToLowerCase FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: org.apache.poi.xssf.usermodel.XSSFCell.toLowerCase() is applicable for argument types: () values: []
    at Script1665186880146$_run_closure1.doCall(Script1665186880146.groovy:32)

I'm not using org.apache.poi.xssf.usermodel.XSSFCell and as I said, this only happens with the list i got from Excel File, ie lst_ListFromExcel or resultsList

I would appreciate any help

Adding toString() should help

resultsList.collect{it.toString().toLowerCase()}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM