Need to perform parallel execution multiple browser sessions with Selenium TestNG by using BeforeSuite

0 votes

I know to run multiple browser sessions on Chrome without @BeforeSuite, but at times, I need to assign variables or something else in @BeforeSuite annotation before going to @BeforeClass or @BeforeTest. And, at the same time I need to start multiple sessions in parallel. How do I do that?

I'v shared sample code below. Here, I use @BeforeSuite to assign few variables and then call 2 parallel tests from TestNG.xml. It will only call 1 test (not 2). But when I don't use @BeforeSuite, it works perfectly fine (both tests run parallelly).
So, Is it possible to run parallel tests while also using @BeforeSuite? Sometimes we do need to use @BeforeSuite in some test scenarios and call multiple browsers sessions.

Thanks.

public class MyClass {
  String baseURL;
  String browser;

  @BeforeSuite
  private void setTheVariables() {
    //Some codes here
    //Some codes here
    this.browser = "chrome";
  }

  @BeforeClass
  private void myBeforeClass() {
    //Some codes here
    //Some codes here
  }

  @BeforeTest
  private void myBeforeClass() {
    //Some codes here
    //Some codes here
  }

  @Test
  @Parameters("baseURL")
  public void f(String baseURL) {

    if (this.browser == "chrome") {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\abc\\Downloads");
      DesiredCapabilities caps = DesiredCapabilities.chrome();
      LoggingPreferences logPrefs = new LoggingPreferences();
      logPrefs.enable(LogType.BROWSER, Level.ALL);
      caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
      WebDriver driver = new ChromeDriver(caps);
      System.out.println("Navigating to " + baseURL);
      driver.get(baseURL);
    }
  }
}

Below is my testNG.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestSuite" parallel="tests" thread-count="2">

  <test name="Test1" preserve-order="true">
    <parameter name="baseURL" value="http://www.amazon.com" />

    <classes>
      <class name="package.myClass" />
    </classes>
  </test>

  <test name="Test2" preserve-order="true">
    <parameter name="baseURL" value="http://www.google.com" />

    <classes>
      <class name="package.myClass" />
    </classes>

  </test>

</suite>
Mar 30, 2018 in Selenium by Shubham
• 13,490 points
6,014 views

1 answer to this question.

0 votes

@Beforesuite annotated method runs before the testNG.XML file. Hence, you have to use another annotation just below this order i.e. @BeforeClass annotation for setting the browser type in your java class. Add a parameter named browser in your testNG.xml to pass on the type of browser. That shall make the parallel execution possible.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestSuite" parallel="tests" thread-count="2">
  <test name="Test1" preserve-order="true">
    <parameter name="baseURL" value="http://www.amazon.com" />
    <parameter name="browser" value="chrome">
    <classes>
      <class name="package.myClass" />
    </classes>
  </test>
  <test name="Test2" preserve-order="true">
    <parameter name="baseURL" value="http://www.google.com" />
    <parameter name="browser" value="firefox">
    <classes>
      <class name="package.myClass" />
    </classes>
  </test>
</suite>
answered Mar 30, 2018 by nsv999
• 5,500 points

Related Questions In Selenium

0 votes
1 answer
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
16,000 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,611 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,109 views
0 votes
2 answers

What is the role of TestNG & JUnit frameworks in Selenium?

TestNG and JUnit are test frameworks . it ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
2,862 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,202 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP