如何使用Selenium WebDriver获取加载页面的确切时间?
我们使用Thread.sleep
我们使用implicitlyWait
我们使用WebDriverWait
但是如何使用Selenium WebDriver获取加载页面的确切时间?
答案 0 :(得分:14)
如果您想知道使用Selenium WebDriver(a.k.a 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);
答案 1 :(得分:3)
您可以使用org.apache.commons.lang3.time包的StopWatch对象。以下是使用Java的Selenium WebDriver的完整代码:
int BUFFER_SIZE = 4096;
String name = "file.txt";
String method = "PUT";
String bucket = "bucket";
String secretKey = "******";
String filePath = "F:\\file.txt";
SimpleDateFormat df = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
Date date = new Date();
String formattedDate = df.format(date);
File uploadFile = new File(filePath);
URL url = new URL("http://bucket.s3.amazonaws.com/" + name);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
String resource = "/" + bucket + "/" + name;
String contentType = "text/plain";
String signn = method + "\n\n" + contentType + "\n" + formattedDate + "\n" + resource ;
Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(
secretKey.getBytes("UTF-8"), "HmacSHA1"));
String signature = (new BASE64Encoder()).encode(
hmac.doFinal(signn.getBytes("UTF-8"))).replaceAll("\n", "");
String authAWS = "AWS " + "**SECRET**" + ":" + signature;
httpConn.setDoOutput(true);
httpConn.setRequestMethod(method);
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Date", formattedDate);
httpConn.setRequestProperty("Content-type", contentType);
httpConn.setRequestProperty("Authorization", authAWS);
httpConn.setRequestProperty("x-amz-acl", "public-read"); // <----- THIS LINE HERE!
OutputStream outputStream = httpConn.getOutputStream();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("Response message : " + httpConn.getResponseMessage());
答案 2 :(得分:0)
我也有此要求,我使用了浏览器提供的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,但更干净(无需启动,停止,重置)。
答案 3 :(得分:0)
使用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.");
}
答案 4 :(得分:-10)
driver.manage()。timeouts()。pageLoadTimeout(60,TimeUnit.SECONDS);