0

I am new to selenium and I want to run two classes or two methods at the same time not one after the other. I referred many links specially this link but in vain. Below is my code of testing.xml (for testing methods) where testStageDashboardand prodDashboards1 are test methods inside single class:

   <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
    <test name="Test 1">
        <classes>
            <class name="com.sd.selenium.FirstTestSelenium" />
            <!-- <class name="com.sd.selenium.practice.Monday_Test_Selenium_Till_Scrum_Call" 
                /> -->
        </classes>

    </test>
</suite>  

Testing.xml(for testing classes) :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="classes" thread-count="2">
  <test name="Test 1">
    <classes>
      <class name="com.sd.selenium.FirstTestSelenium"/>
      <class name="com.sd.selenium.practice.Monday_Test_Selenium_Till_Scrum_Call"/>
    </classes>
  </test>
</suite>  

where FirstTestSelenium and Monday_Test_Selenium_Till_Scrum_Call are two different classes under different packages.Do I need to write anything in java for browser nodes or something ? and how to run this ? What I did is I right clicked on my project and run as > Test NG test. Is anything else needed ? Thanks in advance :)

whatsinthename
  • 1,828
  • 20
  • 59

2 Answers2

0

Check whether you added @Test annotation for your classes/ methods and also specify browser details as test parameters. Here is sample testng.xml file

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="2">
    <test name="Test">
        <parameter name="myName" value="Test1"></parameter>
        <parameter name="browserType" value="chrome"></parameter>
        <parameter name="os" value="WINDOWS"></parameter>
        <classes>
            <class name="com.testng.session1.Session1" />
            <class name="com.testng.session1.Session2" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

Here is the sample code for classes:

public class Session1 {
    @Test 
    @Parameters ({"myName","browserType"})
    public void login(String name, String browserType){
        if(browserType.equalsIgnoreCase("firefox")){
        WebDriver driver=new FirefoxDriver();
        driver.get("www.google.co.in/");
        }
        else{
            System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\Jars\\chromedriver_win32\\chromedriver.exe");
               WebDriver driver = new ChromeDriver();
               driver.get("https://www.google.co.in/"); 
        }
        System.out.println("login Successful");
          System.out.println(name+"Thread ID: "+Thread.currentThread().getId());
    }

}

radhikab
  • 61
  • 3
  • yes it works well but one more add on is that we need to place testng.xml in src folder and run your project as TestNgSuite only.I need for methods as well .can you help me with that? – whatsinthename Apr 21 '17 at 10:08
  • Add @Test annotation for all the methods which you want to run in parallel and in above TestNg.xml file, just change parallel to "methods". It will work for you. We don't need to mention methods in TestNg.xml file. – radhikab Apr 21 '17 at 10:16
  • And also, it depends on thread-count. If you mention 2, every time only 2 threads executed parallely. – radhikab Apr 21 '17 at 10:18
  • it doesn't work. I need for running two methods at the same time . I have edited my testng.xml code for methods .please have a glance :) – whatsinthename Apr 21 '17 at 10:23
  • You have edited parallel to methods, what about your class "FirstTestSelenium.java" file? did you add @Test annotation for all the methods? And also, can you please provide exactly what error you are getting. – radhikab Apr 21 '17 at 12:37
  • Thanks @radhikab for helping but its not giving any error – whatsinthename Apr 24 '17 at 03:39
0

Below is the code for running methods in parallel with two threads.

TestClass:

package com.sd.selenium;

import org.testng.annotations.Test;

public class FirstTestSelenium {

    @Test
    public void test1(){
        System.out.println("method1");
    }

    @Test
    public void test2(){
        System.out.println("method2");
    }

}

Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
    <test name="Test 1">
        <classes>
            <class name="com.sd.selenium.FirstTestSelenium" />
             <!-- <class name="com.sd.selenium.practice.Monday_Test_Selenium_Till_Scrum_Call" 
                /> -->
        </classes>

    </test>
</suite> 

Let me know if you are getting any exceptions.

Akarsh
  • 967
  • 5
  • 9