I'm using Selenide and Phantomjs to test a Webapplication.
Im taking the Screenshot as follows:
byte[] bytes = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.BYTES);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
returnImage = ImageIO.read(bis);
For input Elements, however, the screenshot looks like this:faulty screenshot
It actually looks like this (when using chrome/firefox)
how it's supposed to look
The interesting thing is, that when I set Selenide to use phantomjs (Configuration.Browser = "phantomjs") it takes the screenshots correctly.
It only occurs on that kind of element, too. Buttons etc are being recorded fine. Any ideas?
PS: The screenshots attached to this post are cropped, the code here takes screenshots of the entire page. In my code, I crop the screenshot only to the desired element but even on the screenshot displaying the entire screen the element is not recorded correctly.
You can take screenshot a element using below code:-
WebElement ele = driver.findElement(By.id("hplogo"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
Now in above code you are getting the getWidth() and getHeight(). You can try to adjust it by adding or subtracting the value.
Related
I am running into strange issue with generated pdf's from iText7. The generated pdf's are opening properly in Adobe reader and Chrome browser. But the same pdf is opening partially in the Firefox browser. I am getting the below message in Firefox. The strange thing is other pdf, which are not generated via iText are properly rendering in firefox.
Java code
public static byte[] createPdf(List<String> htmlPages, PageSize pageSize, boolean rotate) throws IOException {
ConverterProperties properties = new ConverterProperties();
// Register classpath protocol handler to be able to load HTML resources from class patch
org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.register();
properties.setBaseUri("classpath:/");
// properties.setBaseUri(baseUri);
FontProvider fontProvider = new DefaultFontProvider(true,false,false);
properties.setFontProvider(fontProvider);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(byteArrayOutputStream));
PdfMerger merger = new PdfMerger(pdf);
for (String htmlPage : htmlPages) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfDocument temp = new PdfDocument(new PdfWriter(baos));
if(rotate) {
temp.setDefaultPageSize(pageSize.rotate()); /** Page Size and Orientation */
} else {
temp.setDefaultPageSize(pageSize); /** Page Size and Orientation */
}
HtmlConverter.convertToPdf(htmlPage, temp, properties);
temp = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
merger.merge(temp, 1, temp.getNumberOfPages());
temp.close();
}
pdf.close();
byteArrayOutputStream.flush(); // Tried this
byteArrayOutputStream.close(); // Tried this
byte[] byteArray = byteArrayOutputStream.toByteArray();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try (FileOutputStream fileOuputStream = new FileOutputStream("D:\\Labels\\Label_"+timestamp.getTime()+".pdf")){
fileOuputStream.write(byteArray);
}
return byteArray;
}
Thanks in advance.
Edit 1:
You can find pdf and html/css for reproducing issue here.
When you embedded the images into your html using base64 URIs, something weird happened to the image of the barcode: Instead of the 205×59 bitmap image in labelData/barcode.png you embedded a 39578×44 image! (Yes, an image nearly a thousand times wider than high...)
The iText HtmlConverter embedded that image just fine but apparently Firefox has issues displaying an image with those dimensions even though (or probably because?) it is transformed into the desired dimensions (about four times wider than high) on the label. At least my Firefox installation stops drawing label contents right here. (Beware, the order of drawing in the PDF content is not identical to the of the HTML elements; in particular in the PDF the number 3232000... is drawn right before the barcode, not afterwards!)
On Firefox:
On Acrobat Reader:
Thus, you may want to check the transformation of the bar code image to the base64 image URI in your HTML file.
I have created 'takescreenshot' reusable method to capture screenshot and have been calling wherever I need it.
however I am facing one strange issue here. Every time this function is called the captured image size keeps increasing
e.g 252K -> 278K -> 310K -> 400K ...
These captured images I am using in ExtentReport. Also apart from selenium session image, I do see a black background image being captured not sure where it is coming from.
method code is as below:
public static void takescreenshot(ExtentTest Test,String Status){
Date d=new Date();
String CurrentTimeStamp=d.toString().replace(":", "_").replace(" ", "_");
File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(CurrentTimeStamp+".png"));
if(Status.equals("pass")){
Test.log(LogStatus.PASS, "snapshot below:-"+CurrentTimeStamp+".png"));
}else if(Status.equals("fail")){
Test.log(LogStatus.FAIL, "snapshot below:-"+CurrentTimeStamp+".png"));
}
}
if I hardcode some existing image in extentreport code then everything works fine.
Has anyone come across this issue ever.
I had written a code to capture screenshot of elements which works fine. May be this will help you. I don't know what Extendreport is, so can't help you there.
public static void takeElementScreenshot(WebDriver driver, WebElement element){
try{
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = element.getLocation();
// Get width and height of the element
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("D:\\Screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
}
catch(Exception e){
}
}
NOTE: I am aware this question has been asked before a few times however I am having a problem that others seem to not be having.
Even though I appear to be getting the point of the element along with its width and height correctly, the final crop is incorrect. It is like the screenshot I am taking has different dimensions to the webpage. I am using the Chrome Driver.
This is my code for attempting to get a screenshot of the google logo image:
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo"));
//Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
}
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
eleHeight);
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e) {
}
//Copy the element screenshot to disk
File screenshotLocation = new File("/Users/M/Desktop/stuff/logo.png");
try{
FileUtils.copyFile(screenshot, screenshotLocation);
}catch(IOException e){
}
Here is my final logo image:
Here is what it should be getting (just as a png not as gif):
Does anyone have any idea what might be going on?
Check if you have custom scaling on your operating system. I had the same problem using 125% scaling on windows.
Refer to the below screenshot for more details.
Go to your system settings > Search for the display > change the value of "Make Everything Bigger" dropdown to 100%. Then run the code again this will work fine for you.
I have a question. When I try to get images from a web page by using Jsoup in Java.
Here is the code:
String link = "http://truyentranhtuan.com/detective-conan/856/doc-truyen/";
Document docs = Jsoup.connect(link).timeout(60000).get();
Elements comics = docs.select("#hienthitruyen img");
System.out.println(comics.size());
for (Element comic : comics) {
int i = 0;
System.out.println(comic);
String linkImage = comic.attr("src");
if (!"".equals(linkImage)) {
URL url = new URL(linkImage);
BufferedImage image = ImageIO.read(url);
ImageIO.write(image, "jpg", new File(i + ".jpg"));
i++;
}
}
The problem is I can't get any img tag in this web page. The size of Elements always be zero.
But when I view source in this web page the img tag always be there.
If you look at the real source, not the DOM structure (for example, save the HTML page and open it in Notepad), you will see that there are no img tags there. They are all populated dynamically by the means of Javascript.
Now the problem is that Jsoup is not meant to execute Javascript, therefore you can only parse the original DOM structure, before it is modified (filled with images) by Javascript.
To do what you want, you can use HTMLUnit which can execute most of the Javascript.
I am developing a plugin in Eclipse, that shows the results in a scrolledComposite. The composite contains a JFace TreeViewer. I want to print this TreeViewer to the printer. I found import org.eclipse.swt.printing.Printer; to print to the printer.
But when i am printing using following snippet
GC gc= new GC(printer);
Control abc[] = Composite.getChildren();
abc[0].print(gc);
The tree that i want to print contains the workspace, project explorer.
The print output is showing only the icons. it is not displaying the names of classes, methods.
i cant post screenshot till my reputation is above 10.check it here
Please let me know if i am not clear..
Thanks in advance
Ramesh Emandi
Tree myWidget = treeViewer.getTree();
Point size = myWidget.getSize();
Image image = new Image(display, size.x, size.y);
GC gc = new GC(myWidget);
gc.copyArea(image, 0, 0);
gc.dispose();
// Get the ImageData and create a new printer Image from it
ImageData imageData = image.getImageData();
Image printImage = new Image(printer, imageData);
http://www.eclipse.org/swt/faq.php#noprintimage