Member-only story
Selenium Tests Should Not Include Java/C# Statements
3 min readMar 23, 2020
I hear sometimes people saying that, in certain conditions, it is ok to use Java/C# statements in Selenium UI tests.
For example, the following UI test checks that all product titles include the search keyword:
@Test
public void All_Product_Titles_Include_Search_Keyword()
{
HomePage homePage = new HomePage(driver); homePage.open();
ResultsPage resultsPage = homePage.searchFor(keyword);
foreach(Result result in resultsPage.allResults())
{
String title = result.title(); if (string.IsNullOrEmpty(title) == false)
{
Assert.IsTrue(title.Contains(keyword)); }
}
}
The test is pretty simple.
It opens the home page.
Then, a keyword search is executed so the results page is displayed.
Finally, in the results page, the test goes through all results and checks that each result title contains the keyword.
The test uses 2 page objects for homePage and resultsPage.
It also uses 2 statements, foreach and if.