General Selenium Testing Samples

With selenium, you can run a test that checks all the images on the current page if they contain the ALT attribute and report images that do not have it?

+2
selenium
source share
3 answers

Yes. The first thing you need to do is decide which selenium you want to use.

  • You can use the Selenium IDE , where the test consists of an HTML table.
  • You can use Selenium RC , where you write a test in a programming language (for example, C #, Java, PHP, Ruby, etc.).

At first, the latter is a bit more complicated, but if you need real power, you will use this method.

Then you need to learn how to find all the images on the page. //img is a good XPath request to use. For Xpath details see w3schools , and especially this page .

Then you will want to find images with the alt attribute: //img[@alt]

One approach is to calculate how many images are there and subtract the number with alt attributes.

+6
source share

You can also do this in WebDriver (Selenium 2 coming soon). The following example is for TestNG / Java, but other client languages โ€‹โ€‹are available.

 List<WebElement> images = driver.findElements(By.xpath("//img[not(@alt)]")); assertEquals(images.size(), 0); 

For more information, you can also use something like the following to display image details without alt attributes:

 for(WebElement image : images) { System.out.println(image.getAttribute("src")); } 
+2
source share

We have a similar test, but we grab the Html page and process the images with a regular expression, and then match the second regular expression that looks for the alt tag. I did not pass the speed test, but I think it could be faster than running the Xpath route.

0
source share

Source: https://habr.com/ru/post/650764/


All Articles