2

I am newbie learning selenium and wrote below java code. I am trying to run a for loop that is supposed to load the site 20 times. Right now it does loop in sequential order and I want that to be run in parallel.

public class lenders {
    
   //ExtentReports logger = ExtentReports.get(lenders.class);
   public static void main(String[] args) throws InterruptedException  {
                            
   for (int i=0; i<20; i++) {
      FirefoxDriver driver= new FirefoxDriver();
      driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);

      try {
         driver.get("https://www.google.com");
      } catch (TimeoutException e) {
           driver.quit();
      }
}

Towards the end I want 20 browsers to be open and loading the site and all of them getting killed.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
user1550159
  • 1,197
  • 3
  • 19
  • 36
  • 1
    _Why_ are you wanting to do this? This is a _bad practice_ in a professional setting and if you are learning, it is better to learn good practices. You have plenty of time to develop bad habits. – Brian Feb 15 '16 at 23:27
  • 20 Firefox instances hammering Google really isn't going to prove much, and will be hideously slow. Are you trying to do performance testing? – Andrew Regan Feb 15 '16 at 23:29
  • I cannot comment on good or bad practice, but my requirement is to run threads in parallel. Any help here is appreciated. – user1550159 Feb 15 '16 at 23:31
  • We're just trying to work out what you're trying to achieve. For all we know you've been given bad advice. – Andrew Regan Feb 15 '16 at 23:32
  • Yes i am trying to do a load test. I added google.com as an example and i will change it different domain during my testing. – user1550159 Feb 15 '16 at 23:32
  • 1
    Still need to know what you're trying to measure. Did you consider JMeter or perhaps Siege? Testing with *real* browsers is *so* slow. – Andrew Regan Feb 15 '16 at 23:33
  • @user1550159 - Check out the [Selenium Grid](http://stackoverflow.com/questions/22903630/how-to-perform-load-testing-using-selenium-webdriver). It is a _much_ better vehicle for load testing. – Brian Feb 15 '16 at 23:33
  • I wouldn't ever use Sel for (server) load testing, and if you're interested in client-side load, why use Grid? But OP should clarify requirements. As ever, there's a world of possibilities. – Andrew Regan Feb 15 '16 at 23:36
  • Instead of doing this you can just integrate your Script with jemeter, jmeter will open 0-300 browser for you. But its really Bad Practice. (Use Headless Browser HTMLunit.) – SacTan Feb 16 '16 at 09:25

2 Answers2

6

If you are on Java-8 you can run a parallel for loop using aparallelStream

 IntStream.range(0,20).parallel().forEach(i->{
         ... do something here
 });
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

In general and at a high level, trying to run code in parallel within java means that you are trying to run multi-threaded code (more than one thread executing at one time).

As individuals have been saying in comments, one must therefore give a warning with my answer. Multi-threading in itself is a complicated topic and one must enter this territory with caution as there can be many issues/topics regarding "thread safety" and even if this is the way you "should" approach the "business request".

In any case, if you really want to create something multi-threaded then I would direct you to a few technical items to get you STARTED on the topic training (and your own further research): You could create another class that implements the Callable interface. It will then have to have the "call" method by nature of implementing that interface. In this class and in the "call" method you would put the actual logic that you want to happen in parallel. In your case, all of the driver, etc.

Then in your parent class (the one you put code from above), you can use a FixedThreadPool and an associated ExecutorService that accepts this callable class. It will essentially run the "call" method in a separate thread so that your for loop can continue onwards at the same time that the logic in the "call" method is executed. It will go the second time around and create another thread, etc. You can manage how many threads are created using your thread pool. You can use different kind of thread pools and services, etc. Again, this is a really BIG topic and field. I am just trying to get your nose in a direction for you to start researching it further.

People might not like my answer because they think you should use completely different technologies other than using Selenium in this manner, etc. I totally understand that point of view and don't disagree with those alternate answers. However, your question was "how to get this running at the same time" and I have tried to give you the building block answer. I hope that helps! Let me know if you need some links to tutorials or anything, but google "ExecutorService" "Thread Pool" and "Callable" (or combinations of them) with the word java and tutorial should get you a variety of answers on the topic.

I hope that helps!

BoBoCoding
  • 161
  • 1
  • 4