Hi i am learning Selenium. my test case is to verify the "Learn SoapUI" link existance under TESTING tab in Guru99 site. When i use if condition it gets passed, when i use Assertion statement condition gets failed. Plz help mw with code. Url: https://www.guru99.com/ My Code:
@Test
public void Sample() {
driver=new FirefoxDriver();
driver.get("https://www.guru99.com/");
List<WebElement> TESTING= driver.findElements(By.xpath("//div[@class='featured-box cloumnsize1']//ul[1]//li"));
for(int i=0;i<TESTING.size();i++) {
Assert.assertEquals(TESTING.get(i).getText().equals("Learn SoapUI"), true);
System.out.println("Learn SoapUI link is present");
//*********************************************************************
if(TESTING.get(i).getText().equals("Learn SoapUI")) {
System.out.println("Learn SoapUI Testing link is in list");
}
}
In your code you are checking the text of each element in collection. If the the text of the firs element will not Learn SoapUI your test will be failed straightway.
The simple way will be to get text of all elements, put them to the List of Strings and check that Learn SoapUI contains in this list.
So here is your code with this changes:
driver=new FirefoxDriver();
driver.get("https://www.guru99.com/");
// Getting all elements from the cours
List<WebElement> TESTING = driver.findElements(By.xpath("//div[@class='featured-box cloumnsize1']//ul[1]//li"));
// Getting text from the each element and putting to List of Strings
List<String> listSoapCourses = TESTING.stream().map(WebElement::getText).collect(Collectors.toList());
// Verification that Learn SoapUI exists in the list of courses
Assert.assertTrue(listSoapCourses.contains("Learn SoapUI"), "Error message: Learn SoapUI not contains in the list of courses");