I have verified these 5 testcases using the below script :
1.Enter correct username(cuname) and wrong password(wpwd)
2.Enter wrong username(wuname) and correct password(cpwd)
3.Test with empty username and correct password
4.Test with correct username and empty password
5.Enter correct username(cuname) and correct password(cpwd)
Prerequisite:
Add jxl jar into project
Install TestNG
Add WebDriver package
package WDTNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;
import jxl.Workbook;
import jxl.Sheet;
public class LoginDD {
WebDriver d;
@BeforeMethod
public void BM()
{
d = new FirefoxDriver();
d.get("http://gmail.com");
}
@Test
public void T()
{
try{
d.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Sheet s;
FileInputStream fi = new FileInputStream("C:\\Users\\Kalyan\\Desktop\\S2TNG\\LoginPage.xls");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(1);
for(int row=1; row <=s.getRows();row++)
{
String username = s.getCell(0, row).getContents();
String password= s.getCell(1, row).getContents();
d.findElement(By.xpath(".//*[@id='Email']")).clear();
d.findElement(By.xpath(".//*[@id='Email']")).sendKeys(username);
d.findElement(By.xpath(".//*[@id='Passwd']")).clear();
d.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys(password);
d.findElement(By.xpath(".//*[@id='signIn']")).click();
try
{
if(waitforelement(By.xpath(".//*[@id='errormsg_0_Email']")))
{
WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Email']"));
String strng = element.getText();
System.out.println(strng);
}
else if(waitforelement(By.xpath(".//*[@id='errormsg_0_Passwd']")))
{
WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Passwd']"));
String strng = element.getText();
System.out.println(strng);
//Assert.assertEquals("Google Search", strng);
}
else if(waitforelement(By.xpath(".//*[@id='errormsg_0_Passwd']")))
{
WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Passwd']"));
String strng = element.getText();
System.out.println(strng);
}
else if(waitforelement(By.xpath(".//*[@id=':b7']/div/div")))
{
WebElement element = d.findElement(By.xpath(".//*[@id=':b7']/div/div"));
String strng = element.getText();
System.out.println(strng);
}
else
{
System.out.println("Script has failed");
d.quit();
}
}
catch(Exception e)
{
//e.printStackTrace();
//throw e;
}
}
}
catch (Exception e)
{
}
}
public boolean waitforelement(By name)
{
try
{
if(d.findElement(name).isDisplayed())
{
return true;
}
}
catch(Exception e)
{
return false;
}
return false;
}
@AfterMethod
public void AM()
{
d.quit();
}
}
See this below image and create an excel sheet with the following data
Note:Save the excel sheet as fully compatible with Excel 97-2003
1.Enter correct username(cuname) and wrong password(wpwd)
2.Enter wrong username(wuname) and correct password(cpwd)
3.Test with empty username and correct password
4.Test with correct username and empty password
5.Enter correct username(cuname) and correct password(cpwd)
Prerequisite:
Add jxl jar into project
Install TestNG
Add WebDriver package
package WDTNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;
import jxl.Workbook;
import jxl.Sheet;
public class LoginDD {
WebDriver d;
@BeforeMethod
public void BM()
{
d = new FirefoxDriver();
d.get("http://gmail.com");
}
@Test
public void T()
{
try{
d.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Sheet s;
FileInputStream fi = new FileInputStream("C:\\Users\\Kalyan\\Desktop\\S2TNG\\LoginPage.xls");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(1);
for(int row=1; row <=s.getRows();row++)
{
String username = s.getCell(0, row).getContents();
String password= s.getCell(1, row).getContents();
d.findElement(By.xpath(".//*[@id='Email']")).clear();
d.findElement(By.xpath(".//*[@id='Email']")).sendKeys(username);
d.findElement(By.xpath(".//*[@id='Passwd']")).clear();
d.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys(password);
d.findElement(By.xpath(".//*[@id='signIn']")).click();
try
{
if(waitforelement(By.xpath(".//*[@id='errormsg_0_Email']")))
{
WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Email']"));
String strng = element.getText();
System.out.println(strng);
}
else if(waitforelement(By.xpath(".//*[@id='errormsg_0_Passwd']")))
{
WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Passwd']"));
String strng = element.getText();
System.out.println(strng);
//Assert.assertEquals("Google Search", strng);
}
else if(waitforelement(By.xpath(".//*[@id='errormsg_0_Passwd']")))
{
WebElement element = d.findElement(By.xpath(".//*[@id='errormsg_0_Passwd']"));
String strng = element.getText();
System.out.println(strng);
}
else if(waitforelement(By.xpath(".//*[@id=':b7']/div/div")))
{
WebElement element = d.findElement(By.xpath(".//*[@id=':b7']/div/div"));
String strng = element.getText();
System.out.println(strng);
}
else
{
System.out.println("Script has failed");
d.quit();
}
}
catch(Exception e)
{
//e.printStackTrace();
//throw e;
}
}
}
catch (Exception e)
{
}
}
public boolean waitforelement(By name)
{
try
{
if(d.findElement(name).isDisplayed())
{
return true;
}
}
catch(Exception e)
{
return false;
}
return false;
}
@AfterMethod
public void AM()
{
d.quit();
}
}
See this below image and create an excel sheet with the following data
Note:Save the excel sheet as fully compatible with Excel 97-2003
No comments:
Post a Comment