Thursday, April 21, 2022

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

Selenium - Facebook Create New Account

 package selenium;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Fb_Create_New_Account {
 public static void main(String[] args) throws InterruptedException {
      System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get( "https://en-gb.facebook.com/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
driver.findElement(By.linkText("Create New Account")).click();
driver.findElement(By.name("firstname")).sendKeys("Suriya");
driver.findElement(By.name("lastname")).sendKeys("Parithy");
driver.findElement(By.name("reg_email__")).sendKeys("8012345679");
        driver.findElement(By.name("reg_passwd__")).sendKeys("123098");
        
      //Static dropdown
        Select date = new Select(driver.findElement(By.id("day")));
        Thread.sleep(1000);
        date.selectByVisibleText("1");
        Select month = new Select(driver.findElement(By.id("month")));
        Thread.sleep(1000);
        month.selectByValue("4");
        Select year = new Select(driver.findElement(By.id("year")));
        Thread.sleep(1000);
        year.selectByValue("1996");
        
      //Radio button
        driver.findElement(By.xpath("//input[@value='2']")).click(); //for selecting male
        driver.findElement(By.name("websubmit")).click();
        driver.close();
 }
}

Friday, April 1, 2022

Selenium - Get the Actual Message in WebDriver

 Path - 1 :
  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.alert-danger p")));
      List<String> alertTextList = driver.findElements(By.cssSelector("div.alert-danger p"))
              .stream()
              .map(WebElement::getText)
              .toList();
      System.out.println("Application Status :" + alertTextList);

Path - 2 :
 WebElement actmessage = new WebDriverWait(driver, 15)
           .until(ExpectedConditions.elementToBeClickable(By.className("Toastify")));
 String act = actmessage.getText();
System.out.println("Application Status :" + act);

Path - 3 :
WebElement display =  driver.findElement(By.className("alert-content"));
String act = display.getText();
//To print the value
System.out.println("Element displayed is :" + act);