selenium : how to handle error messages that get displayed when we dont give credentails - selenium

I am new to selenium, and when testing a web application I want to check when credentials are wrong the message in alert which gets displayed and the error message that is displayed on the page after accepting the alert are same or not please can anybody help me how can I do this
I have handled alert using
web.swichto.alert().accept
but I want to compare the message that alert displays and the error message that is displayed in that page like
Error: Invalid User ID/Password Or Network Is Down!
and message in alert box like
Invalid username/password
thanks in advance

you can store text present in the alert into a variable as follows,
String msg_in_alert=driver.switchTo().alert().getText();
then you can use assert to compare,
Assert.assertEquals(driver.findElement(By.cssSelector("selector_of_error_element")).getText().compareTo(msg_in_alert), true);
"true" specifies that the text should match..
If text does not match,it will result in step failure...

WebElement element = _driver.findElement(By.xpath("//span[#class='ng-binding error-class ng-hide']"));
element.getText();
if(element.equals("Recall and Volume data does not exist for calculation. Please upload the data.")){
System.out.println("true");
}else{
System.out.println("false");
}

WebElement mandmessActionName = driver.findElement(By.xpath("//*[#id='container']/div/form/div[1]/div/fieldset/div[1]/div/span[1]"));
String mandmessActionName1 = mandmessActionName.getText();
if (mandmessActionName1.equals("Action Name is required."))
{
System.out.println("true");
}
else
{
System.out.println("false");
}

Related

Capture the Alert Message text , which is displayed in a TextBox and in editable format

try{
WebDriverWait wait = new WebDriverWait(driver,8(;
if (wait.until(ExpectedConditions.alertIsPresent())== null)
{
Alert alert = driver.switchTo().alert();
String aamessage = alert.getText();
if(aamessage.equals("invoked")) /// this text is in input box in Alert pop up, I am able to edit this message in alert pop up.
{
alert.accept();
}
else
{
syso("no alerts");
}
}
catch(unHandledAlertException f)
{}
catch(NoAlertPresentException h)
{}
To get the text message which is in Input box, i tried this. BUt code not getting that message. Do we have any thing in specific in such cases.
Also in few functionalities, where the alert pop-ups are displayed, I handled it and used alert.accept(). But still the exception "unhandledalert" is displayed in the console. How to avoid this.
Check this :
Alert alert = driver.switchTo().alert();
WebElement element=driver.findElement(By.id("input id"));
System.out.println("Input text value:- " +element.getAttribute("value"));

Geb If Else or try catch handling

I am new to Geb spoke testing and I have a test that ui send message and receive text or email, I have an api call that grabs the sent email/text, I need to use that response and continue in the UI testing. but that response is expired since the api is fast and its getting the previous sent message which (before my ui send that email/text) and the last sent makes the previous expired. so my question is I need to call that api again if the message is expired, I tried if else and waitFor methods but couldn't come up with solution, I use page objects and test specs and I cannot create that as a method since that is happening on one page and in one def class and which I cannot even do waitFor in my test class which results IllegalMonitorStateException
when: "I enter text code"
String text = response.getText()
testPage.enterCode << text
testpage.pager.clickNext() // here is happening the code expiration and couldn't get to the next page,
then: "I am in next page"
at nextPage // if am not on the next page I need to make a new api call to get the newest response
I tried
when: "I enter text code"
String text = response.getText()
testPage.enterCode << text
testpage.pager.clickNext()
def erroHappened = testPage.errorAlert.isdisplayed
then: "I am in next page"
!errorHappened && testPage.enterCode << response.getText()
How can I use the wait or try catch or if else to solve this issue, Thank you in advance

Unable to get the alert text using selenium webdriver

I have the below code to verify the alert text and then dismiss. Verifying the alert text is failing.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alertText is returned as null. So the next step to assert the text is failing.
Next step:
driver.switchTo().alert().accept();
Accepts the alert without any issue.
Another weird scenario is when I debug in eclipse its working fine. But when I am running assertion throws error.
Another differecnce I found while running and debugging.
During running the alert come with this message don't let this page create more messages
Looks very weird.
Showing message as "don't let this page create more messages" is browser feature there to prevent sites from showing hundreds of alerts.
As while debugging your code it is working, it should work even while running too. If not working, definitely its sync issue. I hope adding some hardwait definitely help.
Other approach to get alert text from alert popup:
public String getJsAlertText()
{
Object txt = ((JavascriptExecutor)driver).executeScript("return
window.alert.myAlertText;");
return (String)txt;
}

Handle javascript alert in iframe using Selenium 2

I trying to practice handling Javascript alert. I want to accept alert ,which comes out of iframe in w3school website.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click();
WebElement editable = driver.switchTo().activeElement();
editable.sendKeys("enter");
//handle pop alert
String mainPage = driver.getWindowHandle();
Alert alt = driver.switchTo().alert();
alt.accept();
driver.switchTo().window(mainPage);
I am just getting alert box and then below error. I am not able to accept it.
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : I am an alert box!}
You have all the right code. The error comes when you try to do something other than handle the alert once the alert is up. You have some extraneous code that isn't needed to handle the alert. I've removed it in the code below. You can add things back but you will need to add them before or after launching the alert and handling it.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click(); // this launches the alert
driver.switchTo().alert().accept(); // this one line accepts the alert
driver.switchTo().defaultContent(); // you will probably need to switch back out of the IFRAME at some point to access the main page again so I added this line
One thing that I would strongly suggest for you is to read some guides on debugging in the IDE of your choice. If you learned how to debug your code, you could walk through it and more likely see where the issue was and fix it.

How to switch back to main window after handling alert in Selenium Web Driver

I have to write a selenium automation test script , where test has to create a template that fills up Generalize data in form and enter specific details manually(trying to wait here till manual entry is finished) and then click on SAVE button. However if some of mandatory field is left the system shows JavaScript's validation alert. I am handling this alert by using
Alert alert = driver.switchTo().alert();
alert.accept();
After this I want to get back to main page and wait for several minutes to write the description for missing fields and now click on SAVE button.
How can I achive this in Selenium web driver?
when you deal with alerts firstly you have to check whether alert is present. I would use this approach:
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
for more in click here, Also do not forget about debug step by step to get to know on what step alert appears/not appears. Hope this helps you.
You can go to the alert window from parent window using switchTo() method:
<html>
Alert alrt = driver.switchTo().alert(); // Switching the control to Alert window
alrt.accept(); // to accept Yes to delete the task
//alrt.dismiss();
// to dismiss the action - once you click on YES, of course you don't have option to dismiss
</html>
Now, answering to your question -
once you select any option from the Alert window (say - YES/NO), the control automatically comes back to the parent or main window. And you know what, the same follows in Hidden divs too.The switching back to parent window comes into picture when it is a question of HTML Pop ups.
Hope this helps.
After Accepting Alert box. we want to Switch to main window by geting the window handle details.
//Stores the handle details
String windowHandle=driver.getWindowHandles();
//Alert portion
Alert alert = driver.switchTo().alert();
alert.accept();
//After switching back to main window..
driver.navigate.windows(windowHandle);
//It takes to the main window....
You can give a try on the following code:
driver.navigate.back();