Skip to content

Commit

Permalink
FEAT[TE-16475]: Upgrade Validate data with regex patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
DivyaJakkampudi committed Feb 14, 2024
1 parent 82cb0ba commit f0ad5c3
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 0 deletions.
103 changes: 103 additions & 0 deletions validate_data_with_regex_pattern/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>validate_data_with_regex_pattern</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<testsigma.sdk.version>1.2.5_cloud</testsigma.sdk.version>
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
<lombok.version>1.18.20</lombok.version>

</properties>

<dependencies>
<dependency>
<groupId>com.testsigma</groupId>
<artifactId>testsigma-java-sdk</artifactId>
<version>${testsigma.sdk.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<scope>compile</scope>
</dependency>

</dependencies>
<build>
<finalName>validate_data_with_regex_pattern</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.testsigma.addons.android;

import com.testsigma.sdk.AndroidAction;
import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.Result;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.NoSuchElementException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@Action(actionText = "Verify if the text test-data matches the pattern regex-pattern",
description = "validates options count in a select drop-down",
applicationType = ApplicationType.ANDROID,
useCustomScreenshot = false)
public class RegExPatternMatch extends AndroidAction {

@TestData(reference = "test-data")
private com.testsigma.sdk.TestData testData;

@TestData(reference = "regex-pattern")
private com.testsigma.sdk.TestData regExPattern;


@Override
public Result execute() throws NoSuchElementException {
//Your Awesome code starts here
logger.info("Initiating execution");
logger.info("Test Data::"+testData.getValue());
logger.info("RegEx Pattern::"+regExPattern.getValue());

Result result = Result.SUCCESS;

try {
Pattern pattern = Pattern.compile(regExPattern.getValue().toString());
Matcher matcher = pattern.matcher(testData.getValue().toString());
if (matcher.matches()) {
setSuccessMessage("The input string matches the pattern.");
return Result.SUCCESS;
} else {
result = Result.FAILED;
setErrorMessage("The input string does not match the pattern.");
return result;
}
} catch (Exception error) {
result = Result.FAILED;
logger.info(" Exception occurred while executing the action::"+ ExceptionUtils.getStackTrace(error));
setErrorMessage("Exception occurred while executing the action::"+error.getMessage());
return result;
}

}
/* public static void main(String... a){
MyFirstWebAction myFirstWebAction = new MyFirstWebAction();
myFirstWebAction.setTestData(new com.testsigma.sdk.TestData("Showing 5 most recent authorizations"));
myFirstWebAction.setRegExPattern(new com.testsigma.sdk.TestData("Showing (\\d{1,2}|100) most recent authorizations"));
myFirstWebAction.execute();
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.testsigma.addons.android;

import com.testsigma.sdk.AndroidAction;
import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.Result;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.RunTimeData;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.openqa.selenium.NoSuchElementException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@Action(actionText ="Extract data matching regex and store to a variable, Regex: regex-pattern, Input String: Input-Data, Store Variable Variable-Name",
description = "Extracts the regex matching data from given value and stores the first matching data in given variable",
applicationType = ApplicationType.ANDROID)
public class StoreMatchingRegexValueInAVariable extends AndroidAction {

@TestData(reference = "Input-Data")
private com.testsigma.sdk.TestData testData;

@TestData(reference = "regex-pattern")
private com.testsigma.sdk.TestData regex;

@TestData(reference = "Variable-Name",isRuntimeVariable = true)
private com.testsigma.sdk.TestData runTimeVar;

@RunTimeData
private com.testsigma.sdk.RunTimeData runTimeData;

@Override
public Result execute() throws NoSuchElementException {
logger.info("Initiating execution");
String Regex = regex.getValue().toString();
String data = testData.getValue().toString();
String varName = runTimeVar.getValue().toString();
logger.info("Given test data : "+ data);
logger.info("Given regex-value : "+ Regex);
logger.info("Given runtime variable name : "+varName);
Result result = Result.SUCCESS;
try {
Pattern pattern = Pattern.compile(Regex);
Matcher matcher = pattern.matcher(data);
String matchedString;
if (matcher.find()) {
matchedString = matcher.group();
logger.info("Found the matched data : "+matcher.group());
} else {
setErrorMessage("Matching data with given regex is not found in given data");
return Result.FAILED;
}
runTimeData.setKey(varName);
runTimeData.setValue(matchedString);
setSuccessMessage("Successfully stored the value : "+matchedString+", into given variable :"+varName);
}
catch(Exception e) {
logger.debug(e.getMessage());
setErrorMessage("Failed to perform operation "+e.getMessage()+e.getCause());
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.testsigma.addons.ios;

import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.IOSAction;
import com.testsigma.sdk.Result;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.NoSuchElementException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@Action(actionText = "Verify if the text test-data matches the pattern regex-pattern",
description = "validates options count in a select drop-down",
applicationType = ApplicationType.IOS,
useCustomScreenshot = false)
public class RegExPatternMatch extends IOSAction {

@TestData(reference = "test-data")
private com.testsigma.sdk.TestData testData;

@TestData(reference = "regex-pattern")
private com.testsigma.sdk.TestData regExPattern;


@Override
public Result execute() throws NoSuchElementException {
//Your Awesome code starts here
logger.info("Initiating execution");
logger.info("Test Data::"+testData.getValue());
logger.info("RegEx Pattern::"+regExPattern.getValue());

Result result = Result.SUCCESS;

try {
Pattern pattern = Pattern.compile(regExPattern.getValue().toString());
Matcher matcher = pattern.matcher(testData.getValue().toString());
if (matcher.matches()) {
setSuccessMessage("The input string matches the pattern.");
return Result.SUCCESS;
} else {
result = Result.FAILED;
setErrorMessage("The input string does not match the pattern.");
return result;
}
} catch (Exception error) {
result = Result.FAILED;
logger.info(" Exception occurred while executing the action::"+ ExceptionUtils.getStackTrace(error));
setErrorMessage("Exception occurred while executing the action::"+error.getMessage());
return result;
}

}
/* public static void main(String... a){
RegExPatternMatch myFirstWebAction = new RegExPatternMatch();
myFirstWebAction.setTestData(new com.testsigma.sdk.TestData("Showing 5 most recent authorizations"));
myFirstWebAction.setRegExPattern(new com.testsigma.sdk.TestData("Showing 5 most recent authorizations"));
myFirstWebAction.execute();
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.testsigma.addons.ios;

import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.IOSAction;
import com.testsigma.sdk.Result;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.RunTimeData;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;
import org.openqa.selenium.NoSuchElementException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@Action(actionText ="Extract data matching regex and store to a variable, Regex: regex-pattern, Input String: Input-Data, Store Variable Variable-Name",
description = "Extracts the regex matching data from given value and stores the first matching data in given variable",
applicationType = ApplicationType.IOS)
public class StoreMatchingRegexValueInAVariable extends IOSAction {

@TestData(reference = "Input-Data")
private com.testsigma.sdk.TestData testData;

@TestData(reference = "regex-pattern")
private com.testsigma.sdk.TestData regex;

@TestData(reference = "Variable-Name",isRuntimeVariable = true)
private com.testsigma.sdk.TestData runTimeVar;

@RunTimeData
private com.testsigma.sdk.RunTimeData runTimeData;

@Override
public Result execute() throws NoSuchElementException {
logger.info("Initiating execution");
String Regex = regex.getValue().toString();
String data = testData.getValue().toString();
String varName = runTimeVar.getValue().toString();
logger.info("Given test data : "+ data);
logger.info("Given regex-value : "+ Regex);
logger.info("Given runtime variable name : "+varName);
Result result = Result.SUCCESS;
try {
Pattern pattern = Pattern.compile(Regex);
Matcher matcher = pattern.matcher(data);
String matchedString;
if (matcher.find()) {
matchedString = matcher.group();
logger.info("Found the matched data : "+matcher.group());
} else {
setErrorMessage("Matching data with given regex is not found in given data");
return Result.FAILED;
}
runTimeData.setKey(varName);
runTimeData.setValue(matchedString);
setSuccessMessage("Successfully stored the value : "+matchedString+", into given variable :"+varName);
}
catch(Exception e) {
logger.debug(e.getMessage());
setErrorMessage("Failed to perform operation "+e.getMessage()+e.getCause());
}

return result;
}
}
Loading

0 comments on commit f0ad5c3

Please sign in to comment.