1

I need to automate the two Web applications in parallel and that needs to validate in Chrome browsers.

So I have two testng XML files(testng1.xml and testng2.xml), my scenario is to launch one chrome browser per xml file, so two chrome browsers in parallel.

Also I have common class for launching Chrome browser where I have initialized the webdriver.

Launch Browser code:

public class LaunchBrowser
{
    public static WebDriver driver = null;

    public LaunchBrowser()
    {

    }

    public LaunchBrowser(WebDriver drive)
    {
      driver=drive;
      System.setProperty("webdriver.chrome.driver", "C:\\selenium\\drivers\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
    }

   @Test
   public void initDriver()
   {
     if (driver == null)
     {
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
     }
 }

}

testng1.xml

<suite name="Suite1">

<test name="Browser">
    <classes>
        <class name="com.ci.selenium.LaunchBrowser" />
    </classes>
</test>

<test name="Google">
<classes>
        <class name="com.ci.selenium.Google" />
    </classes>
</test>


<test name="Gmail">
<classes>
        <class name="com.ci.selenium.GmailLogin" />
    </classes>
</test>
</suite>

testng2.xml

 <suite name="Suite2">
   <test name="01StackOverflow">
     <classes>
       <class name="com.ci.selenium.Stackoverflow" />
     </classes>
  </test>

  <test name="02StackLogin">
     <classes>
        <class name="com.ci.selenium.StackLogin" />
     </classes>
  </test>
</suite>

Google Class in xml1

public class Google
{
   WebDriver d;

   LaunchBrowser l = new LaunchBrowser();

   @BeforeClass
   public void launch()
   {
     LaunchBrowser ll = new LaunchBrowser(d);
   }
   @Test
   public void f() throws InterruptedException
   {      
      l.getURL("https://www.google.co.in");
      Thread.sleep(3000);
      System.out.println("google");
   }
}

GmailLogin Class in xml1

 public class Gmail
 {
    @Test
    public void test() throws InterruptedException
    {
      driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/?tab=wm'][text()='Gmail']")).click();
      Thread.sleep(3000);
    }
  }

StackOverflow class in xml2

public class Stackoverflow
{
  WebDriver d1;

  LaunchBrowser l = new LaunchBrowser(d1);

  @Test
  public void f() throws InterruptedException 
  {
     System.out.println(l==null ? "null" : "not null");
     l.getURL("https://stackoverflow.com");
     Thread.sleep(3000);
     System.out.println("Stack");
  }

StackOverflow Login

 public class StackLogin
 {

    LaunchBrowser l = new LaunchBrowser();

    @Test
    public void f() throws InterruptedException
    {
      l.xpathClick("//a[@href='https://stackoverflow.com/users/login?
 ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f']")).click();
     Thread.sleep(3000);
    }
 }

The above code is not running in parallel exceution. So kindly help to resolve this scenario.

  • Did you do a google search on how to run a test cases in parallel in TestNG? Because there are already lot of material on it. – Alok Jan 17 '18 at 6:59
  • You have to configure the number of threads in testng.xml configuration. One sample reference link: howtodoinjava.com/testng/testng-executing-parallel-tests – Bhavani Jan 17 '18 at 7:07
  • Hi Anok, I want to run the xml files in parallel, not the parallel execution of test methods. Since test method in each xml file depends on each other. For eg : If we want to login gmail account we need to go Google.com. So gmail login is depends on Google.com. Like that the two xml files were created. – Mohan Kumar Jan 17 '18 at 7:47
  • My use case is, I have n number of XML test suite files and each xml file should run in individual chrome browser in parallel. Please let me the better solution to achieve this scenario. – Mohan Kumar Jan 17 '18 at 12:47
  • You create two suite xml files and call that in testng.xml (Suite-of-suite). This should help. stackoverflow.com/questions/26810342/… – Alok Jan 17 '18 at 18:40
1

I have achieved this scenario and works fine for me.

Please click the below link for the answer.

One Browser per testNG XML Suite file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.