无法在Selenium webdriver中截取屏幕截图AssertionError:null

时间:2016-01-11 04:30:11

标签: java selenium webpage-screenshot

我在Selenium WebDriver中遇到以下错误

Exception in thread "main" java.lang.AssertionError: null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.fail(Assert.java:101)
at screenshot.selenium.CalculatorTest.googleCalculator(CalculatorTest.java:43)
at screenshot.selenium.Test.main(Test.java:8)

我是硒的新手,并学习它拍摄屏幕截图。这是我的代码:

public class CalculatorTest {

WebDriver driver = new FirefoxDriver();
@Test
//Tests google calculator

public void googleCalculator() throws IOException{
    try{
        //Set implicit wait of 10 seconds 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
        //Launch google 
        driver.get("http://www.google.co.in"); 
        //Write 2+2 in google textbox 
        WebElement googleTextBox = driver.findElement(By.id("gbqfq")); 
        googleTextBox.sendKeys("2+2");
        //Click on searchButton 
        WebElement searchButton = driver.findElement(By.id("gbqfb"));
        searchButton.click(); 
        //Get result from calculator
        WebElement calculatorTextBox = driver.findElement(By.id("cwos")); 
        String result = calculatorTextBox.getText(); 
        //Intentionaly checking for wrong calculation of 2+2=5 in order to take screenshot for faling test 
        Assert.assertEquals(result, "5");
        } catch(Exception e){ Assert.fail(); //To fail test in case of any element identification failure   } } - See more at: http://artoftesting.com/automationTesting/screenShotInSelenium.html#sthash.90egNmMu.dpuf
    }
}

以下是我的另一种方法:

public void takeScreenShotOnFaliure(ITestResult testResult)throws IOException
{
    if(testResult.getStatus() == ITestResult.FAILURE)
    {
        System.out.println(testResult.getStatus());
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile, new File("C:\\testScreenShot.jpg"));
    }
    }
}

请随时帮助我 谢谢

0 个答案:

没有答案