Word Document can't opened after capturing the Screenshots


#1

Hello Katalon Team,

Good Evening…

I have developed code to capture screenshots at run time and save in word document step by step using katalon. here i can able to capture the screenshot and same screenshot again i am saving under word document but the concern is while trying to open the word document i am receiving error alert popup “We’re Sorry . We cant open code for Word Document because we found a problem with its contents.”

Note: Same code if i run using Ecllipse IDE its capture screen shots and storing under word document. i can able to open the document without any issue.

worddocument%20issue

Could you please let us know why the code is not working under Katalon ??


#2

This doesn’t sound as though this is a problem in Katalon itself and that the cause is somewhere in the code you’re writing and using.

Follow the advice I posted here, and let’s see if someone can help further:


#3

Hi Thomas and Team,

Here is code

package word_Documents

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import groovy.lang.Closure;
import internal.GlobalVariable


public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {

			XWPFDocument docx = new XWPFDocument();
			XWPFRun run = docx.createParagraph().createRun();
			FileOutputStream out = new FileOutputStream("D:/Own Docs/doc1.docx");  // Word document location 

			for (int counter = 1; counter <= 5; counter++) {     //capture same screen shot and add in 5 time 
				captureScreenShot(docx, run, out);
				TimeUnit.SECONDS.sleep(1);
			}

			docx.write(out);
			out.flush();
			out.close();



		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void captureScreenShot(XWPFDocument docx, XWPFRun run, FileOutputStream out) throws Exception {

		String screenshot_name = System.currentTimeMillis() + ".png";
		BufferedImage image = new Robot()
				.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		File file = new File("XXX\\XXX\\XXx\\temp.png");   // Location to save PNG
		ImageIO.write(image, "png", file);
		InputStream pic = new FileInputStream(file);
		run.addBreak();
		run.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, screenshot_name, Units.toEMU(350), Units.toEMU(350));
		pic.close();
		file.delete();
	}

}

#4

Thanks for posting the code. This leaves me with more questions :wink:

  1. It’s extremely unusual to see a block of Katalon Groovy code using a main function. Why are you doing it this way?
  2. How do you import the class test into your Katalon TestCase scripts?
  3. Is there some reason you don’t want to use Katalon custom keywords (the conventional way to do this kind of thing)?
  4. Can you show me the code from an example TestCase that calls this code?

#5

Hi Russ_Thomas,

  1. It’s extremely unusual to see a block of Katalon Groovy code using a main function. Why are you doing it this way?
    - I Want to test the particular method, and to run the method in same class with out calling any other test cases i have used Main function.
  2. How do you import the class test into your Katalon TestCase scripts?
    • I created separate keyword(following the principles of katalon logic) and paste the code
  3. Is there some reason you don’t want to use Katalon custom keywords (the conventional way to do this kind of thing)?
    • We are using Katalon provided In-built keywords functionality
  4. Can you show me the code from an example TestCase that calls this code?
    • I haven’t try

Original Code :

import java.text.DateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openqa.selenium.OutputType;
import org.apache.commons.io.FileUtils
import org.apache.poi.*;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;

public class WordDocCodes{

@Keyword

public void genericWordDocUpdateCommentsandScreenshots() throws IOException, InvalidFormatException{

	String TestCaseName = GlobalVariable.currentTestCaseId
	println TestCaseName
	String[] Split_TC = TestCaseName.split('/')
	String TCname=Split_TC[2]
	println TCname

	String UserDir = (System.getProperty('user.dir'))+"/Screenshots/";
	String FilePath = (UserDir);
	println (FilePath);

	String newFolderPath=getLatestFilefromDir(FilePath);
	WebDriver driver;

	XWPFDocument docx = new XWPFDocument( new FileInputStream(FilePath+newFolderPath+"\\"+TCname+".docx"));
	List<XWPFParagraph>  paragraphs = docx.getParagraphs();
	XWPFParagraph paragraph =  paragraphs.get(paragraphs.size()-1);

	try {
		XWPFRun run = docx.createParagraph().createRun();
		FileOutputStream out = new FileOutputStream(FilePath+newFolderPath+"\\"+TCname+".docx");

		captureScreenShot(docx, run, out);
		TimeUnit.SECONDS.sleep(1);
		run.addBreak(BreakType.PAGE);

		XWPFRun runText = paragraph.createRun();
		runText.setText("Test case name : " +TCname);
		runText.addBreak();
		
		docx.write(out);
		out.flush();
		out.close();
		//docx.close();

	} catch (Exception e) {
		e.printStackTrace();
	}

}


@Keyword
public static void captureScreenShot(XWPFDocument docx, XWPFRun run, FileOutputStream out) throws Exception {

	String UserDir2 = (System.getProperty('user.dir'))+"/Images/";
	String imagesfilepath = (UserDir2);
	println (imagesfilepath);

	String screenshot_name = System.currentTimeMillis() + ".png";
	BufferedImage image = new Robot()
			.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

	//BufferedImage image = WebUI.takeScreenshot(imagesfilepath+"temp.png");

	File file = new File(imagesfilepath+"temp.png");
	ImageIO.write(image, "png", file);
	InputStream pic = new FileInputStream(file);
	run.addBreak();
	run.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, screenshot_name, Units.toEMU(500), Units.toEMU(450));
	run.addBreak();
	pic.close();
	//file.delete();
}

}

I found solution for the above issue, added external Apache POI Jar files to project and restarted Katalon and run the scripts… its working as expected