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);

}

}



Tuesday, April 12, 2022

Selenium Java - Shadow DOM, TestNG, Read properties file

 package selenium;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.sukgu.Shadow;
public class Login_Page_NG {
WebDriver driver;
@BeforeTest
public void beforetest() throws InterruptedException, IOException {
  System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get( "Paste the your URL");
}
//Test case - Both User name and Password are entered correctly.
  @Test(enabled=true,priority=0)
  public void user_and_password_correct() throws InterruptedException, IOException {
  Shadow shadow = new Shadow(driver);
  WebElement element = shadow.findElement("form");

  //File Read
  FileReader reader=new FileReader("datafile.properties");
  Properties p=new Properties();  
  p.load(reader);
  element.findElement(By.id("username")).sendKeys(p.getProperty("username"));
  element.findElement(By.id("password")).sendKeys(p.getProperty("password"));
  WebElement signin = element.findElement(By.cssSelector("[class=button][type=submit]"));
  signin.submit();
  Thread.sleep(3000);
  driver.findElement(By.linkText("LogOut")).click();
  System.out.println("User name and Password are entered correctly - Login Successful");
  Thread.sleep(5000);
 }

  //Test case - Both Username and Password Fields are blank.
  @Test(enabled=true,priority=1)
  public void user_and_password_blank() throws InterruptedException {
  Shadow shadow = new Shadow(driver);
  WebElement element = shadow.findElement("form");
  WebElement signin = element.findElement(By.cssSelector("[class=button][type=submit]"));
  signin.submit();
  Thread.sleep(3000);
  System.out.println("Username cannot be empty");
 }

  //Test case - The username field is filled and the Password field is blank.
  @Test(enabled=true,priority=2)
  public void userfilled_and_password_blank() throws InterruptedException, IOException {
  Shadow shadow = new Shadow(driver);
  WebElement element = shadow.findElement("form");
  FileReader reader=new FileReader("datafile.properties");
  Properties p=new Properties();  
  p.load(reader);
  element.findElement(By.id("username")).sendKeys(p.getProperty("username"));
  WebElement signin = element.findElement(By.cssSelector("[class=button][type=submit]"));
  signin.submit();
  System.out.println("Password cannot be empty");
  Thread.sleep(3000);
  }

  //Test case - Enter invalid user name & invalid password
  @Test(enabled=true,priority=3)
  public void invalid_username_and_password() {
  Shadow shadow = new Shadow(driver);
  WebElement element = shadow.findElement("form");
  WebElement username = element.findElement(By.id("username"));
           username.click();
  username.sendKeys("oracle");
  WebElement password = element.findElement(By.id("password"));
  password.click();
  password.sendKeys("qwerty");
 WebElement signin = element.findElement(By.cssSelector("[class=button][type=submit]"));
  signin.submit();
 System.out.println("User does not exist.");
 }
}

Saturday, April 2, 2022

Java Program - Data types and variables PART-5

 package selenium;
public class Datatypesandvariable {
   public static void main(String[] args)  {

//Data types and variables
int rollno, tamil, english, maths, science, social, total;
String sname;
float avg;

rollno=1;
sname="suriya";
tamil=65;
english=80;
maths=70;
science=90;
social=85;

total=tamil+english+maths+science+social;
avg=total/5;

System.out.println("Roll Number : " + rollno);
System.out.println("Student Name : " + sname);
System.out.println("Tamil : " + tamil);
System.out.println("English : " + english);
System.out.println("Maths : " + maths);
System.out.println("Science : " + science);
System.out.println("Social : " + social);
System.out.println("Total : " + total);
System.out.println("Average : " + avg);
    }
}