java - Need assistance in handling Alerts - waiting for alert to be present & Unhandled Alert exception -
i have below alert. problem is returning error on wait.until(expectedconditions.alertispresent())
line unhandledalertexception
, though can see alert triggered on-screen. if remove unhandledalertexception
, error have unhandledalertexception
. can please provide solution this, , explanation understand happened? `
//alert alert getalert() { try{ alert alert = driver.switchto().alert(); string alertcheck = alert.gettext(); this.bcase = true; system.out.println(alertcheck); if(alertcheck.equals("define higher 0 quantity products")){ m.print("correct error message appeared! user cannot advance further!"); } else if(alertcheck.equals("invalid payment document number")){ m.print("correct error message appeared! user cannot advance further!"); }else if (alertcheck.equals("define rates products")){ m.print("correct error message appeared! user cannot advance further!"); } else if (alertcheck.equals("define delivery dates products")){ m.print("correct error message appeared! user cannot advance further!"); } else if(alertcheck.equals("not implemented yet - waiting delivery doc input")){ m.print("correct error message appeared! user cannot advance further!"); }else if (alertcheck.equals("you must upload invoice file!")){ m.print("correct error message appeared! user cannot advance further!"); } else if (alertcheck.equals("you must upload payment order file!")){ m.print("correct error message appeared! user cannot advance further!"); } else if (alertcheck.equals("invalid payment document date")){ m.print("correct error message appeared! user cannot advance further!"); } else if (alertcheck.equals("invalid payment document number")){ m.print("correct error message appeared! user cannot advance further!");} else if (alertcheck.equals("invalid payment document date")){ m.print("correct error message appeared! user cannot advance further!");} else if(driver.findelement(by.xpath("//*[@class='btn pull-left btn-default input-sm'][text()='open order request']")).isdisplayed()){ this.bcase = false; m.print("(fail!!!) user can advance unselected/wrongly completed fields, though error message present! (fail!!!)"); } else {m.print("(fail!!!) user cannot advance, there no pop-up message informing him of problem! (fail!!!)");} driver.switchto().alert().accept(); return alert; } catch (noalertpresentexception e){ m.print("(fail!!!) user can advance wrongly completed fields, without error popup inform them! (fail!!!)"); this.bcase = false; return null; } catch (unhandledalertexception f){ wait.until(expectedconditions.alertispresent()); alert alert = driver.switchto().alert(); string alertcheck = alert.gettext(); if (alertcheck.equals("you must upload invoice file! must upload payment order file!")){ m.print("correct error message appeared! user cannot advance further!"); } else if(driver.findelement(by.xpath("//*[@class='btn pull-left btn-default input-sm'][text()='open order request']")).isdisplayed()){ this.bcase = false; m.print("(fail!!!) user can advance unselected/wrongly completed fields, though error message present! (fail!!!)"); }driver.switchto().alert().accept();return null; } }
`
i think better approach handle alerts first wait alert appear, , handle it. can this, 2 methods:
public boolean checknativealert() { boolean exist = false; try { webdriverwait wait = new webdriverwait(webdriver, 3); if(!(wait.until(expectedconditions.alertispresent()) == null)) { exist = true; } } catch (exception ignore) {} return exist; } public string getnativetextalert(boolean close) { string text = null; try { alert alert = webdriver.switchto().alert(); text = alert.gettext(); if (close) { alert.accept(); } } catch (exception ignore) {} return text; }
handle alerts selenium bit tricky, point of view better use 2 other options:
- change alerts modal dialogs (at least in debug/test mode)
- use autoit handle alerts in selenium tests
Comments
Post a Comment