[英]How can we get exact time to load all the links using selenium webdriver?
[英]How can we get exact time to load a page using Selenium WebDriver?
我们如何使用 Selenium WebDriver 获得加载页面的准确时间?
我们使用 Thread.sleep
我们使用隐式等待
我们使用 WebDriverWait
但是我们怎样才能获得使用 Selenium WebDriver 加载页面的准确时间呢?
如果您想了解使用 Selenium WebDriver(又名 Selenium 2)完全加载一个页面需要多少时间。
通常,只有在页面完全加载后,WebDriver 才会将控制权返回给您的代码。
因此,以下 Selenium Java 代码可能会帮助您确定页面加载的时间 -
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();
}
}
我也有这个需求,我使用了浏览器提供的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 类似,但更干净(无需启动、停止、重置)。
使用 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.");
}
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.