在这种情况下处理异常的好方法(Selenium WebDriver-Using Java)

时间:2014-10-17 21:57:51

标签: java selenium-webdriver

我在使用Selenium Webdriver(使用Java)编写异常处理测试时遇到了一些问题。我正在尝试执行以下操作:逐个单击一系列菜单项,如果弹出任何类型的错误消息(窗口),那么我需要抛出异常以使测试失败。但是,在整个应用程序中,同一个类(例如,x-window-header),我不得不使用for语句来查找特定的标题文本。

下面是我到目前为止的一个示例(我正在尝试使用嵌套的try-catch块来执行此操作。)有没有更好的方法来实现它?

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, this blocks is used to click through all menu items
    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, this block is used to look for this element in each frame, not sure how to implement correctly.
        try{
            List <WebElement> AppbaseErrorWindow = driver.findElements(By.cssSelector(".x-window-header"));
            for (WebElement item : AppbaseErrorWindow) {
                if (item.getText().contains("Request Failure")) {
                    //Do Nothing, not sure what to do here
                }
            }
        }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");
 }

我被告知在进行测试时使用以下格式:

main
    test suite
    try{
        test1();
        test2();
    }catch(e){
        //show the error
        exit
    }
}

0 个答案:

没有答案