Skip to content

Commit 54a1d53

Browse files
Merge pull request #377 from testsigmahq/feat/CUS-12079-Added-class-to-update-the-given-xml-file
feat/CUS-12079-Added class to update the given xml file
2 parents 8f36491 + 5a0d83c commit 54a1d53

5 files changed

Lines changed: 222 additions & 9 deletions

File tree

update_xml/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.testsigma.addons</groupId>
88
<artifactId>updatexmlfile</artifactId>
9-
<version>1.0.11</version>
9+
<version>1.0.12</version>
1010
<packaging>jar</packaging>
1111

1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<maven.compiler.source>11</maven.compiler.source>
1515
<maven.compiler.target>11</maven.compiler.target>
16-
<testsigma.sdk.version>1.2.6_cloud</testsigma.sdk.version>
16+
<testsigma.sdk.version>1.2.24_cloud</testsigma.sdk.version>
1717
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
1818
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
1919
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+

update_xml/src/main/java/com/testsigma/addons/web/UpdateXmlWithNewData.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.openqa.selenium.NoSuchElementException;
1111
import org.w3c.dom.Document;
1212
import org.w3c.dom.Element;
13-
import org.w3c.dom.Node;
14-
import org.w3c.dom.NodeList;
1513

1614
import javax.xml.parsers.DocumentBuilder;
1715
import javax.xml.parsers.DocumentBuilderFactory;
@@ -41,7 +39,7 @@ public class UpdateXmlWithNewData extends WebAction {
4139
@Override
4240
protected Result execute() throws NoSuchElementException {
4341
logger.info("Initiating execution");
44-
com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS;
42+
Result result = Result.SUCCESS;
4543
try {
4644
logger.info("first level");
4745
File inputFile = new File(testData1.getValue().toString());
@@ -70,7 +68,7 @@ protected Result execute() throws NoSuchElementException {
7068
return result;
7169
} catch (Exception e) {
7270
String errorMessage = ExceptionUtils.getStackTrace(e);
73-
result = com.testsigma.sdk.Result.FAILED;
71+
result = Result.FAILED;
7472
setErrorMessage(errorMessage);
7573
logger.warn(errorMessage);
7674
return result;

update_xml/src/main/java/com/testsigma/addons/web/Updatexml.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.testsigma.addons.web;
22

3-
import com.testsigma.sdk.WebAction;
43
import com.testsigma.sdk.ApplicationType;
4+
import com.testsigma.sdk.WebAction;
55
import com.testsigma.sdk.annotation.Action;
66
import com.testsigma.sdk.annotation.TestData;
77
import lombok.Data;
@@ -10,14 +10,13 @@
1010
import org.w3c.dom.Document;
1111
import org.w3c.dom.Element;
1212

13-
import java.io.File;
14-
1513
import javax.xml.parsers.DocumentBuilder;
1614
import javax.xml.parsers.DocumentBuilderFactory;
1715
import javax.xml.transform.Transformer;
1816
import javax.xml.transform.TransformerFactory;
1917
import javax.xml.transform.dom.DOMSource;
2018
import javax.xml.transform.stream.StreamResult;
19+
import java.io.File;
2120

2221
@Data
2322
@Action(actionText = "Update the xml file absolutepath on the element value tagname by index indexvalue with attributename and attributevalue",
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.testsigma.addons.web;
2+
3+
import com.testsigma.sdk.WebAction;
4+
import com.testsigma.sdk.ApplicationType;
5+
import com.testsigma.sdk.annotation.Action;
6+
import com.testsigma.sdk.annotation.TestData;
7+
import lombok.Data;
8+
import org.apache.commons.lang3.exception.ExceptionUtils;
9+
import org.openqa.selenium.NoSuchElementException;
10+
import org.w3c.dom.Document;
11+
import org.w3c.dom.Element;
12+
13+
import java.io.File;
14+
import java.nio.file.Files;
15+
import java.nio.file.StandardCopyOption;
16+
17+
import javax.xml.parsers.DocumentBuilder;
18+
import javax.xml.parsers.DocumentBuilderFactory;
19+
import javax.xml.transform.Transformer;
20+
import javax.xml.transform.TransformerFactory;
21+
import javax.xml.transform.dom.DOMSource;
22+
import javax.xml.transform.stream.StreamResult;
23+
24+
@Data
25+
@Action(actionText = "Update the given xml file absolutepath on the element value tagname by index indexvalue with attributename and attributevalue",
26+
description = "updating the xml value using element tagname,index with atributename and value",
27+
applicationType = ApplicationType.WEB,
28+
useCustomScreenshot = false)
29+
public class UpdatexmlInGivenFile extends WebAction {
30+
31+
@TestData(reference = "absolutepath")
32+
private com.testsigma.sdk.TestData testData1;
33+
@TestData(reference = "tagname")
34+
private com.testsigma.sdk.TestData testData2;
35+
@TestData(reference = "indexvalue")
36+
private com.testsigma.sdk.TestData testData3;
37+
@TestData(reference = "attributename")
38+
private com.testsigma.sdk.TestData testData4;
39+
@TestData(reference = "attributevalue")
40+
private com.testsigma.sdk.TestData testData5;
41+
42+
43+
@Override
44+
public com.testsigma.sdk.Result execute() throws NoSuchElementException {
45+
logger.info("Initiating execution");
46+
com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS;
47+
try {
48+
String filePath = testData1.getValue().toString();
49+
String tagName = testData2.getValue().toString();
50+
int index = Integer.parseInt(testData3.getValue().toString());
51+
String attributeName = testData4.getValue().toString();
52+
String attributeValue = testData5.getValue().toString();
53+
54+
logger.info("Parameters - filepath: " + filePath + ", tagname: " + tagName + ", index: " + index + ", attributename: " + attributeName + ", attributevalue: " + attributeValue);
55+
56+
File inputFile = new File(filePath);
57+
if (!inputFile.exists()) {
58+
logger.warn("File not found: " + filePath);
59+
setErrorMessage("File not found: " + filePath);
60+
return com.testsigma.sdk.Result.FAILED;
61+
}
62+
63+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
64+
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
65+
Document doc = docBuilder.parse(inputFile);
66+
logger.info("XML file parsed successfully");
67+
68+
org.w3c.dom.NodeList nodes = doc.getElementsByTagName(tagName);
69+
logger.info("Total <" + tagName + "> elements found in document: " + nodes.getLength());
70+
71+
Element elementvalue = (Element) nodes.item(index);
72+
if (elementvalue == null) {
73+
logger.warn("No <" + tagName + "> element found at index " + index);
74+
setErrorMessage("No <" + tagName + "> element found at index " + index + ".");
75+
return com.testsigma.sdk.Result.FAILED;
76+
}
77+
78+
logger.info("Setting attribute '" + attributeName + "' = '" + attributeValue + "' on element <" + tagName + "> at index " + index);
79+
elementvalue.setAttribute(attributeName, attributeValue);
80+
81+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
82+
Transformer transformer = transformerFactory.newTransformer();
83+
DOMSource source = new DOMSource(doc);
84+
File tempFile = File.createTempFile("xml_update", ".tmp", inputFile.getParentFile());
85+
StreamResult resultfile = new StreamResult(tempFile);
86+
transformer.transform(source, resultfile);
87+
Files.move(tempFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
88+
89+
logger.info("XML file saved successfully at: " + inputFile.getAbsolutePath());
90+
setSuccessMessage("XML file updated successfully.");
91+
}catch (Exception e) {
92+
String errorMessage = ExceptionUtils.getStackTrace(e);
93+
result = com.testsigma.sdk.Result.FAILED;
94+
setErrorMessage(errorMessage);
95+
logger.warn(errorMessage);
96+
}
97+
return result;
98+
}
99+
}

0 commit comments

Comments
 (0)