How to use CsvUtils class of org.testingisdocumenting.webtau.utils package

Best Webtau code snippet using org.testingisdocumenting.webtau.utils.CsvUtils

Source:DataCsv.java Github

copy

Full Screen

...14 * limitations under the License.15 */16package org.testingisdocumenting.webtau.data;17import org.testingisdocumenting.webtau.data.table.TableData;18import org.testingisdocumenting.webtau.utils.CsvUtils;19import java.nio.file.Path;20import java.nio.file.Paths;21import java.util.Collection;22import java.util.Collections;23import java.util.List;24import java.util.Map;25import java.util.function.Function;26import java.util.function.Supplier;27import static org.testingisdocumenting.webtau.data.DataContentUtils.*;28public class DataCsv {29 /**30 * Use <code>data.csv.table</code> to read data as {@link TableData} from CSV file.31 * <p>32 * Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.33 * @param fileOrResourcePath relative file path, absolute file path or classpath resource path34 * @return table data with CSV content35 */36 public TableData table(String fileOrResourcePath) {37 return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),38 (text) -> tableFromListOfMaps(CsvUtils.parse(text)));39 }40 /**41 * Use <code>data.csv.table</code> to read data as {@link TableData} from CSV file.42 * <p>43 * Passed path is either relative based on working dir or absolute file path.44 * @param filePath relative file path or absolute file path45 * @return table data with CSV content46 */47 public TableData table(Path filePath) {48 return parseCsvTextAsStep(DataPath.fromFilePath(filePath),49 (text) -> tableFromListOfMaps(CsvUtils.parse(text)));50 }51 /**52 * Use <code>data.csv.tableAutoConverted</code> to read data as {@link TableData} from CSV file. Numeric values become values of Numeric type instead of String type.53 * <p>54 * Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.55 * @param fileOrResourcePath relative file path, absolute file path or classpath resource path56 * @return table data with CSV content57 */58 public TableData tableAutoConverted(String fileOrResourcePath) {59 return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),60 (text) -> tableFromListOfMaps(CsvUtils.parseWithAutoConversion(text)));61 }62 /**63 * Use <code>data.csv.tableAutoConverted</code> to read data as {@link TableData} from CSV file. Numeric values become values of Numeric type instead of String type.64 * <p>65 * Passed path is either relative based on working dir or absolute file path.66 * @param filePath relative file path or absolute file path67 * @return table data with CSV content68 */69 public TableData tableAutoConverted(Path filePath) {70 return parseCsvTextAsStep(DataPath.fromFilePath(filePath),71 (text) -> tableFromListOfMaps(CsvUtils.parseWithAutoConversion(text)));72 }73 /**74 * Use <code>data.csv.listOfMaps</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.75 * <p>76 * Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.77 * @param fileOrResourcePath relative file path, absolute file path or classpath resource path78 * @return list of maps79 */80 public List<Map<String, String>> listOfMaps(String fileOrResourcePath) {81 return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath), CsvUtils::parse);82 }83 /**84 * Use <code>data.csv.listOfMaps</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.85 * <p>86 * Passed path is either relative based on working dir or absolute file path.87 * @param filePath relative file path or absolute file path88 * @return list of maps89 */90 public List<Map<String, String>> listOfMaps(Path filePath) {91 return parseCsvTextAsStep(DataPath.fromFilePath(filePath), CsvUtils::parse);92 }93 /**94 * Use <code>data.csv.listOfMapsAutoConverted</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.95 * Numeric values become values of Numeric type instead of String type.96 * <p>97 * Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.98 * @param fileOrResourcePath relative file path, absolute file path or classpath resource path99 * @return list of maps100 */101 public List<Map<String, Object>> listOfMapsAutoConverted(String fileOrResourcePath) {102 return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),103 CsvUtils::parseWithAutoConversion);104 }105 /**106 * Use <code>data.csv.listOfMapsAutoConverted</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.107 * Numeric values become values of Numeric type instead of String type.108 * <p>109 * Passed path is either relative based on working dir or absolute file path.110 * @param filePath relative file path or absolute file path111 * @return list of maps112 */113 public List<Map<String, Object>> listOfMapsAutoConverted(Path filePath) {114 return parseCsvTextAsStep(DataPath.fromFilePath(filePath),115 CsvUtils::parseWithAutoConversion);116 }117 /**118 * Use <code>data.csv.listOfMaps(header, path)</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.119 * <p>120 * Header will be taken from first parameter and first row of CSV file will not be treated as header.121 * <p>122 * Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.123 * @param header header values124 * @param fileOrResourcePath relative file path, absolute file path or classpath resource path125 * @return list of maps126 */127 public List<Map<String, String>> listOfMaps(Collection<String> header, String fileOrResourcePath) {128 return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),129 (text) -> CsvUtils.parse(header, text));130 }131 /**132 * Use <code>data.csv.listOfMaps(header, path)</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.133 * <p>134 * Header will be taken from first parameter and first row of CSV file will not be treated as header.135 * <p>136 * Passed path is either relative based on working dir or absolute file path.137 * @param header header values138 * @param filePath relative file path or absolute file path139 * @return list of maps140 */141 public List<Map<String, String>> listOfMaps(Collection<String> header, Path filePath) {142 return parseCsvTextAsStep(DataPath.fromFilePath(filePath),143 (text) -> CsvUtils.parse(header, text));144 }145 /**146 * Use <code>data.csv.listOfMapsAutoConverted(header, path)</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.147 * <p>148 * Header will be taken from first parameter and first row of CSV file will not be treated as header.149 * Numeric values become values of Numeric type instead of String type.150 * <p>151 * Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.152 * @param header header values153 * @param fileOrResourcePath relative file path, absolute file path or classpath resource path154 * @return list of maps155 */156 public List<Map<String, Object>> listOfMapsAutoConverted(List<String> header, String fileOrResourcePath) {157 return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),158 (text) -> CsvUtils.parseWithAutoConversion(header, text));159 }160 /**161 * Use <code>data.csv.listOfMapsAutoConverted(header, path)</code> to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.162 * Header will be taken from first parameter and first row of CSV file will not be treated as header.163 * Numeric values become values of Numeric type instead of String type.164 * Passed path is either relative based on working dir or absolute file path.165 * @param header header values166 * @param filePath relative file path or absolute file path167 * @return list of maps168 */169 public List<Map<String, Object>> listOfMapsAutoConverted(List<String> header, Path filePath) {170 return parseCsvTextAsStep(DataPath.fromFilePath(filePath),171 (text) -> CsvUtils.parseWithAutoConversion(header, text));172 }173 /**174 * Use <code>data.csv.write</code> to write data to CSV file.175 * @param path relative path or absolute file path of file to create176 * @param rows list of maps to write as CSV177 * @return full path to a newly created file178 */179 public Path write(Path path, List<Map<String, Object>> rows) {180 return writeCsvContentAsStep(path, () -> CsvUtils.serialize(rows));181 }182 /**183 * Use <code>data.csv.write</code> to write data to CSV file.184 * @param path relative path or absolute file path of file to create185 * @param rows list of maps to write as CSV186 * @return full path to a newly created file187 */188 public Path write(String path, List<Map<String, Object>> rows) {189 return writeCsvContentAsStep(Paths.get(path), () -> CsvUtils.serialize(rows));190 }191 private static <R> R parseCsvTextAsStep(DataPath dataPath, Function<String, R> convertor) {192 return readAndConvertTextContentAsStep("csv", dataPath, convertor);193 }194 private static Path writeCsvContentAsStep(Path path, Supplier<String> convertor) {195 return writeTextContentAsStep("csv", path, convertor);196 }197 @SuppressWarnings("unchecked")198 private TableData tableFromListOfMaps(List<?> listOfMaps) {199 if (listOfMaps.isEmpty()) {200 return new TableData(Collections.emptyList());201 }202 Map<String, Object> firstRow = (Map<String, Object>) listOfMaps.get(0);203 TableData result = new TableData(firstRow.keySet().stream());...

Full Screen

Full Screen

Source:CsvUtils.java Github

copy

Full Screen

...25import java.text.NumberFormat;26import java.util.*;27import java.util.stream.Stream;28import static java.util.stream.Collectors.*;29public class CsvUtils {30 private CsvUtils() {31 }32 public static List<Map<String, String>> parse(String content) {33 return parse(Collections.emptyList(), content);34 }35 public static List<Map<String, Object>> parseWithAutoConversion(String content) {36 return convertValues(NumberFormat.getNumberInstance(), parse(content));37 }38 public static List<Map<String, String>> parse(Collection<String> header, String content) {39 List<Map<String, String>> tableData = new ArrayList<>();40 CSVParser csvRecords = readCsvRecords(header, content);41 Collection<String> headerToUse = header.isEmpty() ?42 csvRecords.getHeaderMap().keySet() :43 header;44 for (CSVRecord record : csvRecords) {45 tableData.add(createRow(headerToUse, record));46 }47 return tableData;48 }49 public static List<Map<String, Object>> parseWithAutoConversion(List<String> header, String content) {50 return convertValues(NumberFormat.getNumberInstance(),51 parse(header, content));52 }53 public static String serialize(List<Map<String, Object>> rows) {54 if (rows.isEmpty()) {55 return "";56 }57 return CsvUtils.serialize(58 rows.get(0).keySet().stream(),59 rows.stream().map(Map::values));60 }61 public static String serialize(Stream<String> header, Stream<Collection<Object>> rows) {62 try {63 StringWriter out = new StringWriter();64 CSVPrinter csvPrinter = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader(header.toArray(String[]::new)));65 Iterator<Collection<Object>> it = rows.iterator();66 while (it.hasNext()) {67 csvPrinter.printRecord(it.next());68 }69 return out.toString();70 } catch (IOException e) {71 throw new RuntimeException(e);...

Full Screen

Full Screen

Source:DocumentationArtifacts.java Github

copy

Full Screen

...16 */17package org.testingisdocumenting.webtau.documentation;18import org.testingisdocumenting.webtau.data.table.Record;19import org.testingisdocumenting.webtau.data.table.TableData;20import org.testingisdocumenting.webtau.utils.CsvUtils;21import org.testingisdocumenting.webtau.utils.FileUtils;22import org.testingisdocumenting.webtau.utils.JsonUtils;23import java.nio.file.Path;24import java.util.Objects;25import java.util.concurrent.ConcurrentHashMap;26public class DocumentationArtifacts {27 private static final ConcurrentHashMap<String, Boolean> usedArtifactNames = new ConcurrentHashMap<>();28 public static void registerName(String artifactName) {29 Boolean previous = usedArtifactNames.put(artifactName, true);30 if (previous != null) {31 throw new AssertionError("doc artifact name <" + artifactName + "> was already used");32 }33 }34 public static void clearRegisteredNames() {35 usedArtifactNames.clear();36 }37 static Path capture(String artifactName, String text) {38 registerName(artifactName);39 Path path = DocumentationArtifactsLocation.resolve(artifactName);40 FileUtils.writeTextContent(path, text);41 return path;42 }43 static Path captureText(String artifactName, Object value) {44 return capture(artifactName + ".txt", Objects.toString(value));45 }46 static Path captureJson(String artifactName, Object value) {47 artifactName += ".json";48 if (value instanceof TableData) {49 return capture(artifactName, JsonUtils.serializePrettyPrint(((TableData) value).toListOfMaps()));50 } else {51 return capture(artifactName, JsonUtils.serializePrettyPrint(value));52 }53 }54 static Path captureCsv(String artifactName, Object value) {55 if (!(value instanceof TableData)) {56 throw new IllegalArgumentException("only TableData is supported to be captured as CSV");57 }58 TableData tableData = (TableData) value;59 return capture(artifactName + ".csv", CsvUtils.serialize(60 tableData.getHeader().getNamesStream(),61 tableData.rowsStream().map(Record::getValues)));62 }63}...

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2import org.testingisdocumenting.webtau.utils.FileUtils;3import java.util.List;4import java.util.Map;5public class 1 {6 public static void main(String[] args) {7 String csvContent = FileUtils.readFileContent("input.csv");8 List<Map<String, String>> csv = CsvUtils.parse(csvContent);9 System.out.println(csv);10 }11}12[ { id: "1", name: "John" }, { id: "2", name: "Smith" } ]13import org.testingisdocumenting.webtau.utils.CsvUtils;14import org.testingisdocumenting.webtau.utils.FileUtils;15import java.util.List;16import java.util.Map;17public class 1 {18 public static void main(String[] args) {19 List<Map<String, String>> csv = CsvUtils.parse("input.csv");20 System.out.println(csv);21 }22}23[ { id: "1", name: "John" }, { id: "2", name: "Smith" } ]24import org.testingisdocumenting.webtau.utils.CsvUtils;25import org.testingisdocumenting.webtau.utils.FileUtils;26import java.util.List;27import java.util.Map;28public class 1 {29 public static void main(String[] args) {30 List<Map<String, String>> csv = CsvUtils.parse("input.csv", ";");31 System.out.println(csv);32 }33}34[ { id: "1", name: "John" }, { id: "2", name: "Smith" } ]35import org.testingisdocumenting.webtau.utils.CsvUtils;36import org.testingisdocumenting.webtau.utils.FileUtils;37import java.util.List;38import java.util.Map;39public class 1 {40 public static void main(String[] args) {41 List<Map<String, String>> csv = CsvUtils.parse("input.csv", ";", "'");42 System.out.println(csv);43 }44}45[ { id: "1", name: "John" }, { id: "2", name: "Smith" } ]46import org.testingisdocumenting

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2import org.testingisdocumenting.webtau.utils.FileUtils;3import java.io.File;4import java.util.List;5public class 1 {6 public static void main(String[] args) {7 String csv = FileUtils.read(new File("src/test/resources/csv/1.csv"));8 List<String[]> rows = CsvUtils.parse(csv);9 for (String[] row : rows) {10 System.out.println(row[0] + " " + row[1] + " " + row[2]);11 }12 }13}14CSVUtils.parse() method15CSVUtils.parse() method supports the following CSV features:16CSVUtils.parse() method parameters17String csv = FileUtils.read(new File("src/test/resources/csv/1.csv"));18List<String[]> rows = CsvUtils.parse(csv);19CSVUtils.parse() method return value20CSVUtils.parse() method example217,8,9";22List<String[]> rows = CsvUtils.parse(csv);23for (String[] row : rows) {24 System.out.println(row[0] + " " + row[1] + " " + row[2]);25}26CSVUtils.parseCsv() method27CSVUtils.parseCsv() method supports the following

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1package org.testingisdocumenting.webtau.utils;2import java.io.IOException;3import java.nio.file.Files;4import java.nio.file.Paths;5import java.util.ArrayList;6import java.util.List;7public class CsvUtils {8 public static List<String[]> readCsv(String filePath) throws IOException {9 List<String[]> records = new ArrayList<>();10 String[] record;11 String recordString;12 String[] values;13 List<String> lines = Files.readAllLines(Paths.get(filePath));14 for (int i = 0; i < lines.size(); i++) {15 recordString = lines.get(i);16 values = recordString.split(",");17 record = new String[values.length];18 for (int j = 0; j < values.length; j++) {19 record[j] = values[j];20 }21 records.add(record);22 }23 return records;24 }25}26package org.testingisdocumenting.webtau.utils;27import java.io.IOException;28import java.util.List;29public class CsvUtils {30 public static void main(String[] args) throws IOException {31 List<String[]> records = CsvUtils.readCsv("C:\\Users\\sandeep\\Desktop\\WebTau\\Test\\src\\test\\resources\\test.csv");32 for (String[] record : records) {33 for (String value : record) {34 System.out.println(value);35 }36 System.out.println("37");38 }39 }40}

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2import java.util.List;3import java.util.Map;4public class CsvUtilsTest {5 public static void main(String[] args) {6";7 List<Map<String, String>> rows = CsvUtils.parse(csv);8 for (Map<String, String> row : rows) {9 System.out.println(row);10 }11 }12}13{a=1, b=2, c=3}14{a=4, b=5, c=6}

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2public class 1 {3 public static void main(String[] args) {4 List<Map<String, String>> csv = CsvUtils.parseCsv("a,b,c54,5,6");6 System.out.println(csv);7 }8}9import org.testingisdocumenting.webtau.utils.CsvUtils;10public class 2 {11 public static void main(String[] args) {12 List<Map<String, String>> csv = CsvUtils.parseCsv("a,b,c134,5,6", true);14 System.out.println(csv);15 }16}17import org.testingisdocumenting.webtau.utils.CsvUtils;18public class 3 {19 public static void main(String[] args) {20 List<Map<String, String>> csv = CsvUtils.parseCsv("a,b,c214,5,6", false);22 System.out.println(csv);23 }24}25import org.testingisdocumenting.webtau.utils.CsvUtils;26public class 4 {27 public static void main(String[] args) {28 List<Map<String, String>> csv = CsvUtils.parseCsv("a,b,c294,5,6", true, "a");30 System.out.println(csv);31 }32}

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2import java.util.List;3public class 1 {4 public static void main(String[] args) {5 List<List<String>> data = CsvUtils.read("1.csv");6 System.out.println(data);7 }8}9import org.testingisdocumenting.webtau.utils.CsvUtils;10import java.util.Arrays;11import java.util.List;12public class 2 {13 public static void main(String[] args) {14 List<List<String>> data = Arrays.asList(15 Arrays.asList("name", "age", "city"),16 Arrays.asList("john", "30", "LA"),17 Arrays.asList("jane", "25", "Chicago")18 );19 CsvUtils.write("2.csv", data);20 }21}22import org.testingisdocumenting.webtau.utils.CsvUtils;23import java.util.List;24public class 3 {25 public static void main(String[] args) {26 List<List<String>> data = CsvUtils.read("3.csv");27 System.out.println(data);28 }29}30import org.testingisdocumenting.webtau.utils.CsvUtils;31import java.util.Arrays;32import java.util.List;33public class 4 {34 public static void main(String[] args) {35 List<List<String>> data = Arrays.asList(36 Arrays.asList("name", "age", "city"),37 Arrays.asList("john", "30", "LA"),38 Arrays.asList("jane", "25", "

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2import java.util.List;3import java.util.Map;4public class CsvToMap {5 public static void main(String[] args) {6 List<Map<String, String>> rows = CsvUtils.readCsvAsListOfMaps("file.csv");7 rows.forEach(row -> System.out.println(row.get("column1")));8 }9}10import org.testingisdocumenting.webtau.utils.CsvUtils;11import java.util.List;12public class CsvToPojo {13 public static void main(String[] args) {14 List<Row> rows = CsvUtils.readCsvAsListOfPojo("file.csv", Row.class);15 rows.forEach(row -> System.out.println(row.getColumn1()));16 }17}18import org.testingisdocumenting.webtau.utils.CsvUtils;19import java.util.List;20public class CsvToPojoWithCustomMapping {21 public static void main(String[] args) {22 List<Row> rows = CsvUtils.readCsvAsListOfPojo("file.csv", Row.class, "column1:column1Name");23 rows.forEach(row -> System.out.println(row.getColumn1Name()));24 }25}26import org.testingisdocumenting.webtau.utils.CsvUtils;27import java.util.List;28public class CsvToPojoWithCustomMappingAndCustomSeparator {29 public static void main(String[] args) {30 List<Row> rows = CsvUtils.readCsvAsListOfPojo("file.csv", Row.class, "column1:column1Name", ';');31 rows.forEach(row -> System.out.println(row.getColumn1Name()));32 }33}

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.utils.CsvUtils;2import java.util.List;3import java.util.Map;4import java.util.stream.Collectors;5List<String> csv = CsvUtils.readCsv("test.csv");6List<Map<String, String>> csvAsMap = CsvUtils.readCsvAsMap("test.csv");7import org.testingisdocumenting.webtau.utils.CsvUtils;8import java.util.List;9import java.util.Map;10import java.util.stream.Collectors;11List<String> csv = CsvUtils.readCsv("test.csv");12List<Map<String, String>> csvAsMap = CsvUtils.readCsvAsMap("test.csv");

Full Screen

Full Screen

CsvUtils

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.io.Reader;3import java.util.*;4import java.util.stream.Collectors;5public class CsvUtils {6 private static final int INITIAL_READ_SIZE = 128;7 public static CsvData readCsv(Reader reader) {8 List<CsvRow> rows = new ArrayList<>();9 List<String> columnNames = new ArrayList<>();10 try (CsvReader csvReader = new CsvReader(reader)) {11 String[] columnNamesArray = csvReader.readNext();12 if (columnNamesArray != null) {13 columnNames.addAll(Arrays.asList(columnNamesArray));14 }15 String[] rowValues;16 while ((rowValues = csvReader.readNext()) != null) {17 rows.add(new CsvRow(columnNames, rowValues));18 }19 } catch (IOException e) {20 throw new RuntimeException("failed to read csv", e);21 }22 return new CsvData(columnNames, rows);23 }24 public static CsvData readCsv(String csvFileName) {25 try {26 return readCsv(new ResourceReader().read(csvFileName));27 } catch (IOException e) {28 throw new RuntimeException("failed to read csv file: " + csvFileName, e);29 }30 }31 public static class CsvData {32 private final List<String> columnNames;33 private final List<CsvRow> rows;34 public CsvData(List<String> columnNames, List<CsvRow> rows) {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Webtau automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful