Core Java - Collections Interview Questions

Core Java - Collections Interview Questions

In this page, we discuss frequently used core java topics in selenium framework. 

References: 
https://www.javatpoint.com/java-collections-interview-questions
https://www.javatpoint.com/java-linkedlist


Hierarchy of Collection Framework Image courtesy : JavaPoint




or





  1. How do you compare lists in java ? Give an example
    ( How do you validate web element labels in selenium ? )

1. What is List and how do you compare lists in java ? Give an example
    ( How do you validate web element labels in selenium ? )

  • List is an interface (It is a child interface of Collection) 
  • List interface extends Collection interface 
  • List interface implements ArrayList class (It also implements LinkedList, Stack, and Vector)
  • Used to store/maintain the ordered collection. 
  • package : java.util (import java.util.List;) 
  • Example : List<String> expectedSectionLables = new ArrayList<String>()
> Using equals method we can compare two lists. 
> For instance 
expectedSectionLables  and actualSectionLabels  are two lists as in below example and these lists can be compared as actualSectionLabels.equals(expectedSectionLables)
> equals returns a boolean value either true of false. 

@SuppressWarnings("serial")
List<String> expectedSectionLables = new ArrayList<String>() {
	{
		add("Text1");
		add("Text2");
		add("Text3");
		add("Text4)");
	}
};

@SuppressWarnings("serial")
List<String> actualSectionLabels = new ArrayList<String>() {
	
	{
		add(driver.findElement(By.xpath("//*[@id=\"xpath1\"]/span[1]/label")).getText());
		add(driver.findElement(By.xpath("//label[contains(text(),'xpath2')]")).getText());
		add(driver.findElement(By.xpath("//label[contains(text(),'xpath3')]")).getText());
		add(driver.findElement(By.xpath("//label[contains(text(),'xpath4")).getText());
	}
};

boolean compareLabels = actualSectionLabels.equals(expectedSectionLables);
Assert.assertEquals(true, compareLabels);

No comments:

Post a Comment