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.

9
  • 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. Commented Feb 15, 2016 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? Commented Feb 15, 2016 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. Commented Feb 15, 2016 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. Commented Feb 15, 2016 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. Commented Feb 15, 2016 at 23:33

2 Answers 2

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
 });
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but i am using Java 1.6.0_27
Why are you using Java 1.6.0_27? Its release date was 2011-08-16.
Systems where i am trying to run this are legacy ones and we cannot do an upgrade on them just for this.
OK, fair enough, as long as you add the caveat to your results that this is a very old and unsupported JDK, and shouldn't be considered a guide for any new work or deployments etc.
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.