20

Case :

I have 2 iframes and both have lot of divs and other controls so both iframes are like the medium size of HTML websites. I want to compare both and find out differences.

I thought different options here :

Solution 1: Take a full screenshot of 2 iframes and compare both screenshots using the pillow library of Python which draws the grid on the mismatch area in a screenshot. But here the issue is I did not find any code on the internet which can take full iframe screenshots (I have a long iframe with a scroll bar). I tried almost all answers on SO but all are working for a normal page but not for the iframe.

Reference : https://blog.rinatussenov.com/automating-manual-visual-regression-tests-with-python-and-selenium-be66be950196

Solution 2: Get somehow all HTML code from both iframe and compare it, but this won't be easy to analyze result because it will find some HTML code that is different or have a mismatch in 2 iframes. This will be more like text compare and not a good solution I believe.

So I am looking for either code which can take a full screenshot of iframe using Python or Javascript OR some better option which allows me to compare 2 iframes and find out differences.

I tried almost all answers which google find our as per below :

enter image description here

Sample Iframe is given here where whole html is within iframe : https://grapesjs.com/demo.html , If some code can take full screenshot of this iframe then it will be easy to compare for me.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Helping Hands
  • 5,292
  • 9
  • 60
  • 127
  • All iframes underneath are normal html pages. Do you specifically want to see what they're like as iframes? – Matt Ellen Feb 06 '20 at 08:53
  • Yes all are under html page. My goal is to make sure both iframe looks same in terms of visual. – Helping Hands Feb 06 '20 at 09:15
  • Then would it be acceptable to open the pages the iframes show as top level pages and screen shot those? – Matt Ellen Feb 06 '20 at 09:18
  • I think so but how that is possible because all screenshot codes I tried are capable of taking full screenshot of any pages but when it comes to iframe, it takes screenshot of only portion which is visible without scroll. – Helping Hands Feb 06 '20 at 09:21
  • You have the iframe urls, so you can open them as top level pages. – Matt Ellen Feb 06 '20 at 09:49
  • I do not have iframe specific url as iframe is part of some page. here it is : https://prnt.sc/qyckic – Helping Hands Feb 06 '20 at 10:08
  • But if you insepect the html, the url of the iframe will be present, otherwise your browser cannot load it – Matt Ellen Feb 06 '20 at 10:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207339/discussion-between-helping-hands-and-matt-ellen). – Helping Hands Feb 06 '20 at 10:16

5 Answers5

7

As we discovered in our chat, the iframes under discussion are generated in javascript and not loaded from a URL.

This presents a difficulty in automating screen grabbing the iframe, however a manual process is possible:

In Firefox right click on the iframe and select "This Frame" in the popup menu, then select "Save Frame As...".

image of menus

Once the frame is saved, some of the downloaded CSS will need to be fiddled with to get the background URLs to point to the correct place. Having done that, open the html file locally and you will be able to take a screen shot using the method you currently use for a normal web page.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
6

Grabbing part of the screen

You can either grab it manually or automatically. If there are not many iframes to compare, then doing it manually is an option, you just do a screenshot which contains the content and crop the image if necessary. The difficulty of this approach is that you need to be very very precise while cropping.

You can do it automatically as well, for example loading the part of the DOM into a canvas and making a picture of it, like here: Using HTML5/Canvas/JavaScript to take in-browser screenshots

Also, you can modify temporarily your content to make sure the whole page is what you are interested about and then do a screenshot, as described here: https://www.cnet.com/how-to/how-to-take-a-screenshot-of-a-whole-web-page-in-chrome/

Comparing two images

You can compare two images by looping all their pixels and comparing them.

Algorithm to compare two images

Showing the results

Your program should take two images as input and create a new image of similar size as output. I would suggest that the target image should show the pixels of one of the images to compare and to draw a red line at the border of each differences. For this purpose you will need to divide a region of differences into rectangles. This way you could see where the differences are and what is the content that is different.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Thank you for your answer. Is there any chance I can use Html2Canvas with python because I need something in python or javascript which can take full screenshot of iframe. – Helping Hands Feb 12 '20 at 07:02
  • @HelpingHands HTML2Canvas is a Javascript tool, so you can use it via Javascript (see: https://html2canvas.hertzen.com/). Searching for Python usages of HTML2Canvas, I have found a Python proxy. I didn't try it, but I hope it helps. Here it is: https://github.com/brcontainer/html2canvas-python-proxy – Lajos Arpad Feb 12 '20 at 07:21
2

You could use pillow in combination with pyautogui, mayby pyautogui alone.

Some pseudo code:

As long as the scrollbar doesnt touch the bottom: - take a screenshot - save screenshot name in list - scroll down, continue this loop

Do the above loop again for the second iframe

compare all the screenshots from the two lists of screenshots you have generated.

Well, that's how I would do it. There are probably better ways though.

  • Thing is I have only one iframe but it is long vertically and no tool is able to take it's full screenshot. – Helping Hands Feb 12 '20 at 06:50
  • You don't need one long screenshot. Several screenshots will do great as well. As long as you always scroll the same amount down. So basically, you let your iframe get focus. Then you press the down arrow 5 (or whatever your need) times and take a screenshot. It doesn't matter if you have a bit of double content in your screenshots. You do the same for the other iframe. – PythonAmateur742 Feb 18 '20 at 10:46
1

Use html2canvas for taking screenshot

html2canvas(document.getElementById("img_dots")).then(
  function (canvas2) {
    var img_data2 = canvas2.toDataURL('image/png');
    var im_data2 = img_data2.replace('data:image/png;base64,', '');
    $.ajax({
      type: "POST",
      url: "send_image_to_backend",
      data: {
        "base64data": im_data2,
        // "filename": filename.split(".")[0]
        "filename": new_filename + ".png"
      }, success: function () {
         //send second image and compare
      }
    });
  }
);

This will enable you to send images to back-end.

Use this thread to tweak html2canvas to fetch entire image

Once you have both images you can use openCV to find difference between 2 images you can refer to this - https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/

Aqua 4
  • 771
  • 2
  • 9
  • 26
1

I have 2 iframes and both have lot of divs and other controls so both iframes are like the medium size of HTML websites. I want to compare both and find out differences.

What do you want compare ? CSS rules/properties differences ? Data/text differences ? Or visual render ?


Compare visual render

You can extract the iframe URL and load the page with Selenium to take screenshot (see example). Also you have firefox extension Selenium IDE.

user2226755
  • 12,494
  • 5
  • 50
  • 73
  • Thank you for your answer. Actually my iframe has no url or src. And take screenshot only take view port screenshot but I need full screenshot of iframe. – Helping Hands Feb 16 '20 at 13:41