如何使用selenium Web驱动程序捕获服务器错误页面?

时间:2016-02-19 08:26:11

标签: java selenium selenium-webdriver automation

我在使用selenium web driver处理服务器错误页面时遇到了问题。虽然从一个页面导航到另一个页面,但网页崩溃了,我为获取Web元素而编写的代码没有响应。< / p>

//Function Call by passing the arguments 

commFunction.ElementTitleValidation(driver,props,"CustomerPricing_SubMenu_Title");


  //Method for ElementTitleValidation

     public void ElementTitleValidation(WebDriver driver, Properties props,String MenuTitle) 
     {  
        boolean ElementTitle;
        ElementTitle=ValidationHelper.isElementPresent(driver, By.xpath(LocatorModule(props,MenuTitle)));
        System.out.println("The Visibility of Element title  is "+MenuTitle+" and it is matching"); 
        if(ElementTitle==false)
        {
            Assert.fail("ElementTitle Not Found !!!!");
            System.out.println("ElementTitle Not Found !!!!");
        }


  //Method for Checking the WebElement Present or Not

    public static boolean isElementPresent(WebDriver driver , By by){
      WebElement ElementPresent=driver.findElement(by);
        if( ElementPresent != null)
        {
            return true;

        }
        else{
            return false;
        }


    }

那么如何在使用selenium webdriver自动化时有效地跟踪此类页面错误或服务器错误问题

1 个答案:

答案 0 :(得分:0)

当找不到预期页面上的webelements时,您的方案将失败。我要做的是添加一个挂钩(Cucumber JUnit),它在失败场景退出之前执行以捕获屏幕截图。

@After
/**
 * Embed a screenshot in test report if test is marked as failed
 */
public void embedScreenshot(Scenario scenario) {

    if(scenario.isFailed()) {
    try {
         scenario.write("Current Page URL is " + driver.getCurrentUrl());
         //            byte[] screenshot = getScreenshotAs(OutputType.BYTES);
        byte[] screenshot =  (TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png");
    } catch (WebDriverException somePlatformsDontSupportScreenshots) {
        System.err.println(somePlatformsDontSupportScreenshots.getMessage());
    }

    }
    driver.quit();

}