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

No comments:

Post a Comment