I\'m using Selenium (within PHPUnit) to test my web application. I would like to test whether a certain image which is present on the page really exists. More precisely, whe
Well, in chrome one can write code in java to get the list of image src
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class imageUrl {
public static void main(String[] args) {
//Create Driver-object for chrome browser
System.setProperty("webdriver.chrome.driver","//Users//admin//Desktop//chromedriver");
WebDriver driver = new ChromeDriver();
//get the page
driver.get("https://jet.com/");
// this will find the elements by tag name. Don't forget to write "WebElement" after List.
//or else it will give an error on the for loop
List<WebElement>links=driver.findElements(By.tagName("img"));
// this will display list of all images exist on page
for(WebElement ele:links){
System.out.println(ele.getAttribute("src"));
}
}
}
use xPath, for example
//img[@src='The image src']
but unfortunately i am not sure that it is a good way to test if image was loaded
Assuming that you have an image in a WebElement (lets say img), in Java world you can retrieve the link below
Editing the answer to clarify. By Java world I mean Selenium 2.0 Java bindings. In Selenium 2.0 (of course if you are using webdriver) has a class called WebElement representing elements on the page. getAttribute is a selenium Method in Java binding.
String url = "http://www.my.website.com";
WebDriver driver = new FirefoxDriver();
driver.get(url);
WebElement img = driver.findElement(By.id("foo"));
String src = img.getAttribute("src");
Perhaps there is something similar in PHPUnit