1
\$\begingroup\$

I'm writing a test to validate that while clicking through a series of menu items, an error message pops up, and if it does, then I should throw an exception.

Now I wonder: Is it appropriate to use nested try-catch block here?

public static void main(String[] args) throws Exception{
        File file = new File("C:/Program Files/Selenium/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
        driver.get("http://ap-apbase-d002/Ecx.Web/Account/Login/?ReturnUrl=%2fEcx.Web%2f");
        WebElement element;
        element = driver.findElement(By.id("username"));
        element.sendKeys("Jh");
        element = driver.findElement(By.id("password"));
        element.sendKeys("1");
        element.submit();
        WebElement homeFrame = (new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfElementLocated(By.id("pnCenter_IFrame")));
        driver.switchTo().frame("pnCenter_IFrame");
        WebElement myHomeLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Projects")));
        driver.findElement(By.linkText("My Projects")).click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("pnCenter_IFrame");
        driver.switchTo().defaultContent();
        //parent try block
        try{
        WebElement UniversalInboxLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("Universal Inbox")));
        driver.findElement(By.linkText("Universal Inbox")).click();
        WebElement MyProjectsLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Projects")));
        driver.findElement(By.linkText("My Projects")).click();
        WebElement AllProjectsLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("All Projects")));
        driver.findElement(By.linkText("All Projects")).click();
        WebElement MyTeamLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Team")));
        driver.findElement(By.linkText("My Team")).click();
        WebElement WOTTaxFormLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("WOT Tax Form")));
        driver.findElement(By.linkText("WOT Tax Form")).click();
        WebElement MyDraftsLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Drafts")));
        driver.findElement(By.linkText("My Drafts")).click();
        WebElement MyArchivesLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Archives")));
        driver.findElement(By.linkText("My Archives")).click();
        WebElement RetrieveLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("Retrieve")));
        driver.findElement(By.linkText("Retrieve")).click();
            //Child try block
            try{
        List <WebElement> AppbaseErrorWindow = driver.findElements(By.cssSelector(".x-window-header"));
        for (WebElement item : AppbaseErrorWindow) {
            if (item.getText().contains("Request Failure")) {
                //Do Nothing
            }
        }
            }catch (Exception e){
        System.out.println("Exception is found here: " + e);

            }
            System.exit(0);
            driver.quit();
    }
        catch(Exception e){
        System.out.println("All is OK.");
        }
        System.out.println("Normal behavior");
 }
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to Code Review! To make life easier for reviewers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question. Please also tell us about what makes you post here. Is there any specific feedback that you want? \$\endgroup\$ Oct 17, 2014 at 22:15
  • \$\begingroup\$ Thanks for editing your question, that helped a little bit. I still don't find your question to be good, but I consider it good enough to be on topic now at least. \$\endgroup\$ Oct 17, 2014 at 23:01
  • \$\begingroup\$ @SimonAndréForsberg: You can also edit the title accordingly (purpose of the code). \$\endgroup\$
    – Jamal
    Oct 17, 2014 at 23:04

1 Answer 1

3
\$\begingroup\$

The code that you submitted is not complete as it was missing the driver variable (Had to look up the type as I have not used Selenium).

private WebDriver driver;

First off move everything out of the main method even if you are going to only use one instance, whilst doing this you should also separate the code into single responsibility methods. This will improve the readability of the code and give the program a nicer "flow".

Methods you come to mind are:

private void init();
private void login(String username, String password);
private void clickElement(String element) throws Exception; 
private void processWebElements(String header);
private void terminate();

init() will be:

private void init() {
    // The driver you want to use
    driver = new FirefoxDriver();
    File file = new File("C:/Program Files/Selenium/IEDriverServer.exe");
    System.setProperty("weObdriver.ie.driver", file.getAbsolutePath());
    driver.get("http://ap-apbase-d002/Ecx.Web/Account/Login/?ReturnUrl=%2fEcx.Web%2f");
}

login() will be:

private void login(String username, String password) {
    WebElement element;
    element = driver.findElement(By.id("username"));
    element.sendKeys(username);
    element = driver.findElement(By.id("password"));
    element.sendKeys(password);
    element.submit();

    WebElement homeFrame = (new WebDriverWait(driver, 30)).until(ExpectedConditions.presenceOfElementLocated(By.id("pnCenter_IFrame")));
    driver.switchTo().frame("pnCenter_IFrame");
    WebElement myHomeLink = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("My Projects")));
    driver.findElement(By.linkText("My Projects")).click();
    driver.switchTo().defaultContent();
    driver.switchTo().frame("pnCenter_IFrame");
    driver.switchTo().defaultContent();
}

clickelement() will be:

private void clickElement(String element, int wait) throws Exception {
    WebElement universalInboxLink = (new WebDriverWait(driver, wait)).until(ExpectedConditions.presenceOfElementLocated(By.linkText(element)));
    driver.findElement(By.linkText(element)).click();
}

processWebElements() will be:

private void processWebElements(String header) {
    List<WebElement> AppbaseErrorWindow = driver.findElements(By.cssSelector(header));
    for (WebElement item : AppbaseErrorWindow) {
        if (item.getText().contains("Request Failure")) {
            //Do Nothing
        }
    }
}

finally terminate() will be:

private void terminate() {
    System.exit(0);
    driver.quit();
}

This way you can easily change a bit of the program without rewriting for every time you wanted to call do something. Your try-catch can also be unested as the try inside will only start when everything inside the outside try block is finished. Using the above functions my constructor would be as follows:

public WebAuto() {
    init();
    login("Jh", "1");

    try {
        clickElement("Universal Inbox", 10);
        clickElement("My Projects", 10);
        clickElement("All Projects", 10);
        clickElement("My Team", 10);
        clickElement("WOT Tax Form", 10);
        clickElement("My Drafts", 10);
        clickElement("My Archives", 10);
        clickElement("Retrieve", 10);
        terminate();
    } catch (Exception e) {
        System.out.println("All is OK.");
    }

    try {
        processWebElements(".x-window-header");
    } catch (Exception e) {
        System.out.println("Exception is found here: " + e);
    }
    System.out.println("Normal behavior");
}

Extra: You should set up constants for strings such as file locations.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.