element - Selenium - Unable to get Exception/Failure if the WebElement is not available on Page -


i know question has been asked many times on forum. however, posting again after trying out solutions found on site & other resources. atlast need post on forum help.

my product web content filter allow user browse internet or filter (based on permissions has).

so test case user can view or cannot view page based on permissions granted him.

valid test case if user has permission can view page, works fine selenium able detect element & move forward. had used assertion in case around try catch block(after tried lot of other stuff well)

invalid test case if user doesnt have permission cannot view page. whenever tries go blacklisted site gets page being blocked(this page different page). know element selenium trying find not there in case. during execution script gets stuck & doesn't catch exception & move forward. tried debugging & on there stops when line search element comes. expecting script move forward after catching exception. see test case failed well.

one of friend suggested check developer whether element present in dom. not sure difference make.(any pointers on me learn)

below given stuff tried in code

snippet 1

public static void main(string[] args){ -------some code--- system.out.println("user logs in"); d1.get("http://www.maxim.com"); system.out.println("site blocked"); assert.asserttrue(doeselementexist(by.id("block-system-user-menu"))); d1.quit(); } public static boolean doeselementexist(by locator) { if (d1.findelements(locator).size() > 0) { return true; } else { return false; } 

snippet 2

d1.get("http://www.maxim.com"); mylocator = by.id("block-system-user-menu"); if (iselementpresent(mylocator)) { webelement myelement = d1.findelement(mylocator); myelement.gettext();      }   public static boolean iselementpresent(by locator) { return d1.findelements(locator).size() > 0; } 

snippet 3

private static boolean verifyelementabsent() throws exception { try { d1.findelement(by.xpath("//*[@id='block-system-user-menu']/div/div/ul/li/a")); system.out.println("element present"); return false; } catch (nosuchelementexception e) { system.out.println("element absent"); return true; } 

snippet 4

d1.get("http://www.maxim.com"); string s1 = "//*[@id='block-system-user-menu']/div/div/ul/li/a"; verifyelementabsent(s1); d1.quit(); } private static boolean verifyelementabsent(string locator) throws exception { boolean visible = d1.findelement(by.xpath(locator)).isdisplayed(); boolean result = !visible; system.out.println(result); return result; 

snippet 5

boolean titletextfield = d1.findelement(by.id("block-system-user-menu")).isdisplayed(); assert.assertfalse(titletextfield, "title text field present not expected"); 

snippet 6

webdriverwait wait1 = new webdriverwait(d1,    20);webelementelement2=wait1.until(expectedconditions.visibilityofelementlocated(by.id("block-system-user-menu"))); 

snippet 7

iselementpresent(by.id("block-system-user-menu")); public static boolean iselementpresent(by by){ try{ d1.findelement(by); return true; }catch(nosuchelementexception e){ return false; } } 

snippet 8

webdriverwait wait = new webdriverwait(driver, 60); wait.until(presenceofelementlocated(locator)); 

snippet 9

boolean ispresent = driver.findelements(by.yourlocator).size() > 0 

any appreciated. thanks, amar


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -