I recently switch from C# to Java and not able to solve this problem. I am automation UI using selenium. I like to build a model for a list of elements on a page, retrieve property and then work with these properties. In the example bellow, I am searching on amazon.com and getting a list of results. I have SearchResultsModel
class which represents every item returned, public List<SearchResultModel> GetAllResults(bool title = false;bool isPrime = false;bool price = false)
method which retrieves data from UI and place it my model, it has default parameters which allows me to manipulate what data I want to retrieve instead of retrieving everything. Then I invoking by List<SearchResultModel> actual = myPage.GetAllResults(title:true,isPrime:true);
in this instance I get a list of SearchResultsModel that each contain only 2 properties - title and isPrime.
In ideal world I should retrieve all the data from the page but it takes lots of time to do so and defeats the whole purpose of automation of being faster then manual testing.
I could use method overload but then I end up with tens or even hundreds methods. In this example I have only 3 property so I will end up having 9 methods, in case of an object with 10 properties, I am afraid to even do a math. I could use varagrs but then building an argument will become a mess. I am not sure how to solve this problem in Java. Please advise
public class SearchResultsModel
{
//model that represents a single search result item
public string Title{get;set;}
public boolean IsPrime{get;set;}
public float Price {get;set;]
}
//method to retrieve all the search results from UI
public List<SearchResultModel> GetAllResults(bool title = false;bool isPrime = false;bool price = false)
{
List<SearchResultModel> toReturn = new List<SearchResultModel>();
IList<IWebElement> results = driver.FindElements(By.css("my locattors"))
foreach(IWebElement element in results)
{
SearchResultModel result = new SearchResultModel();
result.Title = title? element.FindElement(By.css("some locator")).GetText(): null;
result.IsPrime = isPrime? element.FindElement(By.css("some locator")).Selected(): false;
result.Price = price? element.FindElement(By.css("some locator")).GetText(): null;
toReturn.Add(result);
}
return toReturn;
}
//this is how I can invoke objects only with a specific properties
List<SearchResultModel> actual = myPage.GetAllResults(title:true,isPrime:true);
foreach(SearchResultModel model in actual)
{
Assert.That(model.isPrime == true);
}