Iam working on Module 17--video 8(related to login to application with username/password from excel and validating messages)
In our internal application, if username & password doesnt match/if any of them is not entered, then validation is displayed in "Alert" window.
By using "d.switch.to.alert", iam able to click on alert window and validated all messages in different scenarios.
But in last scenario (case E in below code), tried to login with valid credentials. As both login details are valid, able to login to application without any issue. but iam not able to compare text that is displayed after succesful login (Hello xxxx) with another string (string V).
Reason: Getting error message as "org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)"
can you please go through "Case E" and let me know how to handle this scenario??
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
public class Login_scenarios {
WebDriver d;
String parta = "The following errors have occurred:";
String partb = " Password should be at least 6 characters";
String errmsg1 = parta + partb;
String errmsg2 = "The following errors have occurred: Please enter user name";
String errmsg3 = "The following errors have occurred: Password must be entered";
String errmsg4 = "The following errors have occurred: Please enter user name Password must be entered";
String v = " Hello Shanmukh!";
@BeforeTest
public void driver() {
d = new FirefoxDriver();
}
@Test(dataProvider = "getdata")
public void testlogin(String username, String password, String expectedresult) throws InterruptedException {
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.navigate().to("http://stage.crosstrio.com/");
d.findElement(By.xpath("//*[@id='ctl00_cnpPlaceHolder_loginPB_UserName']")).sendKeys(username);
Thread.sleep(2000);
d.findElement(By.xpath("//*[@id='ctl00_cnpPlaceHolder_loginPB_Password']")).sendKeys(password);
Thread.sleep(2000);
d.findElement(By.xpath("//*[@id='ctl00_cnpPlaceHolder_loginPB_Login']")).click();
Thread.sleep(2000);
d.switchTo().alert();
Alert a = d.switchTo().alert();
String Errormessage = a.getText();
System.out.println(Errormessage.trim());
String finalmessage = Errormessage.replaceAll("\\s+", " ");
a.accept();
d.switchTo().defaultContent();
// validations
if (expectedresult.equals("CaseA"))
Assert.assertEquals(finalmessage, errmsg1);
else if (expectedresult.equals("CaseB"))
Assert.assertEquals(finalmessage, errmsg2);
else if (expectedresult.equals("CaseC"))
Assert.assertEquals(finalmessage, errmsg3);
else if (expectedresult.equals("CaseD"))
Assert.assertEquals(finalmessage, errmsg4);
else if (expectedresult.equals("CaseE"))
Assert.assertEquals(v,d.findElement(By.xpath("//*[@id='ctl00_TopHeader1_TopLogo1_lbluserName']")).getText());
}
@DataProvider
public Object[][] getdata() throws IOException {
File f = new File("D:\\Login data.xlsx");
FileInputStream fis = new FileInputStream(f);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet("Logincredentials");
// To get number of rows
int rows = sheet.getPhysicalNumberOfRows();
System.out.println(rows);
// To get number of columns
int columns = sheet.getRow(0).getLastCellNum();
System.out.println(columns);
Object a[][] = new Object[rows - 1][columns];
for (int rc = 1; rc < rows; rc++) {
for (int cc = 0; cc < columns; cc++) {
System.out.print(sheet.getRow(rc).getCell(cc) + " ");
if (sheet.getRow(rc).getCell(cc) != null) {
a[rc - 1][cc] = sheet.getRow(rc).getCell(cc).getStringCellValue();
} else {
a[rc - 1][cc] = "";
}
}
System.out.println();
}
return a;
}
}
[attachment=0:mbbwh333]No Alert.png[/attachment:mbbwh333]