Friday, April 22, 2022

Selenium WebDriver Browser Commands Explained: Best Practices and Examples

package selenium;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium_Webdriver {
public static void main(String[] args) {
String driverExecutablePath = "C:\\Selenium\\chromedriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverExecutablePath);

// Create a new instance of the Chrome driver 
WebDriver driver = new ChromeDriver(); 

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.manage().window().maximize();

// Storing the Application Url in the String variable 
String url = "https://suriyaparithy.blogspot.com/"; 

//Launch the ToolsQA WebSite 
driver.get(url); 

// Storing Title name in the String variable 
String title = driver.getTitle(); 

// Storing Title length in the Int variable 
int titleLength = driver.getTitle().length(); 

// Printing Title & Title length in the Console window 
System.out.println("Title of the page is : " + title); 
System.out.println("Length of the title is : "+ titleLength); 

// Storing URL in String variable 
String actualUrl = driver.getCurrentUrl(); 

if (actualUrl.equals(url)){ 
System.out.println("Verification Successful - The correct Url is opened.");
}
else {
System.out.println("Verification Failed - An incorrect Url is opened."); 

//In case of Fail, you like to print the actual and expected URL for the record purpose 
System.out.println("Actual URL is : " + actualUrl); 
System.out.println("Expected URL is : " + url);
}

// Storing Page Source in String variable 
String pageSource = driver.getPageSource(); 

// Storing Page Source length in Int variable 
int pageSourceLength = pageSource.length(); 

// Printing length of the Page Source on the console 
System.out.println("Total length of the Pgae Source is : " + pageSourceLength); 

}
}


From Beginner to Expert: Java Loop Programming

 Loops :

  •   Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

Types of Loops :

  for loop: This executes a statement a particular number of times.

  while loop: This executes a statement an unknown number of times.

  do-while loop: This executes a statement at least one time.

The syntax for the "for" loop is :

  for(Variable Initialization; Boolean_Expression Test; Increment/Decrement){

   //Statements

}

Example :

package selenium;

public class Java_practise {

public static void main(String[] args) {

  // This will print -- 0,1,2,3,4,5

for(int Increment = 0;Increment<=5;Increment++){

System.out.println("Count is  ==> " + Increment );

}

// This will print -- 5,4,3,2,1,0

for(int Decrement = 5;Decrement>=0;Decrement--){

System.out.println("Count is ==> " + Decrement );

}

// This will print -- 0,2,4

for(int Increment = 0;Increment<=5;Increment+=2){

System.out.println("Skip every one another  ==> " + Increment );

}

// This will print -- 0,1,2,3,4,5

for(int Count = 0;Count<=10;Count++)

{

if(Count==6){

break;

}

System.out.println("Count is ==> " + Count );

}

// This will just print -- 3

            for(int Count = 0;Count<=5;Count++){

             if(Count==3){

         System.out.println("Count is ==> " + Count);

          continue;

}

}

}

}


The syntax for the while loop :
   while(Boolean_Expression Test){
//Statements
}

Example :
package selenium;
public class Java_practise {
   public static void main(String[] args) {
int Count = 0;
// This will print -- 5,10,15,20,25
while(Count < 25){
Count = Count + 5;
System.out.println("Count is ==> "+ Count);
}        
}
}

The syntax for the do-while loop  :
   do{
//Statements
}while(Boolean_Expression Test);

Example :
package selenium;
public class Java_practise {
  public static void main(String[] args) {
int CountOnce = 25;
System.out.println("<==== Next Count ====>");
// This will just print once 
do{
CountOnce = CountOnce + 5;  
System.out.println("Count is ==> "+ CountOnce);
}while(CountOnce < 25);
     
}
}

The syntax for enhanced for loop :
   for (data_type variable: array_name)

Examples :
package selenium;
public class Java_practise {
     public static void main(String[] args) {
// Array of String storing days of the week
    String days[] = { "Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun"};

    // Enhanced for loop, this will automatically iterate on the array list 
    for (String dayName : days) {
      System.out.println("Days ==> "+ dayName);
    }

    System.out.println("<==== Normal For Loop ====>");
    // Normal for loop
    for (int i=0; i < days.length; i++) {
        System.out.println("Days ==> "+ days[i]);

     }
}
}



Thursday, April 21, 2022

Java Arrays: From Basics to Beyond

 Arrays :
  •  An array is a type of variable that can store multiple values. It is like a list of items but it always contains similar data type values.
  • An array is a data structure in java that can hold one or more values in a single variable.
  •  Array in java is a collection of similar types of values.

Example 1:
  String[] aCarMake = new String[5];
      aCarMake[0] = "BMW";
      aCarMake[1] = "AUDI";
      aCarMake[2] = "TOYOTA";
      aCarMake[3] = "SUZUKI";
      aCarMake[4] = "HONDA";

Example 2:
  String [] aCarMake = {"BMW", "AUDI", "TOYOTA", "SUZUKI", "HONDA"};

Example 3:
package selenium;
public class Java_practise {
public static void main(String[] args) {
//Declaring an Array
String [] aMake = {"BMW", "AUDI", "TOYOTA", "SUZUKI", "HONDA"};

// Calling the Print Array method and passing an Array as a parameter
Print_Array(aMake);
}

//This accepts Array as an argument of type String 
public static void Print_Array(String []array){
                 for(int i = 0;i<=array.length-1;i++){
System.out.println("All the values of an Array ==> " + array[i]);
}
}
}


Java - Decision making (IF Statement)

 1. Decision-making in java

  •   There are two types of decision-making statements in Java. One is very commonly used which is the If statement and you will find it almost in every piece of code. The second is the Switch statement.

 various forms of if...else statements in Java:-

  if statement

  if...else statement

  if...else if...else statement


if statement :

Example

package selenium;

public class Java_practise {

public static void main(String[] args) {

String sDay = "Sunday";

int iDay = 7;


if(sDay.equals("Sunday")){

System.out.println("Today is Sunday");

}


if(iDay==7){

System.out.println("Today is Sunday"); }

}

}


 if...else statement :


Example
package selenium;

public class Java_practise {

public static void main(String[] args) {
String sDay = "Saturday";
int iDay = 6;

if(sDay.equals("Sunday")){
System.out.println("Today is Sunday");
}else{
System.out.println("Today is not Sunday");
}

if(iDay==7){
System.out.println("Today is Sunday");
}else{
System.out.println("Today is not Sunday");
}
}
}


if...else if...else statement

Example
package selenium;

public class Java_practise {

public static void main(String[] args) {
String sDay = "Monday";
int iDay = 1;

if(sDay.equals("Sunday")){
System.out.println("Today is Sunday");
}else if(sDay.equals("Saturday")){
System.out.println("Today is not Saturday");
}else{
System.out.println("Today is a Weekday");
}

if(iDay==7){
System.out.println("Today is Sunday");
}else if(iDay==6){
System.out.println("Today is Saturday");
}else{
System.out.println("Today is a Weekday");
}
}
}



JAVA - Data Types and Variables

1. Data Type - int

  •   The integer data type is used to store numeric/numbers in the variable. 
  •    But a decimal number can not be stored in the 'int' data type.

Example :

 package selenium;

 public class Java_practise {

 public static void main(String[] args) {

                int mark;

mark = 445;

System.out.println("suriya total mark in 10 th " + mark);

 }

 }


2. Data Type - char

  •   The character data type is used to store just one character in the variable. It is very rarely used in Selenium. 

Example :

 package selenium;

 public class Java_practise {

 public static void main(String[] args) {

char a;

                 a = 'S';

        //Print the value of the char variable

      System.out.println("Value of char is : " +  a);

}

}


3. Data Type - double

  •   To declare a variable used to hold such a decimal number, you can use the double keyword.

Example :

package selenium;

public class Java_practise {

public static void main(String[] args) {

double PI;

    //Initialize the double variable with value 'P'

        PI = 3.14159;

  //Print the value of the double variable

        System.out.print("PI: " + PI);

}

}