Всем привет!
Друзья тренируюсь в написание авто тестов на selenium+java+testng, возникла необходимость делать скриншоты на ошибках. Реализовал стандартными средствами Selenium WebDriver
Внимание вопрос:
Как же сделать у каждого скриншота уникальное имя? Желательно дата и время а там как пойдёт. Часть кода где на данный момент делаю скриншот (пока реализовал в позитивной части теста)
package Topiframe.AppManager; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.Assert; import org.testng.annotations.Test; import java.io.File; import java.text.DateFormat; import java.time.Duration; public class NavigationHelper { private void commission() { if (isElementPresent(By.cssSelector(".SmartInput_textFail__37qzX"))){ LOG.error("Введённая сумма не входит в допустимый диапазон"); Assert.fail(); } LOG.info("Получаем стоимость услуги с комиссией"); WebDriverWait wait = new WebDriverWait(wd, Duration.ofSeconds(10)); try { wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".Service_inputsRow__1zBtT:nth-child(1) > .Form_value__cMLhf"))); WebElement comm = wd.findElement(By.cssSelector(".Service_inputsRow__1zBtT:nth-child(1) > .Form_value__cMLhf")); String commission = comm.getAttribute("innerText"); LOG.info("TL_COMMISSION = " + commission); WebElement tlAmount = wd.findElement(By.cssSelector(".Service_inputsRow__1zBtT:nth-child(2) > .Form_value__cMLhf")); String amount = tlAmount.getAttribute("innerText"); LOG.info("TL_AMOUNT = " + amount); //Делаем скрин takeSnapShot(wd, "F:\\GitHub\\MyTests\\TopiframePayTest\\Screenshots\\date.png") ; }catch (Exception e) { LOG.error("Блок комиссий не доступен"); Assert.fail(); } } 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); } }