How to run test case using TestNG

0 votes
I am beginner in testNG. Can someone tell me as how can I create test case to check wether my browser is working or not?

Thank you in advance.
Jan 8 in Selenium by Anjali
• 2,900 points
59 views

1 answer to this question.

0 votes

I am assuming that you want to create test case where you open the different webdriver related to different browser.

You need to start by creating a testNG class, here you will create test cases for different browsers. Then you will create a xml file where you will create the different class and test cases.

Here is a sample testNG class that I created that works for Chrome and Morzilla Firefox.

package testNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class MultipleTest
{
    WebDriver driver;
    String url = "https://www.google.com";
    @Parameters("browserType")
    @Test
    public void invokebrowser(String browserType) throws InterruptedException
    {
        browserType = browserType.trim();
        if (browserType.equalsIgnoreCase("chrome"))
        {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Downloads\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        else if(browserType.equalsIgnoreCase("firefox"))
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Users\\Downloads\\geckodriver.exe");
            driver = new FirefoxDriver();
        }
        else
        {
            System.out.println("Invalid Browser");
        }
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(2000);
        driver.close();
        
    }
}

Here is the XML file for the testNG class 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="ParellelTesting" parallel = 'tests'>

    <test name = "Chrome Testing">

        <parameter name = 'browserType' value = 'chrome'/>
        
            <classes>

                <class name = 'testNG.MultipleTest'/>

            </classes>

    </test>

    <test name = "Firefox Testing">
    
        <parameter name = 'browserType' value = 'firefox'/>
            
            <classes>
            
                <class name = 'testNG.MultipleTest'/>
                
            </classes>
    
    </test>

</suite>

Hope this will help.

answered Jan 8 by Nabarupa

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How to run a group of test cases using TestNG in Selenium Webdriver?

Hey Kajal, TestNG allows you to perform ordered ...READ MORE

answered Jun 12 in Selenium by Abha
• 15,680 points
45 views
0 votes
1 answer

How to run Selenium test case file from command line?

You’ll need Selenium RC for this: http://seleniumhq.org/download/  And the ...READ MORE

answered Apr 23, 2018 in Selenium by Vardy
• 2,280 points
2,416 views
0 votes
1 answer
0 votes
1 answer

How to run test scripts in Selenium using PhantomJS?

Hey @Shushil, you can run your test ...READ MORE

answered May 17 in Selenium by Abha
• 15,680 points
24 views
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
250 views
0 votes
1 answer
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
621 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
243 views