How to capture Server Error Pages using selenium Web driver?

爷,独闯天下 提交于 2019-12-23 02:30:48

问题


I had an issue with handling the server error page using selenium web driver.While navigating from one page to the other the webpage got crashed and the code which I wrote for fetching the Web Element which were in place is not responding.

//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;
        }


    }

So how to efficiently track such Page error or Server error issues while automating with selenium webdriver


回答1:


Your scenario will fail when webelements on the expected page are not found. What I would do is add an after hook (Cucumber JUnit) that executes just before the failing scenario exits to capture a screen shot.

@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();

}


来源:https://stackoverflow.com/questions/35500586/how-to-capture-server-error-pages-using-selenium-web-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!