|
| 1 | +package com.testsigma.addons.web; |
| 2 | + |
| 3 | +import com.testsigma.sdk.ApplicationType; |
| 4 | +import com.testsigma.sdk.Result; |
| 5 | +import com.testsigma.sdk.WebAction; |
| 6 | +import com.testsigma.sdk.annotation.Action; |
| 7 | +import com.testsigma.sdk.annotation.TestData; |
| 8 | +import lombok.Data; |
| 9 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 10 | +import org.openqa.selenium.NoSuchElementException; |
| 11 | +import org.w3c.dom.Document; |
| 12 | +import org.w3c.dom.Element; |
| 13 | +import org.w3c.dom.NodeList; |
| 14 | + |
| 15 | +import javax.xml.parsers.DocumentBuilder; |
| 16 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 17 | +import javax.xml.transform.Transformer; |
| 18 | +import javax.xml.transform.TransformerFactory; |
| 19 | +import javax.xml.transform.dom.DOMSource; |
| 20 | +import javax.xml.transform.stream.StreamResult; |
| 21 | +import java.io.File; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.StandardCopyOption; |
| 24 | + |
| 25 | +@Data |
| 26 | +@Action(actionText = "Update the given xml filepath by finding tagname with old_value at index and replacing with new_value", |
| 27 | + description = "Update the xml file by finding the Nth (index) element matching tagname and old_value, then replacing its content with new_value", |
| 28 | + applicationType = ApplicationType.WEB, |
| 29 | + useCustomScreenshot = false) |
| 30 | + |
| 31 | +public class UpdateXmlInGivenFileWithNewData extends WebAction { |
| 32 | + @TestData(reference = "filepath") |
| 33 | + private com.testsigma.sdk.TestData testData1; |
| 34 | + @TestData(reference = "tagname") |
| 35 | + private com.testsigma.sdk.TestData testData2; |
| 36 | + @TestData(reference = "old_value") |
| 37 | + private com.testsigma.sdk.TestData testData3; |
| 38 | + @TestData(reference = "new_value") |
| 39 | + private com.testsigma.sdk.TestData testData4; |
| 40 | + @TestData(reference = "index") |
| 41 | + private com.testsigma.sdk.TestData testData5; |
| 42 | + @Override |
| 43 | + protected Result execute() throws NoSuchElementException { |
| 44 | + logger.info("Initiating execution"); |
| 45 | + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; |
| 46 | + try { |
| 47 | + String filePath = testData1.getValue().toString(); |
| 48 | + String tagName = testData2.getValue().toString(); |
| 49 | + String oldValue = testData3.getValue().toString(); |
| 50 | + String newValue = testData4.getValue().toString(); |
| 51 | + int targetIndex = Integer.parseInt(testData5.getValue().toString()); |
| 52 | + |
| 53 | + logger.info("Parameters - filepath: " + filePath + ", tagname: " + tagName + ", old_value: " + oldValue + ", new_value: " + newValue + ", index: " + targetIndex); |
| 54 | + |
| 55 | + File inputFile = new File(filePath); |
| 56 | + if (!inputFile.exists()) { |
| 57 | + logger.warn("File not found: " + filePath); |
| 58 | + setErrorMessage("File not found: " + filePath); |
| 59 | + return com.testsigma.sdk.Result.FAILED; |
| 60 | + } |
| 61 | + |
| 62 | + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
| 63 | + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
| 64 | + Document doc = docBuilder.parse(inputFile); |
| 65 | + logger.info("XML file parsed successfully"); |
| 66 | + |
| 67 | + NodeList nodes = doc.getElementsByTagName(tagName); |
| 68 | + logger.info("Total <" + tagName + "> elements found in document: " + nodes.getLength()); |
| 69 | + |
| 70 | + Element matchedElement = null; |
| 71 | + int matchCount = 0; |
| 72 | + for (int i = 0; i < nodes.getLength(); i++) { |
| 73 | + Element el = (Element) nodes.item(i); |
| 74 | + if (el.getTextContent().equals(oldValue)) { |
| 75 | + if (matchCount == targetIndex) { |
| 76 | + matchedElement = el; |
| 77 | + break; |
| 78 | + } |
| 79 | + matchCount++; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + logger.info("Search: tag=<" + tagName + ">, old_value='" + oldValue + "', index=" + targetIndex + " -> " + (matchedElement != null ? "found" : "not found")); |
| 84 | + |
| 85 | + if (matchedElement != null) { |
| 86 | + logger.info("Updating element content from '" + oldValue + "' to '" + newValue + "'"); |
| 87 | + matchedElement.setTextContent(newValue); |
| 88 | + } else { |
| 89 | + logger.warn("No <" + tagName + "> element with value '" + oldValue + "' found at index " + targetIndex); |
| 90 | + setErrorMessage("No <" + tagName + "> element with value '" + oldValue + "' found at index " + targetIndex + "."); |
| 91 | + return com.testsigma.sdk.Result.FAILED; |
| 92 | + } |
| 93 | + |
| 94 | + // Save the changes to the XML file |
| 95 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 96 | + Transformer transformer = transformerFactory.newTransformer(); |
| 97 | + DOMSource source = new DOMSource(doc); |
| 98 | + File tempFile = File.createTempFile("xml_update", ".tmp", inputFile.getParentFile()); |
| 99 | + StreamResult resultFile = new StreamResult(tempFile); |
| 100 | + transformer.transform(source, resultFile); |
| 101 | + Files.move(tempFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 102 | + logger.info("XML file saved successfully at: " + inputFile.getAbsolutePath()); |
| 103 | + setSuccessMessage("XML file updated successfully."); |
| 104 | + } catch (Exception e) { |
| 105 | + String errorMessage = ExceptionUtils.getStackTrace(e); |
| 106 | + result = com.testsigma.sdk.Result.FAILED; |
| 107 | + setErrorMessage(errorMessage); |
| 108 | + logger.warn(errorMessage); |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | + |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments