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,290 points
1,203 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,050 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

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

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,130 points
2,972 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

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,050 points
406 views
0 votes
1 answer
0 votes
1 answer

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

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

answered Apr 18, 2018 in Selenium by king_kenny
• 3,610 points
215 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,130 points
762 views
0 votes
1 answer
0 votes
1 answer