Annotations
Annotations
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriver driver;
@BeforeSuite
public void beforeSuiteMethod()
{
System.out.println("---Before Suite--");
}
@AfterSuite
public void afterSuiteMethod()
{
System.out.println("---After Suite--");
}
@BeforeTest
public void beforeTestMethod()
{
System.out.println("---Before Test---");
}
@AfterTest
public void afterTestMethod()
{
System.out.println("---After Test---");
}
@BeforeClass
public void beforeClassMethod()
{
System.out.println("---Before Class---");
}
@AfterClass
public void afterClassMethod()
{
System.out.println("---After Class---");
}
@BeforeMethod
public void setupBrowser() {
System.out.println("---Before Method---");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.tutorialsninja.com/demo/");
}
@AfterMethod
public void closeBrowser() {
System.out.println("---After Method---");
driver.quit();
}
@Test(priority = 1)
public void LoginWithValidCredentials() {
System.out.println("--Test 1--");
driver.findElement(By.xpath("//span[contains(text(),'My Account')]")).click();
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("input-email")).sendKeys("[email protected]");
driver.findElement(By.id("input-password")).sendKeys("Admin123");
driver.findElement(By.xpath("//input[@class='btn btn-primary']")).click();
@Test(priority = 2)
public void LoginWithInValidCrednetials() {
System.out.println("--Test 2--");
driver.findElement(By.xpath("//span[contains(text(),'My Account')]")).click();
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("input-email")).sendKeys("[email protected]");
driver.findElement(By.id("input-password")).sendKeys("Admin123a");
driver.findElement(By.xpath("//input[@class='btn btn-primary']")).click();
Assert.assertEquals(
driver.findElement(By.xpath("//div[@class='alert alert-danger alert-dismissible']")).getText(),
"Warning: Your account has exceeded allowed number of login attempts. Please try again in 1 hour.");
}
@Test(priority = 3)
public void LoginWithInvalidPassword() {
System.out.println("--Test 3--");
driver.findElement(By.xpath("//span[contains(text(),'My Account')]")).click();
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("input-email")).sendKeys("[email protected]");
driver.findElement(By.id("input-password")).sendKeys("Admin123a");
driver.findElement(By.xpath("//input[@class='btn btn-primary']")).click();
Assert.assertEquals(
driver.findElement(By.xpath("//div[@class='alert alert-danger alert-dismissible']")).getText(),
"Warning: No match for E-Mail Address and/or Password.");
}
@Test(priority = 4)
public void LoginWithInvalidUsername() {
System.out.println("--Test 4--");
driver.findElement(By.xpath("//span[contains(text(),'My Account')]")).click();
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("input-email")).sendKeys("[email protected]");
driver.findElement(By.id("input-password")).sendKeys("Admin123");
driver.findElement(By.xpath("//input[@class='btn btn-primary']")).click();
Assert.assertEquals(
driver.findElement(By.xpath("//div[@class='alert alert-danger alert-dismissible']")).getText(),
"Warning: Your account has exceeded allowed number of login attempts. Please try again in 1 hour.");
}
@Test(priority = 5)
public void LoginWithEmptyUsernamePassword() {
System.out.println("--Test 5--");
driver.findElement(By.xpath("//span[contains(text(),'My Account')]")).click();
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("input-email")).sendKeys("");
driver.findElement(By.id("input-password")).sendKeys("");
driver.findElement(By.xpath("//input[@class='btn btn-primary']")).click();
Assert.assertEquals(
driver.findElement(By.xpath("//div[@class='alert alert-danger alert-dismissible']")).getText(),
"Warning: No match for E-Mail Address and/or Password.");
}
}