-1
私はウェブページのスクリーンショットを取るコードを試しています。私はクラスを使用してランダムな文字列値を生成し、次にこれらの値をファイル名に割り当てました。しかし、コードを実行すると完全に実行されますが、スクリーンショットは決してディレクトリに保存されません。スクリーンショットはSeleniumのディレクトリに保存されません
コード:私は、このランダムな文字列ジェネレータを削除し、直接のfileutilsにディレクトリ・パスを指定するしかし
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenShot {
private WebDriver driver;
private String BaseUrl;
@Before
public void setUp() throws Exception {
BaseUrl = "https://www.flock.co";
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(BaseUrl);
Thread.sleep(5000);
WebElement emailField = driver.findElement(By.xpath("//div[@id='main-area']//input"));
WebElement Button = driver.findElement(By.xpath("//div[@id='main-area']//button"));
emailField.sendKeys("incomeplete");
Button.click();
}
public static String getRandomString(int length) {
StringBuilder sb = new StringBuilder();
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for (int i = 0; i < length; i++) {
int index = (int) (Math.random() * characters.length());
sb.append(characters.charAt(index));
}
return sb.toString();
}
@After
public void tearDown() throws Exception {
String fileName = getRandomString(10) + ".png";
String directory = "C:\\Users\\farzan.s.DIRECTI\\Desktop\\LetsKodeIt";
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(directory+fileName));
driver.quit();
}
}
は、それが動作します。 コード:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenShot {
private WebDriver driver;
private String BaseUrl;
@Before
public void setUp() throws Exception {
BaseUrl = "https://www.flock.co";
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(BaseUrl);
Thread.sleep(5000);
WebElement emailField = driver.findElement(By.xpath("//div[@id='main-area']//input"));
WebElement Button = driver.findElement(By.xpath("//div[@id='main-area']//button"));
emailField.sendKeys("incomeplete");
Button.click();
}
@After
public void tearDown() throws Exception {
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(C:\\Users\\farzan.s.DIRECTI\\Desktop\\LetsKodeIt\jfjfnfh.png));
driver.quit();
}
}