t.me/atinfo_chat Telegram группа по автоматизации тестирования

Тестирование PDF?


(snark) #1

А чем можно тестировать генерированные отчеты в pdf? Есть какие-то инструменты,или писать свой велосипед, который парсит, сравнивает...

Сравнивать хеш не предлагать :)

Отчеты генерирует BIRТ, и сохраняются селениумом.  Управляется все Хатсоном.

 


(tekkerey) #2

Можно попробоват несколько способов описанных вот тут http://stackoverflow.com/questions/145657/how-to-compare-two-pdf-files


(Mykhailo Poliarush) #3

Подобных решений не надоходил, приходилось решать все программный способом. 

Например, на python можно использовать pyPDF

{syntaxhighlighter brush: bash;fontsize: 100; first-line: 1; }import pyPdf

class PDFLibrary:
“”" PDF library description “”"
ROBOT_TESTING_SCOPE = ‘GLOBAL’
pdf = ‘’

def __init__(self, path='d:\\LorstemMasterInvoice.pdf'):
	self.pdf = pyPdf.PdfFileReader(file(path, "rb"))

def get_text(self, page):
	""" get_text description is here """
	return self.pdf.getPage(int(page)).extractText() + " "

def check_text_contains(self, text_to_check):
	""" check_text_contains description is here """
	print self.get_text(0).find(text_to_check)
	if self.get_text(0).find(text_to_check) == -1:
		return False
	else:
		return True

def get_keyword_names(self):
	return ['get_text','check_text_contains']{/syntaxhighlighter}</p><p>также можно посмотреть реализацию на Ruby&nbsp;<a href="http://agilesoftwaretesting.com/?p=166">http://agilesoftwaretesting.com/?p=166</a></p><p>&nbsp;</p>

(Mykhailo Poliarush) #4

да кстати, для робот фреймворка люди написали тоже библиотеку. я думаю код можно переиспользовать в связке в селениум. https://code.google.com/p/robotframework-pdflibrary/source/browse/src/main/java/de/codecentric/robot/pdf/PDFKeywords.java

{syntaxhighlighter brush: java;fontsize: 100; first-line: 1; }/* * Copyright 2010 codecentric AG * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

package de.codecentric.robot.pdf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

@RobotKeywords
public class PDFKeywords {

? public static final String ROBOT_LIBRARY_SCOPE = “GLOBAL”;

? private PdfReader reader;
? private Map<Integer, String> pdfData;

? @RobotKeyword
? public void parsePdf(String filename) throws IOException {
? ? reader = new PdfReader(filename);
? ? System.out.println("Reading file "+filename);

? ? pdfData = new HashMap<Integer, String>();
? ? int numberOfPages = reader.getNumberOfPages();
? ? for (int page = 1; page <= numberOfPages; page++) {
? ? ? System.out.println("Reading page "+page);
? ? ? String textFromPage = PdfTextExtractor
? ? ? ? ? .getTextFromPage(reader, page);
? ? ? pdfData.put(page, textFromPage);

? ? }
? }

? @RobotKeyword
? public void pdfShouldContain(String expectedValue) {
? ? Collection<String> values = pdfData.values();
? ? collectionShouldContain(expectedValue, values);
? }

? @RobotKeyword
? public void pdfShouldContain(String expectedValue, String ignoreLinebreaks) {
? ? if (StringUtils.isEmpty(ignoreLinebreaks)) pdfShouldContain(expectedValue);
? ? Collection<String> values = pdfData.values();
? ? values = removeLinebreaks(values);
? ? collectionShouldContain(expectedValue, values);
? }

? @RobotKeyword
? public void pdfShouldContain(String expectedValue, String ignoreLinebreaks, String ignoreCase) {
? ? if (StringUtils.isEmpty(ignoreCase)) pdfShouldContain(expectedValue, ignoreLinebreaks);
? ? Collection<String> values = pdfData.values();
? ? values = removeLinebreaks(values);
? ? collectionShouldContain(expectedValue, values, StringUtils.isNotEmpty(ignoreCase));
? }
?
? private void collectionShouldContain(String expectedValue, Collection<String> values) {
? ? collectionShouldContain(expectedValue, values, false);
? }
?
? private void collectionShouldContain(String expectedValue, Collection<String> values, boolean ignoreCase) {
? ? for (String content : values) {
? ? ? if (ignoreCase?StringUtils.containsIgnoreCase(content, expectedValue):StringUtils.contains(content, expectedValue)) {
? ? ? ? return;
? ? ? }
? ? }
? ? throw new RuntimeException("could not find " + expectedValue + " in "
? ? ? ? + pdfData);
? }

? protected Collection<String> removeLinebreaks(Collection<String> values) {
? ? List<String> contentWithoutLinebreacks = new ArrayList<String>();
? ? for (String string : values) {
? ? ? String reformattedString = string.replaceAll("\n", " “);
? ? ? while (reformattedString.contains(” “)) {
? ? ? ? reformattedString = reformattedString.replaceAll(” ", " ");
? ? ? }
? ? ? contentWithoutLinebreacks.add(reformattedString);
? ? }
? ? values = contentWithoutLinebreacks;
? ? return values;
? }

}
{/syntaxhighlighter}

и его использование https://code.google.com/p/robotframework-pdflibrary/source/browse/src/test/java/de/codecentric/robot/pdf/PDFKeywordsTest.java

{syntaxhighlighter brush: java;fontsize: 100; first-line: 1; }/*

  • Copyright 2010 codecentric AG
  • Licensed under the Apache License, Version 2.0 (the “License”);
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an “AS IS” BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */

package de.codecentric.robot.pdf;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.Test;

public class PDFKeywordsTest {

    private PDFKeywords pdfLibrary;

    final static String SUCCESS_STORY_PDF = "effektive-neukundengewinnung.pdf";
    final static String ARTICLE_PDF = "business-case-fuer-agilitaet.pdf";
    final static String FIELD_GEBURTSNAME = "profil_geburtsdatum";
    final static String TITLE = "SUCCESS STORY";

    @Before
    public void setup() {
            pdfLibrary = new PDFKeywords();
    }
    
    @Test
    public void shouldParseField() throws IOException {
            pdfLibrary.parsePdf(SUCCESS_STORY_PDF);
            pdfLibrary.pdfShouldContain(TITLE);
    }
    
    @Test(expected=IOException.class)
    public void shouldFailOnNoFile() throws IOException {
            pdfLibrary.parsePdf(SUCCESS_STORY_PDF+"XXX");
    }

    @Test(expected=Throwable.class)
    public void shouldFailOnUnknownText() throws IOException {
            pdfLibrary.parsePdf(SUCCESS_STORY_PDF);
            pdfLibrary.pdfShouldContain(TITLE+"XXX");
    }
    
    @Test
    public void shouldFindWrappedLines() throws IOException {
            pdfLibrary.parsePdf(SUCCESS_STORY_PDF);
            pdfLibrary.pdfShouldContain("Ziel ist, innerhalb von 3 Jahren jeden Monat mehr als 100.000 "+
                                                                    "neue Adressen und ?ber 10.000 neue Kunden zu gewinnen.", "ignore linebreaks");
    }

    @Test
    public void shouldFindWrappedHeaders() throws IOException {
            pdfLibrary.parsePdf(ARTICLE_PDF);
            pdfLibrary.pdfShouldContain("Business Case f?r Agilit?t", "ignore linebreaks");
    }

    @Test
    public void shouldIgnoreCase() throws IOException {
            pdfLibrary.parsePdf(ARTICLE_PDF);
            pdfLibrary.pdfShouldContain("STUDIEN gehen DAVON aus, DASS fast ZWEI Drittel DER Funk- "+
                            "tionen in einer Anwendung selten oder ?berhaupt nicht "+
                            "GENUTZT WERDEN", "ignore linebreaks", "ignore case");
    }
    
    
    @Test
    public void shouldNotTouchWhenNoLinebreaks() {
            Collection&lt;String&gt; values = Arrays.asList(new String[] {"a", "b", "c"}); 
            values = pdfLibrary.removeLinebreaks(values);
            assertTrue(values.contains("a"));
            assertTrue(values.contains("b"));
            assertTrue(values.contains("c"));
    }

    @Test
    public void shouldRemoveLinebreaks() {
            Collection&lt;String&gt; values = Arrays.asList(new String[] {"a\nb", "c\nd"}); 
            values = pdfLibrary.removeLinebreaks(values);
            assertTrue(values.contains("a b"));
            assertTrue(values.contains("c d"));
    }

    @Test
    public void shouldRemoveDoubleSpaces() {
            Collection&lt;String&gt; values = Arrays.asList(new String[] {"a  b", "c   d", "e\n f", "g \nh"}); 
            values = pdfLibrary.removeLinebreaks(values);
            assertTrue(values.contains("a b"));
            assertTrue(values.contains("c d"));
            assertTrue(values.contains("e f"));
            assertTrue(values.contains("g h"));
    }

}
{/syntaxhighlighter}


(snark) #5

Спасибо, похоже  то что надо.Smile


(snark) #6

Спасибо, похоже  то что надо.Smile


(Mykhailo Poliarush) #7

you are welcome, если сделаешь что-то еще понакрученее, делись ;)