如何使用Selenium WebDriver获取页面加载的精确时间?
我们可以使用Thread.sleep。
我们可以使用implicitlyWait。
我们可以使用WebDriverWait。
但是,如何使用Selenium WebDriver获取页面加载的精确时间呢?
如何使用Selenium WebDriver获取页面加载的精确时间?
我们可以使用Thread.sleep。
我们可以使用implicitlyWait。
我们可以使用WebDriverWait。
但是,如何使用Selenium WebDriver获取页面加载的精确时间呢?
long start = System.currentTimeMillis();
driver.get("Some url");
long finish = System.currentTimeMillis();
long totalTime = finish - start;
System.out.println("Total Time for page load - "+totalTime);
如果这样不起作用,那么你必须等到页面上显示出某个元素。 long start = System.currentTimeMillis();
driver.get("Some url");
WebElement ele = driver.findElement(By.id("ID of some element on the page which will load"));
long finish = System.currentTimeMillis();
long totalTime = finish - start;
System.out.println("Total Time for page load - "+totalTime);
您可以使用org.apache.commons.lang3.time包中的StopWatch对象。以下是使用Java的Selenium WebDriver的完整代码:
import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TimerInSeleniumWebDriver {
public static void main(String[] args) {
WebDriver driver;
driver = new FirefoxDriver();
StopWatch pageLoad = new StopWatch();
pageLoad.start();
//Open your web app (In my case, I opened facebook)
driver.get("https://www.facebook.com/");
// Wait for the required any element (I am waiting for Login button in fb)
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("u_0_l")));
pageLoad.stop();
//Get the time
long pageLoadTime_ms = pageLoad.getTime();
long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");
System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds");
driver.close();
}
}
为了获得最佳体验,请使用TestListners:
@AfterMethod
public void getResult(ITestResult result) throws Exception {
String sMethodName = "";
Long lExecutionTime;
sMethodName = result.getMethod().getMethodName().toString();
lExecutionTime = (result.getEndMillis() - result.getStartMillis()) / 1000;
System.out.println(sMethodName + " execution Time is : " + lExecutionTime + "sec.");
}
我也有这个需求,我使用了浏览器提供的performance.timeOrigin对象。我在C#中使用它,但是对于这个问题,我修改了@Ripon Al Wasim的答案,使其适用于Java。
import org.apache.commons.lang3.time.StopWatch;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TimerInSeleniumWebDriver {
public static void main(String[] args) {
WebDriver driver;
driver = new FirefoxDriver();
//Open your web app (In my case, I opened facebook)
driver.get("https://www.facebook.com/");
// Wait for the required any element (I am waiting for Login button in fb)
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("u_0_l")));
//Get the time
long pageLoadTime_ms = getPageLoadTime();
long pageLoadTime_Seconds = pageLoadTime_ms / 1000;
System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds");
System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds");
driver.close();
}
public static long getPageLoadTime(WebDriver driver){
//Creating the JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//This will get you the time passed since you page navigation started
return (Long)((JavascriptExecutor)driver).executeScript("return Date.now() - performance.timeOrigin;"");
}
}
public static long getPageLoadTime(WebDriver driver){
//Creating the JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//This will get you the time passed since you page navigation started
return (Long)((JavascriptExecutor)driver).executeScript("return Date.now() - performance.timeOrigin;"");
}
你不需要担心页面加载开始的位置,但就像其他答案中所述,你必须在页面加载完成后立即放置它。
这类似于StopWatch,但更加简洁(无需启动、停止、重置)。
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
驱动程序.管理().超时().页面加载超时(60, TimeUnit.SECONDS);