From 5f7e414fd31985eb9706c20b28632c6d054be927 Mon Sep 17 00:00:00 2001 From: YamStranger Date: Sat, 6 Feb 2016 16:19:24 +0200 Subject: [PATCH 1/4] fix: updated test to fail if darkroom fail in parallel screenshot taking --- ...APhotographerTakesLotsOfScreenshots.groovy | 91 +++++++++++++------ 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy index a95c6dec58..f83f3c3f3d 100644 --- a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy +++ b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy @@ -4,6 +4,7 @@ import net.serenitybdd.core.photography.Darkroom import net.serenitybdd.core.photography.Photographer import net.serenitybdd.core.photography.ScreenshotPhoto import org.openqa.selenium.WebDriver +import org.openqa.selenium.chrome.ChromeDriver import org.openqa.selenium.firefox.FirefoxDriver import spock.lang.Specification @@ -12,57 +13,89 @@ import java.nio.file.Path class WhenAPhotographerTakesLotsOfScreenshots extends Specification { - def "when a photographer takes a series of screenshots they all should be stored"() { + def "when a photographer takes a series of screenshots they all should be stored"(def Class browser) { given: def photographer = new Photographer(); when: - def driver = new FirefoxDriver() - Darkroom.isOpenForBusiness(); List photos = Lists.newArrayList(); - for (photoCount in 0..10){ - photos.add(photographer.takesAScreenshot() + def driver + try { + driver = new FirefoxDriver() + Darkroom.isOpenForBusiness(); + for (photoCount in 0..10) { + photos.add(photographer.takesAScreenshot() .with(driver) .andSaveToDirectory(screenshotDirectory)); + } + Darkroom.waitUntilClose(); + } finally { + try { + if (driver) { + driver.close() + driver.quit() + } + } catch (some) { + } } - Darkroom.waitUntilClose(); - driver.quit() then: photos.findAll { photo -> Files.exists(photo.pathToScreenshot) }.size() == 11 + where: + browser << [ChromeDriver.class, FirefoxDriver.class] } - def "should handle multiple screenshots in parallel"() { + def "should handle multiple screenshots in parallel"(def Class browser){ given: def photographer = new Photographer(); - List photos = Lists.newArrayList(); + List files = Lists.newArrayList(); + def Integer threads = 10 + def Integer photos = 10 and: - def threads = [] - for (i in 1..10) { - threads << Thread.start { - Darkroom.isOpenForBusiness(); - def driver = new FirefoxDriver() - driver.get(siteFromUrlAt("/static-site/index.html")) - - for (j in 1..10) { - photos.add(photographer.takesAScreenshot() - .with(driver) - .andSaveToDirectory(screenshotDirectory)); + def runners = [] + for (i in 1..threads) { + runners << new Thread(new Runnable() { + @Override + void run() { + Darkroom.isOpenForBusiness(); + def driver = null + try { + driver = (WebDriver)browser.newInstance() + driver.get(siteFromUrlAt("/static-site/index.html")) + for (j in 1..files) { + files.add(photographer.takesAScreenshot() + .with(driver) + .andSaveToDirectory(screenshotDirectory)); + } + } finally { + try { + if (driver) { + driver.close() + driver.quit() + } + } catch (some) { + } + } + Darkroom.waitUntilClose(); } - driver.quit() - Darkroom.waitUntilClose(); - } + }) } - println threads + println runners when: - threads.forEach { it.join() } + runners.each { + it.start() + it.join() + } + runners.each { it.join() } then: - photos.size() == 100 - photos.findAll { + files.size() == threads * photos + files.findAll { photo -> Files.exists(photo.pathToScreenshot) - }.size() == 100 + }.size() == threads * photos + where: + browser << [ChromeDriver.class, FirefoxDriver.class] } - WebDriver driver; + Path screenshotDirectory; long startTime; From 1c84dd4f4a7b27abce81fdf265bbc4b7ba586f79 Mon Sep 17 00:00:00 2001 From: YamStranger Date: Tue, 1 Mar 2016 11:09:22 +0200 Subject: [PATCH 2/4] test: Darkroom can be used in parallel --- ...APhotographerTakesLotsOfScreenshots.groovy | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy index f83f3c3f3d..08b8ae809c 100644 --- a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy +++ b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy @@ -1,26 +1,28 @@ package net.serenitybdd.core.photography.integration + import com.google.common.collect.Lists import net.serenitybdd.core.photography.Darkroom import net.serenitybdd.core.photography.Photographer import net.serenitybdd.core.photography.ScreenshotPhoto -import org.openqa.selenium.WebDriver -import org.openqa.selenium.chrome.ChromeDriver -import org.openqa.selenium.firefox.FirefoxDriver +import org.openqa.selenium.phantomjs.PhantomJSDriver import spock.lang.Specification - import java.nio.file.Files import java.nio.file.Path +import java.util.concurrent.Callable +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors +import java.util.concurrent.Future class WhenAPhotographerTakesLotsOfScreenshots extends Specification { - def "when a photographer takes a series of screenshots they all should be stored"(def Class browser) { + def "when a photographer takes a series of screenshots they all should be stored"() { given: def photographer = new Photographer(); when: List photos = Lists.newArrayList(); def driver try { - driver = new FirefoxDriver() + driver = new PhantomJSDriver() Darkroom.isOpenForBusiness(); for (photoCount in 0..10) { photos.add(photographer.takesAScreenshot() @@ -41,29 +43,31 @@ class WhenAPhotographerTakesLotsOfScreenshots extends Specification { photos.findAll { photo -> Files.exists(photo.pathToScreenshot) }.size() == 11 - where: - browser << [ChromeDriver.class, FirefoxDriver.class] + } - def "should handle multiple screenshots in parallel"(def Class browser){ + def "should handle multiple screenshots in parallel"() { given: def photographer = new Photographer(); - List files = Lists.newArrayList(); + def List>> processing = new ArrayList<>(); + def List screenshots = new ArrayList<>(); def Integer threads = 10 def Integer photos = 10 - and: - def runners = [] - for (i in 1..threads) { - runners << new Thread(new Runnable() { + + def ExecutorService service = Executors.newFixedThreadPool(10); + when: + threads.times { + processing << service.submit(new Callable>() { @Override - void run() { + List call() throws Exception { + def List photo = new ArrayList<>() Darkroom.isOpenForBusiness(); def driver = null try { - driver = (WebDriver)browser.newInstance() + driver = new PhantomJSDriver() driver.get(siteFromUrlAt("/static-site/index.html")) - for (j in 1..files) { - files.add(photographer.takesAScreenshot() + photos.times { + photo.add(photographer.takesAScreenshot() .with(driver) .andSaveToDirectory(screenshotDirectory)); } @@ -77,23 +81,19 @@ class WhenAPhotographerTakesLotsOfScreenshots extends Specification { } } Darkroom.waitUntilClose(); + return photo } }) } - println runners - when: - runners.each { - it.start() - it.join() + processing.each { future -> + screenshots.addAll(future.get()) } - runners.each { it.join() } then: - files.size() == threads * photos - files.findAll { + processing.size() == threads + screenshots.size() == threads * photos + screenshots.findAll { photo -> Files.exists(photo.pathToScreenshot) }.size() == threads * photos - where: - browser << [ChromeDriver.class, FirefoxDriver.class] } Path screenshotDirectory; @@ -109,7 +109,7 @@ class WhenAPhotographerTakesLotsOfScreenshots extends Specification { String siteFromUrlAt(String path) { File baseDir = new File(System.getProperty("user.dir")); - File testSite = new File(baseDir, "src/test/resources" + path); + File testSite = new File(baseDir, "src/test/resources" + path); return "file://" + testSite.getAbsolutePath(); } From 28908c9c14e18411f38036db54d2c5fe4c383406 Mon Sep 17 00:00:00 2001 From: YamStranger Date: Tue, 1 Mar 2016 11:30:18 +0200 Subject: [PATCH 3/4] updated error handling in test --- .../integration/WhenAPhotographerTakesLotsOfScreenshots.groovy | 3 +++ 1 file changed, 3 insertions(+) diff --git a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy index 08b8ae809c..bc22ccc024 100644 --- a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy +++ b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy @@ -71,6 +71,9 @@ class WhenAPhotographerTakesLotsOfScreenshots extends Specification { .with(driver) .andSaveToDirectory(screenshotDirectory)); } + } catch (some) { + println some + } finally { try { if (driver) { From 1a6f15809e451f0c2b56e58e812ce663e3d78ae0 Mon Sep 17 00:00:00 2001 From: YamStranger Date: Tue, 1 Mar 2016 11:56:09 +0200 Subject: [PATCH 4/4] test: redusing resource usage during testing of darkroom --- .../integration/WhenAPhotographerTakesLotsOfScreenshots.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy index bc22ccc024..05e3efdc99 100644 --- a/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy +++ b/serenity-core/src/test/groovy/net/serenitybdd/core/photography/integration/WhenAPhotographerTakesLotsOfScreenshots.groovy @@ -52,7 +52,7 @@ class WhenAPhotographerTakesLotsOfScreenshots extends Specification { def List>> processing = new ArrayList<>(); def List screenshots = new ArrayList<>(); def Integer threads = 10 - def Integer photos = 10 + def Integer photos = 3 def ExecutorService service = Executors.newFixedThreadPool(10); when: