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 |
}
|
selenium-tests: automatically taking screenshots of each page tested and updating to selenium 2.39