1

I'm writing a java code to do the loading test using selenium. I have a text file which contains the urls of websites i'm using for the test. I want my app to read the text file and display the loading time for each url in the text file. How can i do it?

This code is for two websites but how can i read a text file which includes several websites and get the loading times.

package webauto;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebAuto {

    public static void main(String[] args) {
        WebDriver firefoxDriver=new FirefoxDriver();

        long start = System.currentTimeMillis();

        firefoxDriver.get("http://www.google.com");

        long finish = System.currentTimeMillis();
        long totalTime = finish - start; 
        System.out.println("Total Time for page load - "+(totalTime/1000)+"s");

        long start1 = System.currentTimeMillis();

        firefoxDriver.get("http://www.ebay.com");

        long finish1 = System.currentTimeMillis();
        long totalTime1 = finish1 - start1; 
        System.out.println("Total Time for page load - "+(totalTime1/1000)+"s");
    }
}
Danielson
  • 2,459
  • 2
  • 25
  • 48
PVS
  • 25
  • 4
  • Take a look at this [question](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java), it'll tell you how to read a text file. – mike Aug 20 '15 at 09:54

3 Answers3

1

You can do something like below, assuming every line in your text file refers to a website link, pass the path of your text file in filePath variable:-

WebDriver firefoxDriver=new FirefoxDriver();
try {
    BufferedReader reader= new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"));         
    String websiteLink;
    while ((websiteLink = reader.readLine()) != null) {
        long start = System.currentTimeMillis();
        firefoxDriver.get(websiteLink);
        long finish = System.currentTimeMillis();
        long totalTime = finish - start; 
        System.out.println("Total Time for page load - "+(totalTime/1000)+"s");
    }
    reader.close();

} catch (IOException e) {
    e.printStackTrace();
}
Danielson
  • 2,459
  • 2
  • 25
  • 48
Amit Bhati
  • 5,215
  • 1
  • 19
  • 42
-1

As a load testing tool it's better to use Jmeter, but in some cases, especially when you write tests for AJAX-rich applications or when you need to imitate real users scenarios, it's necessary to combine advantages of Jmeter, that is more suitable, than Selenium for generating load and collecting performance metrics, and Selenium, that is more suitable for interacting with a web page. And of course it's recommended to use Blazemeter, as a cloud based solution for running tests.

The details of this solution you may find here: https://www.blazemeter.com/blog/gotta-test-em-all

-1

If your objective is to purely test how the page loads in a single browser session instead of emulating a user load pattern, you can user browser mob proxy with selenium. you can parse the .har file in language of choice to determine the page load time.

user3262242
  • 174
  • 1
  • 2
  • 13