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
You can use 'getAttribute' with XPath to get an image src (or any other attribute: alt, title, width, etc.)
selenium.getAttribute("//img/@src");
driver.findElement(By.cssSelector("img")).getAttribute("src")
If You are you using any PHP webdriver like facebook/phpwebdiver, then it will solve your problem very easily.
Example :
1) Create a wedriver object:
public function setUp()
{
parent::setUp();
$web_driver = new WebDriver();
$element = $web_driver->session();
}
With this $element
variable you can easily access any HTML attribute with the following line:
$element->element('xpath', '//*[@id="logo"]')->attribute('src');
This will return you the value of src
attibute. You can use this line to get any attribute like class, id, title, alt, etc...
You need to use XPath. For PHPUnit and Selenium the Syntax for getting the src attribute of an image is:
$imageUrl = $this->getAttribute("//img/@src");
Below code will help you to get the list of all images and their URLs. You can also use script instead on img to get the list of javascript files.
package seleniumlinkpackage; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class xmenfirstclass { public static void main(String[] args) throws InterruptedException { String url = "Paste Your URL here"; WebDriver driver = new FirefoxDriver(); driver.get(url); List 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")); } //Wait for 5 Sec Thread.sleep(5); // Close the driver driver.quit(); } }
you have to use
storeAttribute
And at the target place u have to mention like this
//img[@src='imagename.png']@src.
And store that in a variable lets say
img
Now verify that by using
verifyAttribute
eg:command:
verifyAttribute
target:
//img[@src='imagename.png']@src
value:
${img}