I wrote a method to select the filters (two or more) based on the true/false conditions. Here is the method I used for selecting the multiple filters:
public T SetPropertyTypes<T>(bool residential, bool commercial) where T : IPage, new()
{
// Residential Property Type Check logic
if (residential && (ElementIsNotActive(() => FindElement(By.CssSelector(propertyTypeResidentialCss))).Invoke(Driver)))
ClickButton(() => FindElement(By.CssSelector(propertyTypeResidentialCss)), "Residential");
else if (ElementIsActive(() => FindElement(By.CssSelector(propertyTypeResidentialCss))).Invoke(Driver))
ClickButton(() => FindElement(By.CssSelector(propertyTypeResidentialCss)), "Residential");
// Commercial Property Type Check logic
if (commercial && (ElementIsNotActive(() => FindElement(By.CssSelector(propertyTypeCommercialCss))).Invoke(Driver)))
ClickButton(() => FindElement(By.CssSelector(propertyTypeCommercialCss)), "Commercial");
else if (ElementIsActive(() => FindElement(By.CssSelector(propertyTypeCommercialCss))).Invoke(Driver))
ClickButton(() => FindElement(By.CssSelector(propertyTypeCommercialCss)), "Commercial");
}
Later I found that the code is redundant and it could be better if I make it simpler and non-ambiguous. I'm new to C# and am not aware of many features. Is there any way to refactor this using any features of C#? I tried checking the variables using if-else-if but that's not working as intended.