Forum Discussion

rushikesh's avatar
rushikesh
Contributor
9 years ago
Solved

How to skip certain code if image does not exist in below code.

How to skip certain code if image does not exist in below code.

 

My code is 

 

var baseImage = Utils.Picture
var outputImage = Utils.Picture

baseImage.LoadFromFile(baseImagePath)
outputImage.LoadFromFile(outputImagePath)

diffImage = outputImage.Difference(baseImage);

if (diffImage == null)

Log.Checkpoint('Image comparision passed );
else
{
Log.Error('Image comparision failed)
diffImage.SaveToFile(diffImagePath)
}

 

In above code when there is no image present at either baseImagePath or outputImagePath we get error when diffImage = outputImage.Difference(baseImage); line is executed and my test stops.

 

How can i add a condition so that if no image is present at either baseImagePath or outputImagePath skip the code from diffImage = outputImage.Difference(baseImage);

  • EDIT: DOH!  I got my booleans backwards... I've edited the below code a bit... removed the not (!) so that we check for TRUE AND TRUE in the if logic... my bad

     

     

    The LoadFromFile method returns a boolean value to indicate success or failure to load the image.  So, you need to add some logic around those calls to detect whether or not the method.  This might not be optimal code, but I'd try something like

     

    var baseImage = Utils.Picture
    var outputImage = Utils.Picture
    
    if ((baseImage.LoadFromFile(baseImagePath) && (outputImage.LoadFromFile(outputImagePath)) {
    
    diffImage = outputImage.Difference(baseImage);
    
    if (diffImage == null)
    
    Log.Checkpoint('Image comparision passed );
    else
    {
    Log.Error('Image comparision failed)
    diffImage.SaveToFile(diffImagePath)
    }
    }
    else {
        Log.Error('One of the images did not successfully load');
    }



     

1 Reply

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    EDIT: DOH!  I got my booleans backwards... I've edited the below code a bit... removed the not (!) so that we check for TRUE AND TRUE in the if logic... my bad

     

     

    The LoadFromFile method returns a boolean value to indicate success or failure to load the image.  So, you need to add some logic around those calls to detect whether or not the method.  This might not be optimal code, but I'd try something like

     

    var baseImage = Utils.Picture
    var outputImage = Utils.Picture
    
    if ((baseImage.LoadFromFile(baseImagePath) && (outputImage.LoadFromFile(outputImagePath)) {
    
    diffImage = outputImage.Difference(baseImage);
    
    if (diffImage == null)
    
    Log.Checkpoint('Image comparision passed );
    else
    {
    Log.Error('Image comparision failed)
    diffImage.SaveToFile(diffImagePath)
    }
    }
    else {
        Log.Error('One of the images did not successfully load');
    }