Sunday, July 28, 2019

How to Test a Selenium Script Parallel in Multiple Browser ?| TestNG parameters tag |How to create a TestNG.xml file ?? | Cross Browser Testing |

Suppose , if we have a single script and that we need to run on multiple browsers at a same time than rather than creating two separate class for each browser we can do it with the TestNG's parameter tag.

How?

let me show it

Step 1:-
- Create a new java class. and create a method with @BeforeTest annotation of TestNG. and take the parameter's name ( Browser's name ) on which test is going to run inside that method.


@Parameters("Browsername")
 
 @BeforeTest
 public void setup(String Browsername)

Step 2:-

Write a code for setting up a Web Driver based on the based on the Browser's name.

@BeforeTest
 public void setup(String Browsername)
 {
  if(Browsername.equalsIgnoreCase("chrome"))
  {
         System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\eclipse-workspace\\chromedriver.exe");
    
        driver = new ChromeDriver();
  }
  
  if(Browsername.equalsIgnoreCase("firefox"))
  {
   System.setProperty("webdriver.gecko.driver","C:\\Users\\5558\\eclipse-workspace\\geckodriver.exe");
   driver = new FirefoxDriver();
  }
  
  
 }

Step 3:- Create a @Test method and write your test code in that method.

Step 4:- Create @AfterTestMethod and write a code to close the browser.


Step 6:-

Right click on that class file and go to Testng >> Convert To TestNG class





and Create a testng.xml file of a class.

this file will be show in TestNG folder of your project.


Step 7:-  configure testng.xml file like this image below.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Suite" parallel="tests">
  <test name="TestChrome">
  <parameter name="Browsername" value="chrome"> </parameter>
    <classes>
      <class name="mmt.crossbrowsers"/>
    </classes>
  </test>
   
   <test name="Testff">
  <parameter name="Browsername" value="firefox"> </parameter>
    <classes>
      <class name="mmt.crossbrowsers"/>
    </classes>
  </test> 
  
  
  </suite> 


here, Parameter name is should be same as we have declared in class and its value refers to the browser on which test is going to run.

in Suits tag Thread-counts will refer to the number of browsers in which our test will run.


Now ,
Right click on this testng.xml file and run it with the testng

it will open a chrome browser and firefox browser parallely , than opens a url specified , it will print a title of a page.

you can Add IE browser in this suit and method too. but remember one thing , we need to run testng.xml file and not a class file otherwise it will give an error.



i hope this article helps you guys :) if you like it useful and please share it with others too.
also if you have any query than comment down below i will try my level best to answer it to you :)


Thanks! 

No comments:

Post a Comment