Reputation: 78
I have been able to run a selenium test case with headless Firefox, However when taking a screenshot, the screenshot is not that of the web-page(web-page tested in the testcase) rather, the screenshot is taken of the background (as in. the current window shown (e.g. eclipse IDE running the testcase))
Screenshot function
File screenShotFolder = new File("Screenshots");
WebDriver driver = getDriver();
try {
if (!screenShotFolder.exists() && !screenShotFolder.mkdir()) {
getLog().error(
"Cannot create a new file in the intended location. "
+ "" + screenShotFolder.getAbsolutePath());
}
File scrFile =
((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String filePath =
screenShotFolder.getAbsolutePath() + File.separator
+ imageName + ".png";
FileUtils.copyFile(scrFile, new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
is there any other "options" or "arguments" that need to be set?
Upvotes: 3
Views: 2850
Reputation: 1
In Django(Python), you can take the screenshots of Django Admin on headless Firefox as shown below. *I use pytest-django and selenium and my answer explains it more:
import pytest
from selenium import webdriver
def take_screenshot(driver, name):
os.makedirs(os.path.join("screenshot", os.path.dirname(name)), exist_ok=True)
driver.save_screenshot(os.path.join("screenshot", name))
def test_1(live_server):
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
driver = webdriver.Firefox(options=options)
driver.set_window_size(1024, 768)
driver.get(("%s%s" % (live_server.url, "/admin/")))
take_screenshot(driver, "admin/firefox.png")
assert "Log in | Django site admin" in driver.title
Upvotes: 0
Reputation: 897
Here is the way you can still use
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument( "--headless" )
# options.add_argument( "--screenshot test.jpg http://google.com/" )
driver = webdriver.Firefox( firefox_options=options )
driver.get('http://google.com/')
driver.save_screenshot('test.png')
print driver.title
print driver.current_url
driver.quit()
sys.exit()
Upvotes: 1
Reputation: 18572
Taking a screenshot with headless Firefox should work like for usual driver.
In the past I used the following approach:
public static String makeScreenshot() {
String fileName = System.currentTimeMillis() + "Test";
File screenshot = Driver.driver.get().getScreenshotAs(OutputType.FILE);
File outputFile = new File("LoggerScreenshots/" + fileName + ".png");
System.out.println(outputFile.getAbsolutePath());
try {
FileUtils.copyFile(screenshot, outputFile);
} catch (IOException e) {
e.printStackTrace();
}
return outputFile.getName();
}
And called it when test execution fails:
Upvotes: 1