Skip to content

Commit

Permalink
Feat[TE-15905]: Extract text from alert addon. (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
SunilGembali authored Apr 2, 2024
1 parent 718e30b commit b41c19f
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
103 changes: 103 additions & 0 deletions extract_text_from_alert/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>extract_text_from_alert</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.6_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>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<scope>compile</scope>
</dependency>
<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>

</dependencies>
<build>
<finalName>extract_text_from_alert</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,55 @@
package com.testsigma.addons.web;

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

@Data
@Action(actionText = "Store the text in alert into a runtime variable variable-name",
description = "Store the text present in alert into a runtime variable",
applicationType = ApplicationType.WEB)
public class ExtractTextFromAlert extends WebAction {

@TestData(reference = "variable-name",isRuntimeVariable = true)
private com.testsigma.sdk.TestData testData;
@RunTimeData
private com.testsigma.sdk.RunTimeData runTimeData;

@Override
public Result execute() throws NoSuchElementException {
Result result = Result.SUCCESS;
String currentWindowHandle = driver.getWindowHandle();
try{
Alert alert = driver.switchTo().alert();
logger.info("Alert is present in the screen");

String alertText = alert.getText();
logger.info("Text in the alert is: "+alertText);

runTimeData.setKey(testData.getValue().toString());
runTimeData.setValue(alertText);
logger.info("Stored the alert text in runtime data");

setSuccessMessage("Successfully extracted text in alert and stored it in a runtime variable "+testData.getValue().toString());
} catch (NoAlertPresentException e){
logger.info(ExceptionUtils.getStackTrace(e));
setErrorMessage("There is no alert present in the screen");
result = Result.FAILED;
} catch (Exception e){
logger.info(ExceptionUtils.getStackTrace(e));
setErrorMessage("Unable to extract data from the alert");
result = Result.FAILED;
} finally {
driver.switchTo().window(currentWindowHandle);
}
return result;
}
}

0 comments on commit b41c19f

Please sign in to comment.