无法在测试注释方法中传递驱动程序值
Couldn't pass the driver value in test annotated method
我有以下代码:
@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);
}
我遇到错误
Can inject only one of <ITestContext, XmlTest, Method, Object[], ITestResult>
into a @AfterMethod annotated takeSnapShot.
我无法传递包含从另一个 class 存储的值的驱动程序值。
帮我解决这个问题或使用不同的解决方案。
它不会按照您尝试的方式工作。 TestNG 在每个 @Test
注释方法之后自动调用 @AfterMethod()
。
您需要做的是访问@AfterMethod
中的驱动程序实例。将驱动程序实例存储在您启动它的上下文变量中,然后访问它。
参考以下代码:
@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());
}
这就是你之后的方法
@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);
}
我有以下代码:
@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);
}
我遇到错误
Can inject only one of
<ITestContext, XmlTest, Method, Object[], ITestResult>
into a @AfterMethod annotated takeSnapShot.
我无法传递包含从另一个 class 存储的值的驱动程序值。
帮我解决这个问题或使用不同的解决方案。
它不会按照您尝试的方式工作。 TestNG 在每个 @Test
注释方法之后自动调用 @AfterMethod()
。
您需要做的是访问@AfterMethod
中的驱动程序实例。将驱动程序实例存储在您启动它的上下文变量中,然后访问它。
参考以下代码:
@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());
}
这就是你之后的方法
@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);
}