1

I have below code:

@AfterMethod()
public static void takeSnapShot(WebDriver webdriver, String fileWithPath) throws Exception {
    // Convert web driver object to TakeScreenshot
    TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
    // Call getScreenshotAs method to create image file
    File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
    // Move image file to new destination
    File DestFile = new File(fileWithPath);
    // Copy file at destination
    FileUtils.copyFile(SrcFile, DestFile);
}

I'm getting below error

Can inject only one of <ITestContext, XmlTest, Method, Object[], ITestResult> into a @AfterMethod annotated takeSnapShot.

I couldn't pass the driver value which contains the value stored from another class.

Help me to solve this or with different solution.

2
  • Can you post the complete error message? which line of code is triggering this message? Commented Mar 13, 2020 at 12:05
  • 1
    Please do not post duplicate questions: stackoverflow.com/q/60666543/3092298 Commented Mar 13, 2020 at 12:19

1 Answer 1

1

It won't work in the way you are trying to do. TestNG automatically calls @AfterMethod() after each @Test annotated method.

What you need to do is to access driver instance in @AfterMethod. Store the driver instance in context variable from where you are initiating it and then access it.

Refer below code:

@BeforeMethod()
public static void setup(ITestContext context) throws Exception {
    System.setProperty("webdriver.chrome.driver",
            "/Users/narendra.rajput/bulkpowders/bulk-powders/resources/drivers/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.wego.com.my/hotels");
    context.setAttribute("driver", driver);
}

@Test
public void test() {
    System.out.print("ok");
}

@AfterMethod()
public static void screenShot(ITestContext context) {

    WebDriver driver = (WebDriver) context.getAttribute("driver");
    System.out.print(driver.getCurrentUrl());
}

This is how you after method will be

@AfterMethod()
public static void screenShot(ITestContext context) {

    final String fileWithPath = "file_path";
    WebDriver driver = (WebDriver) context.getAttribute("driver");
    TakesScreenshot scrShot = ((TakesScreenshot) driver);
    File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
    File DestFile = new File(fileWithPath);
    FileUtils.copyFile(SrcFile, DestFile);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.