Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix[TE-19968]: Updated pdf_actions addon to latest version and removed unneccessary String.format usages #58

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pdf_actions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>pdf_actions</artifactId>
<version>1.0.2</version>
<version>1.0.7</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ protected PasswordAuthentication getPasswordAuthentication() {
doc = PDDocument.load(fileParse); //loads the file as pdf
sb.append(new PDFTextStripper().getText(doc));
Assert.assertTrue(sb.toString().contains((CharSequence) testData.getValue()));
setSuccessMessage(String.format(SUCCESS_MESSAGE + " " + testData.getValue()));
setSuccessMessage(SUCCESS_MESSAGE + " " + testData.getValue());
return Result.SUCCESS;
} catch (Exception e) {

String errorMessage = ExceptionUtils.getStackTrace(e);
logger.info(errorMessage);
setErrorMessage(String.format(ERROR_MESSAGE + " " + testData.getValue() + " " + "Cause of Exception:"
+errorMessage));
logger.warn("Error message:" +errorMessage +":" +sb.toString());
setErrorMessage(ERROR_MESSAGE + " " + testData.getValue() + " " + "Cause of Exception:"
+errorMessage);
return Result.FAILED;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.testng.Assert;
Expand Down Expand Up @@ -43,18 +45,21 @@ public com.testsigma.sdk.Result execute() {
fileParse = new BufferedInputStream(is); //reads data from file
doc = PDDocument.load(fileParse); //loads the file as pdf
sb.append(new PDFTextStripper().getText(doc));
Assert.assertTrue(sb.toString().contains((CharSequence) testData.getValue()));
setSuccessMessage(String.format(SUCCESS_MESSAGE + " " + testData.getValue()));
return Result.SUCCESS;
} catch (IOException e) {

e.printStackTrace();
setErrorMessage(String.format(ERROR_MESSAGE + "" + testData.getValue() + " " + "Cause of Exception:" + e.getCause().toString()));
return Result.FAILED;
}



if (sb.toString().contains((CharSequence) testData.getValue())) {
setSuccessMessage(SUCCESS_MESSAGE + " " + testData.getValue());
logger.info(SUCCESS_MESSAGE + " " + testData.getValue());
return Result.SUCCESS;
} else {
setErrorMessage(ERROR_MESSAGE + " " + testData.getValue());
logger.warn(ERROR_MESSAGE + " " + testData.getValue() +":" +sb.toString());
return Result.FAILED;
}
} catch (Exception e) {
String errorMessage = ExceptionUtils.getStackTrace(e);
setErrorMessage(ERROR_MESSAGE + "" + testData.getValue() + " " + "Cause of Exception:" + errorMessage);
logger.warn(ERROR_MESSAGE + "" + testData.getValue() + " " + "Cause of Exception:" + errorMessage);
return Result.FAILED;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public com.testsigma.sdk.Result execute() {
runTimeData = new com.testsigma.sdk.RunTimeData();
runTimeData.setValue(list2.toString().replaceAll("[^a-zA-Z0-9]", ""));
runTimeData.setKey(testData3.getValue().toString());
setSuccessMessage(String.format("Extracted "+testData1.getValue()+" characters i.e "+list2.toString().replaceAll("[^a-zA-Z0-9]", "")+" and stored it in runtime variable "+testData3.getValue().toString() ));
setSuccessMessage("Extracted "+testData1.getValue()+" characters i.e "+list2.toString().replaceAll("[^a-zA-Z0-9]", "")+" and stored it in runtime variable "+testData3.getValue().toString());
return Result.SUCCESS;


} catch (IOException e) {

e.printStackTrace();
setErrorMessage(String.format(ERROR_MESSAGE + "" + "Cause of Exception:" + e.getCause().toString()));
setErrorMessage(ERROR_MESSAGE + "" + "Cause of Exception:" + e.getCause().toString());
return Result.FAILED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public com.testsigma.sdk.Result execute() {
runTimeData = new com.testsigma.sdk.RunTimeData();
runTimeData.setValue(list2.toString().replaceAll("[^a-zA-Z0-9]", ""));
runTimeData.setKey(testData3.getValue().toString());
setSuccessMessage(String.format("Extracted "+testData1.getValue().toString()+" characters before test data "+testData2.getValue().toString()+" and stored extacted characters i.e "+list2.toString().replaceAll("[^a-zA-Z0-9]", "")+" in a runtime variable "+testData3.getValue().toString()));
System.out.println(String.format("Extracted "+testData1.getValue().toString()+" characters before test data "+testData2.getValue().toString()+" and stored extacted characters i.e "+list2.toString().replaceAll("[^a-zA-Z0-9]", "")+" in a runtime variable "+testData3.getValue().toString()));
setSuccessMessage("Extracted "+testData1.getValue().toString()+" characters before test data "+testData2.getValue().toString()+" and stored extacted characters i.e "+list2.toString().replaceAll("[^a-zA-Z0-9]", "")+" in a runtime variable "+testData3.getValue().toString());
System.out.println("Extracted "+testData1.getValue().toString()+" characters before test data "+testData2.getValue().toString()+" and stored extacted characters i.e "+list2.toString().replaceAll("[^a-zA-Z0-9]", "")+" in a runtime variable "+testData3.getValue().toString());
return Result.SUCCESS;

} catch (IOException e) {

e.printStackTrace();
setErrorMessage(String.format( ERROR_MESSAGE));
setErrorMessage( ERROR_MESSAGE);
//System.out.println(ERROR_MESSAGE + "" + "Cause of Exception:" + e.getCause().toString());
return Result.FAILED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public com.testsigma.sdk.Result execute() {
runTimeData = new com.testsigma.sdk.RunTimeData();
runTimeData.setValue(list2.toString().replaceAll("[^a-zA-Z0-9]", " "));
runTimeData.setKey(testData3.getValue().toString());
setSuccessMessage(String.format(SUCCESS_MESSAGE +""+"Next " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+" which is stored in runtime variable "+testData3.getValue().toString()));
System.out.println(String.format(SUCCESS_MESSAGE +""+"Next " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+" which is stored in runtime variable "+testData3.getValue().toString()));
setSuccessMessage(SUCCESS_MESSAGE +""+"Next " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+" which is stored in runtime variable "+testData3.getValue().toString());
System.out.println(SUCCESS_MESSAGE +""+"Next " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+" which is stored in runtime variable "+testData3.getValue().toString());
return Result.SUCCESS;
} catch (IOException e) {
e.printStackTrace();
setErrorMessage(String.format(ERROR_MESSAGE + "" + "Cause of Exception:" + e.getCause().toString()));
setErrorMessage(ERROR_MESSAGE + "" + "Cause of Exception:" + e.getCause().toString());
return Result.FAILED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public com.testsigma.sdk.Result execute() {
runTimeData.setKey(testData3.getValue().toString());


setSuccessMessage(String.format(SUCCESS_MESSAGE +" "+"Previous " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+"stored in runtime variable "+testData3.getValue().toString()));
System.out.println(String.format(SUCCESS_MESSAGE +" "+"Previous " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+"stored in runtime variable "+testData3.getValue().toString()));
setSuccessMessage(SUCCESS_MESSAGE +" "+"Previous " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+"stored in runtime variable "+testData3.getValue().toString());
System.out.println(SUCCESS_MESSAGE +" "+"Previous " + testData1.getValue() + " words are " + list2.toString().replaceAll("[^a-zA-Z0-9]", " ")+"stored in runtime variable "+testData3.getValue().toString());
return Result.SUCCESS;

} catch (IOException e) {
e.printStackTrace();
setErrorMessage(String.format(ERROR_MESSAGE+ "" + "Cause of Exception:" + e.getCause().toString()));
setErrorMessage(ERROR_MESSAGE+ "" + "Cause of Exception:" + e.getCause().toString());
return Result.FAILED;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ public com.testsigma.sdk.Result execute() {
reader.setStartPage(Integer.parseInt(testData2.getValue().toString()));
reader.setEndPage(Integer.parseInt(testData3.getValue().toString()));
String content = reader.getText(doc);
Assert.assertTrue(content.contains(testData.getValue().toString()));
setSuccessMessage(String.format(SUCCESS_MESSAGE +testData.getValue().toString()+" in given range of pages "));
logger.info(content);
return Result.SUCCESS;

} catch (IOException e) {

if(content.contains(testData.getValue().toString())){
setSuccessMessage(SUCCESS_MESSAGE +testData.getValue().toString()+" in given range of pages ");
logger.info(content);
return Result.SUCCESS;

}else {
setErrorMessage(ERROR_MESSAGE +testData.getValue().toString()+" in given range of pages ");
logger.warn(content);
return Result.FAILED;
}
} catch (Exception e) {
e.printStackTrace();
setErrorMessage(String.format(ERROR_MESSAGE + " " + "Cause of Exception:" + e.getCause().toString()));
setErrorMessage(ERROR_MESSAGE + " " + "Cause of Exception:" + e.getCause().toString());
return Result.FAILED;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.testsigma.addons.pdf.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.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

@Data
@Action(actionText = "Verify the content from pdf is contains text testdata",
description = "Extracts the text from the PDF and verify the text",
applicationType = ApplicationType.WEB)
public class VerifypdfContent extends WebAction {

@TestData(reference = "testdata")
private com.testsigma.sdk.TestData testData;

@Override
public Result execute() {
//Your Awesome code starts here
Result result = Result.SUCCESS;
logger.info("Initiating execution");
logger.debug("test-data: " + this.testData.getValue());
//StringBuffer sb = new StringBuffer();
URL url;
BufferedInputStream fileParse;
if (driver.getCurrentUrl().contains(".pdf")) {
setSuccessMessage("pdf file detected");
System.out.println("PDF detected");
} else {
setErrorMessage("Not a pdf file");
System.out.println("PDF not detected");
return Result.FAILED;
}
try {
url = new URL(driver.getCurrentUrl());
InputStream is = url.openStream(); //Converting url as input
fileParse = new BufferedInputStream(is); //reads data from file
PDDocument doc = null;
doc = PDDocument.load(fileParse); //loads the file as pdf
String st=new PDFTextStripper().getText(doc);

if(st.trim().contains(testData.getValue().toString())) {
setSuccessMessage("Assertion passed PDF content contains test data" + testData.getValue());
logger.info("Assertion passed PDF content contains test data" + testData.getValue());
}else {
setErrorMessage("Assertion failed PDF content does not contain test data" +testData.getValue());
logger.warn("Assertion failed PDF content does not contain test data" +st.trim());
return Result.FAILED;
}
} catch (IOException e) {
e.printStackTrace();
setErrorMessage("Cause of Exception:" + e.getCause().toString());
return Result.FAILED;
}
return result;
}
}