0

I have no idea how can I compare two photo address, I tried to search answers for hours and still found nothing. So I came here and I am looking for answers

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class FirstAutomatedTest {
    
    private WebDriver driver;

    @BeforeMethod
    public void beforeTest() {
        System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe");
        driver = new ChromeDriver();
    }
    @Test
    public void myFirstTest() {
        driver.navigate().to("http://michalmarek.great-site.net/tytul/?i=2");
        driver.findElements(By.tagName("img"));
        driver.navigate().refresh();
        driver.findElements(By.tagName("img"));
    }
    @AfterMethod
    public void afterTest() {
    }
}
Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
Piwko
  • 11
  • 1
  • Please add the code in the question instead of a screenshot of the code. – Bouke May 18 '22 at 11:01
  • 1
    Also would like to recommend to be more specific, do you for instance want to know if the photos are binary identical? – Bouke May 18 '22 at 11:03
  • my page after every refresh shows different image and i want to check if their src's are different – Piwko May 18 '22 at 11:12

2 Answers2

2

I assume you are trying to compare two image sources not images. If that is your case then you can do like below,

Code:

driver.navigate().to("https://www.stackoverflow.com");
String orginalImage = driver.findElement(By.xpath("//div[@style='clip-path:url(#curve)']/div/img"))
        .getAttribute("src");
System.out.println("Original image source: " + orginalImage);
driver.navigate().refresh();
String refreshedImage = driver.findElement(By.xpath("//div[@style='clip-path:url(#curve)']/div/img"))
        .getAttribute("src");
System.out.println("Refreshed image source: " + refreshedImage);
Assert.assertEquals(orginalImage, refreshedImage, "Both image sources are not same.");

Output:

Original image source: https://cdn.sstatic.net/Img/home/illo-code.svg?v=b7ee00fff9d8
Refreshed image source: https://cdn.sstatic.net/Img/home/illo-code.svg?v=b7ee00fff9d8

If you want to compare images pixel by pixel then you need try these,

Nandan A
  • 2,702
  • 1
  • 12
  • 23
1

Welcome to SO.

According to your comment, you want to check all image source location should be different when the page refresh.

To be doing that, we need to store first image locations and compare this list after refresh.

@Test
public void myFirstTest() {
    driver.navigate().to("http://michalmarek.great-site.net/tytul/?i=2");
    List<String> firstImages = driver.findElements(By.tagName("img"))
            .stream()
            .map(e -> e.getAttribute("src"))
            .collect(Collectors.toList());
    driver.navigate().refresh();
    List<String> lastImages = driver.findElements(By.tagName("img"))
            .stream()
            .map(e -> e.getAttribute("src"))
            .collect(Collectors.toList());
    Assertions.assertTrue(Collections.disjoint(firstImages, lastImages));
}

Collections.disjoint Returns true if the two specified collections have no elements in common.