JAVA program to add top two maximum number of an Array.

Below program i have written to add top two max number of an Array:

package com.company;

public class ArrayDemo {


public int addAge(int [] age){

int largest = age[0];
int secondlargest = age[0];

for (int i=1;i<age.length;i++){

if ( age[i] > largest ){
secondlargest = largest;
largest = age[i];
}
if (age[i] < largest && age[i] > secondlargest){
secondlargest = age[i];
}

}


System.out.println("largest number is = " +largest);

System.out.println("Second largest number is = " +secondlargest);
int total = largest + secondlargest;
return total;
}


public static void main (String[] args){

int [] age = {12,88,13,4,15,25,5,49,87};

ArrayDemo arrayAddition = new ArrayDemo();

int total = arrayAddition.addAge(age);

System.out.println(total);


}
}

Selenium – For loop to navigate to the web element list and verify list item.

Step 1: Store item of the list in a List using findElements

List<WebElement> searchResultlist = Driver.driver.findElements(By.xpath("//span[@class='document-name']"));

Step 2: Use for loop to traverse in the list and verify the text using IF condition.
To make sure input by the user and text from the application are in same case, convert both the strings in UPPER case and match.

for (WebElement resultList : searchResultlist){
     System.out.println(resultList.getText());
     if (resultList.getText().toUpperCase().contains(DocumentNumber.toUpperCase())){
         resultList.click();
         break;
    }
}

How to set JAVA Path in Linux machine

Run below command to install JAVA

yum install java-1.8*

Once the installation is completed, run below command to check JAVA Version.

java -version

Above command should give you the following result.

openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

Run below command to find the JAVA path.

find /usr/lib/jvm/java-1.8* | head -n 3

Above command should give you the following results.

/usr/lib/jvm/java-1.8.0
/usr/lib/jvm/java-1.8.0-openjdk
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64

Use last path from above result to update the bash profile.

To update the bash profile run below command.

vi .bash_profile

Update bash_profile as below.

JAVA_HOME = /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
PATH=$PATH:$JAVA_HOME:$HOME/.local/bin:$HOME/bin

Save and Exit the bash_profile.

Refresh the bash profile using the below command.

source ~/.bash_profile

After refresh, run the following command to verify the JAVA path.

echo $JAVA_HOME

Output of the above command should be:

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64

Lambda expression in Selenium

How to use JAVA Lambda expression to select the value from drop down in selenium.

Let say you have a list and want to select a value from it.

<ul class = 'list'> 
 <li data-value="value1" class="option selection focus">Value1</li>
 <li data-value="value2" class="option selection focus">Value2</li> 
  <li data-value="value3" class="option selection focus">Value3</li> 
 <li data-value="value4" class="option selection focus">Value4</li> 

Below code can select the value from drop down as per the parameter passed in the method  

Public void selectValueFromDropDown(String SearchValue)
WebElement element = Driver.driver.findElements(By.xpath("//ul[@class='list']//li")).stream().
 filter(e-> e.getText().equalsIgnoreCase(SearchValue)).findFirst().get();
         element.click();