Try catch block is not executed

70 views
Skip to first unread message

garvitag...@gmail.com

unread,
Jun 28, 2017, 2:50:12 AM6/28/17
to Selenium Users
HI,

I am using selenium verison 3.4, testng and maven. Issue is: try catch block is not executed. No error comes, build is successful. Steps to reproduce :

1. There is user page, in which all users are inside <li> tag. I am clicking most recent user created in that run by using iTestContext and pulling value from other class.
2. Store list elelemt and use for loop to traverse through it and click if text()== username recently created.

Note: I commented code inside for loop and just printed one line. it worked fine and try catch block got executed. As i run steps to click element, scroll screen up and edit access, try ctach block is not executed. I am not able to understand whats wrong in this for loop code.

public class AccessToProjects extends Base {
    String path = "/src/test/java/ipd/administration/users/users.properties";
    String usercreated;

    public AccessToProjects() throws IllegalArgumentException, IOException {
        super();
    }

    @BeforeClass
    public void load() throws IOException {
        prop = new Properties();
        loadPropertyFile(path);
    }

    @Test
    public void project_access(ITestContext itc) {
        usercreated = (String) itc.getAttribute(CreateUser.user_name);
        List<WebElement> li = driver.findElements(By
                .xpath(".//*[@id='users-list']/ul/li/div/div/div[1]/span/a"));

        for (WebElement opt : li) {

            if (opt.getText().equalsIgnoreCase(usercreated)) {
                opt.click();
                click("EditProjectAccess");
                click("SelectAnalyst");
                JavascriptExecutor jse = (JavascriptExecutor) driver;
                jse.executeScript("window.scrollBy(0,-250)", "");
                click("submitAccess");
                jse.executeScript("window.scrollBy(0,-250)", "");
                return;

            }

        }

        /* verify analyst access is given to user */
        try {
            driver.findElement(By.xpath(prop.getProperty("verify_access")))
                    .isDisplayed();
            System.out.println("User is given analyst access.  ");

        } catch (Exception e) {
            // error
            System.out
                    .println("There is some issue, user is not given analyst access.");
        }

    }
}

Please help on this. 
Thanks !

⇜Krishnan Mahadevan⇝

unread,
Jun 28, 2017, 5:37:20 AM6/28/17
to Selenium Users
The if condition within your for loop has a "return" statement in it. Maybe that is what is causing Java to skip further exception and thus your code within your try..catch block is not getting executed ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/a60973ac-46ff-4c93-ac5c-9cde3f640405%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

garvitag...@gmail.com

unread,
Jun 28, 2017, 6:08:03 AM6/28/17
to Selenium Users
Hi,

I tried running commenting return;

for loop is executed but i got below error and build is failed :

project_access(ipd.administration.users.AccessToProjects)  Time elapsed: 1.569 s  <<< FAILURE!
org.openqa.selenium.StaleElementReferenceException: 
stale element reference: element is not attached to the page document
  (Session info: chrome=59.0.3071.115)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 26 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'EVS08AURA332', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\ipd3\AppData\Local\Temp\scoped_dir5500_15419}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: aee2be2aa7b667b77089b1466d68c8c1
at ipd.administration.users.AccessToProjects.project_access(AccessToProjects.java:40)
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

garvitag...@gmail.com

unread,
Jun 28, 2017, 7:14:34 AM6/28/17
to Selenium Users
Hi,

I used break instead of return. It worked for me. Try catch block is being executed now.

Thanks,
Garvita Girotra
Reply all
Reply to author
Forward
0 new messages