我正在尝试记录从页面开始到显示特定内容的时间,并将该时间写入excel文件,然后再次执行x次。现在我每次都可以通过循环来覆盖所有内容而且我只能以最后的结果结束。另一个问题是它没有将信息放在我预期的位置。我只想在第一列中得到所有结果。几天来,我一直在试着对抗这个问题,尝试了我在SO中找到的所有选项,取得了不同的成功,但从未得到我一直在拍摄的结果。任何帮助将不胜感激。
import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
// Revert back to here
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
public class PageLoadTimer {
public static void main(String[] args) {
// Set Browser Location
System.setProperty("webdriver.chrome.driver",
"/Users/mackro/Desktop/Java Workspace/BrowserDrivers/chromedriver");
// Create object of SimpleDateFormat class and decide the format
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
// get current date time with Date()
Date date = new Date();
// Now format the date
String date1 = dateFormat.format(date);
// Start Loop
int i = 0;
while (i <= 2) {
WebDriver driver = new ChromeDriver();
// Start timer
StopWatch pageLoad = new StopWatch();
pageLoad.start();
// Go to URL & skip basic auth
driver.get("https://www.godaddy.com/");
// Wait for cookies to be displayed or 10 seconds then
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Cookies")));
pageLoad.stop();
// Get the time
long pageLoadTime_ms = pageLoad.getTime();
long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
driver.close();
// put the time in an excel file
try {
FileOutputStream fileOut = new FileOutputStream(
"/Users/mackro/Desktop/Java Workspace/Data Tables/" + date1 + ".xls");
Workbook workbook = new HSSFWorkbook();
CreationHelper createHelper = workbook.getCreationHelper();
org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("Page Load Times");
Row row1 = sheet.createRow((short) 0);
// inserting first row cell value
row1.createCell(i).setCellValue(createHelper.createRichTextString("Time " + pageLoadTime_ms));
workbook.write(fileOut);
workbook.close();
fileOut.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
i++;
}
System.out.println("results recorded");
}
{
}
}
答案 0 :(得分:0)
对于while循环的第一次迭代,您需要创建工作簿,并且在下面的迭代中,您可以使用在第一次迭代中创建的相同的工作簿。尝试如下:
int i = 0;
while (i <= 2) {
WebDriver driver = new ChromeDriver();
// Start timer
StopWatch pageLoad = new StopWatch();
pageLoad.start();
// Go to URL & skip basic auth
driver.get("https://www.godaddy.com/");
// Wait for cookies to be displayed or 10 seconds then
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Cookies")));
pageLoad.stop();
// Get the time
long pageLoadTime_ms = pageLoad.getTime();
long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
driver.close();
try {
HSSFWorkbook workbook;
Sheet sheet;
if(i==0) {
workbook = new HSSFWorkbook();
sheet = workbook.createSheet("Page Load Times");
}
else {
FileInputStream fis = new FileInputStream("/Users/mackro/Desktop/Java Workspace/Data Tables/" + date1 + ".xls");
workbook = new HSSFWorkbook(fis);
sheet = workbook.getSheet("Page Load Times");
}
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellType(CellType.STRING);
cell.setCellValue(pageLoadTime_ms);
FileOutputStream fos = new FileOutputStream("/Users/mackro/Desktop/Java Workspace/Data Tables/" + date1 + ".xls");
workbook.write(fos);
fos.flush();
fos.close();
workbook.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
i++;
}