Module 17: video 8 | Selenium Forum
M
Posted on 30/09/2016
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]

M
Replied on 01/10/2016

send a screen shot of the alert.


M
Replied on 02/10/2016

Below is screenshot of alert message

[attachment=0:mdoik9he]Alert_message.png[/attachment:mdoik9he]

Responsive image

M
Replied on 03/10/2016

try sending an escape key or return key.

driver.findelement(By.xpath("html/body").sendkeys(Keys.ESC)


M
Replied on 03/10/2016

Can you please let me know how this will solve in handling "Case E"

[attachment=0:35ctjidx]Module17-alert.png[/attachment:35ctjidx]

Responsive image

M
Replied on 03/10/2016

try it and let me know if this can dismiss the alert.


M
Replied on 03/10/2016

Issue is not related to dismissing "Alert"

Issue: In excel sheet there are 5 different scenarios.
1) Case A: Invalid username
2) Case B: Invalid password
3) Case C: Password strength
4) Case D: No username/password
5) Case E: Login with valid credentials.

All the first 4 cases (A,B,C,D) are executed as expected because there is alert window when click on sign in button. later able to switch to this alert and click on accept.

But for Case E, it is positive scenario where user is able to login to application (as valid credentials are given).

In this scenario, when click on "Sign in" button, below error message is displayed

org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)

I am not able to handle this scenario (as Case E is valid case where there wont be any Alert window).


M
Replied on 04/10/2016

contact me on skype. my id is qtpselenium1


M
Replied on 04/10/2016

Thanks Ashish for your help over skype. Able to fix this issue Responsive image