import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class PageLoadTimeToCSV {
public static void main(String[] args) {
// Set the path to your WebDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// List of URLs to test
List<String> urls = Arrays.asList(
"https://www.example.com",
"https://www.google.com",
"https://www.github.com"
);
// CSV file path
String csvFile = "page_load_times.csv";
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
try (FileWriter writer = new FileWriter(csvFile)) {
// Write CSV header
writer.append("URL,Load Time (ms)\n");
for (String url : urls) {
// Start time before page load
long startTime = System.currentTimeMillis();
// Navigate to the URL
driver.get(url);
// Wait for the page to load completely (customize the wait condition as needed)
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));
// End time after page load
long endTime = System.currentTimeMillis();
// Calculate the load time
long loadTime = endTime - startTime;
// Write result to CSV
writer.append(url).append(",").append(String.valueOf(loadTime)).append("\n");
System.out.println("Page Load Time for " + url + ": " + loadTime + " milliseconds");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Leave a Reply