Project

General

Profile

« Previous | Next » 

Revision 09a69162

Added by Andreas Kohlbecker over 10 years ago

selenium-tests: automatically taking screenshots of each page tested and updating to selenium 2.39

View differences:

7.x/modules/cdm_dataportal/test/java/dataportal-selenium-tests/pom.xml
12 12

  
13 13
  <properties>
14 14
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15
    <selenium-version>2.33.0</selenium-version>
15
    <selenium-version>2.39.0</selenium-version>
16 16
  </properties>
17 17

  
18 18
  <scm>
7.x/modules/cdm_dataportal/test/java/dataportal-selenium-tests/src/main/java/eu/etaxonomy/dataportal/pages/PortalPage.java
1 1
package eu.etaxonomy.dataportal.pages;
2 2

  
3
import java.io.File;
4
import java.io.IOException;
3 5
import java.lang.reflect.Constructor;
4 6
import java.net.MalformedURLException;
5 7
import java.net.URL;
......
7 9
import java.util.List;
8 10
import java.util.concurrent.TimeUnit;
9 11

  
12
import org.apache.commons.io.FileUtils;
10 13
import org.apache.log4j.Logger;
11 14
import org.openqa.selenium.By;
12 15
import org.openqa.selenium.NoSuchElementException;
16
import org.openqa.selenium.OutputType;
17
import org.openqa.selenium.TakesScreenshot;
13 18
import org.openqa.selenium.WebDriver;
14 19
import org.openqa.selenium.WebElement;
15 20
import org.openqa.selenium.interactions.Actions;
......
118 123
        // tell browser to navigate to the page
119 124
        driver.get(pageUrl.toString());
120 125

  
126
        takeScreenShot();
127

  
121 128
        // This call sets the WebElement fields.
122 129
        PageFactory.initElements(driver, this);
123 130

  
......
147 154
        // tell browser to navigate to the given URL
148 155
        driver.get(url.toString());
149 156

  
157
        takeScreenShot();
158

  
150 159
        if(!isOnPage()){
151 160
            throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " +  pageUrl + " )");
152 161
        }
......
180 189
        // driver.getCurrentUrl() is a sub path of the base path
181 190
        this.pageUrl = new URL(context.getBaseUri().toString());
182 191

  
192
        takeScreenShot();
193

  
183 194
        if(!isOnPage()){
184 195
            throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " +  pageUrl + " )");
185 196
        }
......
208 219
        if(!driver.getCurrentUrl().equals(pageUrl.toString())){
209 220
            driver.get(pageUrl.toString());
210 221
            wait.until(new UrlLoaded(pageUrl.toString()));
222
            // take screenshot of new page
223
            takeScreenShot();
211 224
            PageFactory.initElements(driver, this);
212 225
        }
213 226
    }
......
372 385
        } catch (Exception e) {
373 386
            throw new RuntimeException(e);
374 387
        }
388
        // take screenshot of new page
389
        takeScreenShot();
375 390
        return pageInstance;
376 391
    }
377 392

  
......
398 413
        return featureName.replace('_', '-');
399 414
    }
400 415

  
416
    public File takeScreenShot(){
417

  
418
        File destFile = fileForTestMethod(new File("screenshots"));
419

  
420
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
421
        try {
422
            FileUtils.copyFile(scrFile, destFile);
423
            logger.info("Screenshot taken and saved as " + destFile.getAbsolutePath());
424
            return destFile;
425
        } catch (IOException e) {
426
            logger.error("could not copy sceenshot to " + destFile.getAbsolutePath(), e);
427
        }
428

  
429
        return null;
430

  
431
    }
432

  
433
    /**
434
     * Finds the test class and method in the stack trace which is using the
435
     * PortalPage and returns a File object consisting of:
436
     * {@code $targetFolder/$className/$methodName }
437
     *
438
     *
439
     * If no test class is found it will fall back to using
440
     * "noTest" as folder name and a timestamp as filename.
441
     * @return
442
     */
443
    private File fileForTestMethod(File targetFolder){
444

  
445
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
446
        for (StackTraceElement stackTraceElement : trace) {
447
            // according to the convention all test class names should end with "Test"
448
            if(stackTraceElement.getClassName().endsWith("Test")){
449
                return uniqueIndexedFile(
450
                            targetFolder.getAbsolutePath() + File.separator + stackTraceElement.getClassName(),
451
                            stackTraceElement.getMethodName(),
452
                            "png");
453

  
454
            }
455
        }
456
        return uniqueIndexedFile(
457
                targetFolder.getAbsolutePath() + File.separator + "noTest",
458
                Long.toString(System.currentTimeMillis()),
459
                "png");
460
    }
461

  
462

  
463
    private File uniqueIndexedFile(String folder, String fileName, String suffix){
464
        File file;
465
        int i = 0;
466
        while(true){
467
            file = new File(folder + File.separator + fileName + "_" + Integer.toString(i) + "." + suffix);
468
            if(!file.exists()){
469
                return file;
470
            }
471
        }
472
    }
473

  
401 474

  
402 475
}
7.x/modules/cdm_dataportal/test/java/dataportal-selenium-tests/src/test/java/eu/etaxonomy/dataportal/selenium/tests/flMalesiana/FloraMalesiana_OriginalSourceTest.java
11 11

  
12 12
import static org.junit.Assert.assertEquals;
13 13

  
14
import java.io.File;
15 14
import java.util.List;
16 15
import java.util.UUID;
17 16

  
18
import org.apache.commons.io.FileUtils;
19 17
import org.junit.After;
20 18
import org.junit.Before;
21 19
import org.junit.Ignore;
22 20
import org.junit.Test;
23
import org.openqa.selenium.OutputType;
24
import org.openqa.selenium.TakesScreenshot;
25 21

  
26 22
import eu.etaxonomy.dataportal.DataPortalContext;
27 23
import eu.etaxonomy.dataportal.elements.LinkElement;
......
79 75

  
80 76
        List<LinkElement> primaryTabs = taxonProfileIillicium.getPrimaryTabs();
81 77

  
82
        // due to some obscure "magic" there are sometimes 5 tabs when the tests are run
83
        // headless, thus we take a screenshot:
84
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
85
        File destFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "Illicium-tabs.png");
86
        FileUtils.copyFile(scrFile, destFile);
87
        logger.info("Screenshot taken and saved as " + destFile.getAbsolutePath());
88

  
89 78
        assertEquals("Expecting 4 tabs", 4, primaryTabs.size());
90 79
        assertEquals("General\n(active tab)", primaryTabs.get(0).getText());
91 80
        assertEquals("Synonymy", primaryTabs.get(1).getText());

Also available in: Unified diff