This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Screenshot via Test Listener integration
If derived from FunctionalTest, tests will generate screen shots in the event of before & after click, navigating to a new url, or test exception.
- Loading branch information
1 parent
42454c1
commit d93e8ed
Showing
7 changed files
with
279 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
functional-test/src/main/java/org/zanata/util/TestEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package org.zanata.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.NoAlertPresentException; | ||
import org.openqa.selenium.OutputType; | ||
import org.openqa.selenium.TakesScreenshot; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebDriverException; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.support.events.WebDriverEventListener; | ||
|
||
/** | ||
* @author Damian Jansen <a | ||
* href="mailto:djansen@redhat.com">djansen@redhat.com</a> | ||
*/ | ||
@Slf4j | ||
public class TestEventListener implements WebDriverEventListener { | ||
|
||
private WebDriver driver; | ||
private String targetDir; | ||
private String testId = ""; | ||
|
||
/** | ||
* A registered TestEventListener will perform actions on navigate, | ||
* click and exception events | ||
* @param drv the WebDriver to derive screen shots from | ||
* @param targetDirectory full path to screen shot storage | ||
*/ | ||
public TestEventListener(WebDriver drv, String targetDirectory) { | ||
driver = drv; | ||
targetDir = | ||
targetDirectory.endsWith("/") ? targetDirectory | ||
: targetDirectory.concat("/"); | ||
log.info("Writing screenshots to {}", targetDir); | ||
} | ||
|
||
/** | ||
* Update the screen shot directory/filename test ID component | ||
* @param testId test identifier string | ||
*/ | ||
public void updateTestID(String testId) { | ||
this.testId = testId; | ||
} | ||
|
||
private void createScreenshot(String ofType) { | ||
String outputDir; | ||
try { | ||
outputDir = targetDir.concat(testId); | ||
File testIDDir = new File(outputDir); | ||
testIDDir.mkdirs(); | ||
File screenshotFile = | ||
((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); | ||
FileUtils.copyFile(screenshotFile, new File(outputDir | ||
+ File.separator + generateFileName(ofType))); | ||
|
||
} catch (WebDriverException wde) { | ||
throw new RuntimeException("[Screenshot]: Invalid WebDriver: " | ||
+ wde.getMessage()); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException("[Screenshot]: Failed to write to " | ||
+ targetDir); | ||
} catch (NullPointerException npe) { | ||
throw new RuntimeException("[Screenshot]: Null Object: " | ||
+ npe.getMessage()); | ||
} | ||
} | ||
|
||
private String generateFileName(String ofType) { | ||
return testId.concat(":").concat(String.valueOf(new Date().getTime())) | ||
.concat(ofType).concat(".png"); | ||
} | ||
|
||
private boolean isAlertPresent(WebDriver driver) { | ||
try { | ||
driver.switchTo().alert(); | ||
return true; | ||
} catch (NoAlertPresentException nape) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void afterNavigateTo(String url, WebDriver driver) { | ||
createScreenshot("_nav"); | ||
} | ||
|
||
@Override | ||
public void beforeClickOn(WebElement element, WebDriver driver) { | ||
createScreenshot("_preclick"); | ||
} | ||
|
||
@Override | ||
public void afterClickOn(WebElement element, WebDriver driver) { | ||
if (isAlertPresent(driver)) { | ||
log.info("[Screenshot]: Prevented by Alert"); | ||
return; | ||
} | ||
createScreenshot("_click"); | ||
} | ||
|
||
@Override | ||
public void onException(Throwable throwable, WebDriver driver) { | ||
createScreenshot("_exc"); | ||
} | ||
|
||
/* | ||
* Unused interface functions | ||
*/ | ||
@Override | ||
public void afterChangeValueOf(WebElement element, WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void beforeNavigateTo(String url, WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void beforeNavigateBack(WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void afterNavigateBack(WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void beforeNavigateForward(WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void afterNavigateForward(WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void beforeFindBy(By by, WebElement element, WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void afterFindBy(By by, WebElement element, WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void beforeChangeValueOf(WebElement element, WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void beforeScript(String script, WebDriver driver) { | ||
} | ||
|
||
@Override | ||
public void afterScript(String script, WebDriver driver) { | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
functional-test/src/test/java/org/zanata/feature/FunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2013, Red Hat, Inc. and individual contributors as indicated by the | ||
* @author tags. See the copyright.txt file in the distribution for a full | ||
* listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this software; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF | ||
* site: http://www.fsf.org. | ||
*/ | ||
package org.zanata.feature; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.rules.TestName; | ||
import org.zanata.page.WebDriverFactory; | ||
import org.zanata.page.utility.HomePage; | ||
import org.zanata.util.ResetDatabaseRule; | ||
import org.zanata.workflow.AbstractWebWorkFlow; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author Damian Jansen <a | ||
* href="mailto:djansen@redhat.com">djansen@redhat.com</a> | ||
*/ | ||
@Slf4j | ||
@Category(DetailedTest.class) | ||
public class FunctionalTest { | ||
|
||
@ClassRule | ||
public static ResetDatabaseRule resetDatabaseRule = new ResetDatabaseRule(); | ||
|
||
@Rule | ||
public TestName name = new TestName(); | ||
|
||
@Before | ||
public void setupTestFunction() { | ||
WebDriverFactory.INSTANCE.getDriver(); | ||
updateTestId(); | ||
String date = new Date().toString(); | ||
log.info("[TEST] {}:{}:{}", getClass().getName(), name.getMethodName(), date); | ||
} | ||
|
||
private void updateTestId() { | ||
String testId = getClass().getName(); | ||
testId = testId.substring(testId.lastIndexOf(".")+1) | ||
.concat(":").concat(name.getMethodName()); | ||
WebDriverFactory.INSTANCE.updateListenerTestName(testId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters