maven - Selenium Java handle object(alert dialog) exception without delaying. (unpredictable Java pop-up) -
goal: alert pop up. whether it's shown or not, want continue. if show, have select check box, , hit continue. if not , ignore.
blocker: if alert shows, handle action , dialog closed. when it's not, selenium hangs there without handling condition when it's not shown.
back ground: use uft before, maybe logic wrong. pop application alert(not systems), assumed "switch to(),accept()/dismiss() won't work. , add handle alert right after log in, , within login method below.
selenium framework background. : use selenium maven framework, serenity bdd. object set @ beginning of page. , serenity.properties handle time out default , on.
pop objects (if appears):
@findby(xpath = "//input[@id='notification-ack']") private webelement pconoticechbx; //this check box, needs checked @findby(xpath = "//button[contains(.,'continue')]") private webelement pconoticecontinuebtn;//button close alert *log in method *
public void loginintoextapplication(string baseurl, string loginurl, string uid, string pwd, string extappenv)throws exception { gotoextloginpage(baseurl, loginurl); enterlogincredential(uid, pwd); openat("https://" + extappenv + ".programportaltest.hrsa.gov/sdms- extranet/index.xhtml"); my approaches:
//1.
if (pconoticechbx!=null) { pconoticechbx.click(); pconoticecontinuebtn.click(); } else { system.out.println("dialog didn't display, no need action"); } //2. hanged here after log in actions.
if(!getdriver().findelements(by.xpath("//*[@id='submit']")).isempty()){ pconoticechbx.click(); pconoticecontinuebtn.click(); } else { system.out.println("dialog didn't display, no need action"); } //3. added watch doesnt work, shows pending , below code failed too. ran maven in junit debug mode. used work fine. watch elements show (pending)..
boolean ispresent = getdriver().findelements(by.id("noticebox")).size() >0 system.out.println("the diaolog exist= " + ispresent); //4. tried try catch method.
try{ pconoticechbx.click(); pconoticecontinuebtn.click(); }catch (exception e){ // printing logs report log.error("report category button element not found."); // after doing work, want stop test case throw(e); } return; } //5. tried list webelemets:
list temp = webdriver.findelements(org.openqa.selenium.by.id("noticebox"));
if (temp.count > 0 && temp[0].displayed) { // script execute if element found } else { // continue test } //6. , below
if (!webdriver.findelements(by.xpath("//*[@id='submit']")).isempty()==true); { //handle dialog } else{ //continue } // 7.tried boolean value, hang on here first steps
boolean nbox = pconoticechbx.isdisplayed(); { if (nbox==false) { system.out.println("dialog didn't display, no need action"); } else if (nbox==true) { pconoticechbx.click() ; pconoticecontinuebtn.click(); }
if popups i've dealt work this:
- customer comes site , site checks existence of cookie. if cookie exists, popup never launched (ideal state). if cookie not exist (typical state), after specified period of time popup appears.
- customer dismisses popup , cookie created expiration time
- once expiration time passes, cookie expires , popup fire again
you need investigation , find cookie created once popup dismissed. clear cache, browse site, , note cookies exist. wait popup , dismiss it. find cookie created , examine it. cookie need create. there lot of tutorials on creating cookies. it's pretty straightforward.
once learn how create cookie, add script described below:
- navigate page on domain know doesn't exist, e.g. www.domain.com/some404page. because won't trigger popup countdown , need on domain create cookie.
- create cookie
- do normal test
no more popups.
Comments
Post a Comment