0
0
Selenium Javatesting~10 mins

Screenshot attachment on failure in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to take a screenshot on test failure.

Selenium Java
File screenshot = ((TakesScreenshot) driver).[1](OutputType.FILE);
Drag options to blanks, or click blank then click option'
AgetScreenshotAs
BcaptureScreenshot
CtakeScreenshot
DmakeScreenshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the TakesScreenshot interface.
Trying to call screenshot methods directly on the driver without casting.
2fill in blank
medium

Complete the code to save the screenshot file to a specific path.

Selenium Java
FileUtils.[1](screenshot, new File("screenshots/failure.png"));
Drag options to blanks, or click blank then click option'
AsaveFile
BcopyFile
CwriteFile
DmoveFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not exist in FileUtils.
Confusing copyFile with moveFile which deletes the source.
3fill in blank
hard

Fix the error in the code to attach screenshot on failure inside a TestNG listener.

Selenium Java
public void onTestFailure(ITestResult result) {
    WebDriver driver = (WebDriver) result.getTestContext().getAttribute("[1]");
    // rest of code
}
Drag options to blanks, or click blank then click option'
Adriver
Bwebdriver
CwebDriver
DdriverInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute names that cause null pointer exceptions.
Using camelCase or different casing than the stored attribute.
4fill in blank
hard

Fill both blanks to correctly implement screenshot capture and save in the listener.

Selenium Java
File screenshot = ((TakesScreenshot) driver).[1](OutputType.FILE);
FileUtils.[2](screenshot, new File("screenshots/error.png"));
Drag options to blanks, or click blank then click option'
AgetScreenshotAs
BcopyFile
CsaveScreenshot
DwriteFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods for screenshot capture or file saving.
Mixing up method names causing compilation errors.
5fill in blank
hard

Fill all three blanks to implement a TestNG listener method that captures and saves a screenshot on test failure.

Selenium Java
public void onTestFailure(ITestResult result) {
    WebDriver driver = (WebDriver) result.getTestContext().getAttribute("[1]");
    File screenshot = ((TakesScreenshot) driver).[2](OutputType.FILE);
    FileUtils.[3](screenshot, new File("screenshots/failure_" + System.currentTimeMillis() + ".png"));
}
Drag options to blanks, or click blank then click option'
Adriver
BgetScreenshotAs
CcopyFile
DsaveFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong attribute names causing null driver.
Using incorrect method names causing compilation errors.
Not saving the file properly leading to no screenshot saved.