How to verify data changed from String to image in C# Tuple / selenium specflow

Viewed 61

I'm trying to verify if Image element is displayed which used to be a string element (C# Selenium using specflow scenario outline Examples).

My apologies that my knowledge of coding is very minimal so my explanation might be not clear but I'll do my best. Sorry it's quite complicated to explain but it might not be that complicate.

Specflow scenario outline Examples:

//Image column used to fall under Condition when it was string, I've Image column for svg but not sure if I can do that to non-string value 
 
Then I verify "<Name>" with "<Condition>" "<Image>"row is displayed  
Examples:  
| Name | Condition |  Image |  
| Jim  |           |  svg   |  
| Jo   |      A    |        | 

Step Definition:

Then(@"I verify ""(.*)"" with ""(.*)"" ""(.*)""row is displayed")] 
//I've added bool image 
        public void ThenIVerifyNameWithRowIsDisplayed(string name, string condition, bool image) 
        { 
//Added "image" in the existing code   
var actualNameDetails = _myPage.GetABC_ByXYZ(name, condition, image); 
//This is existing code, Item2 is condition 
var actualTag = actualNameDetails.Item2; 
//I've added isImageDisplayed, Item3 is Image                          
var imageDisplayed = actualNameDetails.Item3; 
 
switch (condition) 
     { 
  case "Image": 
      { 
       //When it was String 
       _context.verify.AreEqual("svg", actualTag, $"Expected svg is displayed"); 
       //Now I need to update to verify if image exists 
       _context.verify.IsTrue(image, imageDisplayed); 
       break; 
      } 

Page Object:

//I've added bool as Item3 and bool isImageDisplayed 
public Tuple <string, string, bool> GetABC_ByXYZ(string id, string condition, bool image) 
 { 
  //the below is used for different condition tags, I didn’t add, this is existing 
  string tag = null; 
  //I've added bool isImageDisplayed 
 bool isImageDisplayed= IsElementDisplayed(By.XPath($"/div..........div/*[local-name()='svg']")); 
                  
 //tag 
    if (condition == "A") 
         { 
            tag = GetText(By.XPath($"/div............)); 
         } 
//My condition used to fit in else but not anymore since I've added Image column, what do I do with this else if?  
    else if (condition != "" && condition != "A") 
         { 
            //When element was String 
            //tag = GetText(By.XPath($"/div..............")); 
            //Now String is changed to svg, I need to update to validate the svg exists. Updated elementID to svg but need to get rid of GetText 
           //tag = GetText(By.XPath($"/div........div/*[local-name()='svg']"));                  
          } 
            return Tuple.Create(Name, tag, isImageDisplayed); 
     
1 Answers

First of all, if the image has a unique src attribute you can just try to find it in the webpage:

img[id='TheImageID'][src='TheImageSrc']

If you don't know the src attribute value, or that it may change very often - you can take real screen shot of the screen or the element using Selnium:

Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile("C://Image.png", 
        ScreenshotImageFormat.Png); 

You may also take a screenshot of a specific element:

 var img = GetElementScreenShot(driver, element);
                    img.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);

After saving the images - you can take more images and compare them to the baseline valid image.

Related