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.
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);
}