简体   繁体   中英

How to use method of one class into other in TestNG

I'm using webdriver and eclipse with testNG.

I'm automating web for sanity checks. I have many modules in web application like A, B, C and so on. I created one class for A which covers all its sub-modules as well. Similarly, for B and CI have different classes. I have a login method for class A, which I also need for B and C as all these classes are independent, I have to write code from scrtach for each class.

Is there any way to use the Login() method from A in B and C?

My methods looks like this..

  Class A
   {

   @Test(priority = 0, Description = "Login")

   Public void Login()
   {
     //some code
   }

Thanks.

This question is not more related to Web-Driver though What you should do is the actions to be performed over multiple classes are same should be grabbed in Base class that contains regular operation and further this Base class can be extended to your other child class like A, B, C and all will posses with your Base class features.

Within a Base class you can also do the setup configuration before all your test starts and close your driver/streams after the completion of the test ie your Test Suite so this happens with each of your Test Suite that you do

Example code

class BaseTest{
    WebDriver driver;
    @BeforeSuite
    public void setupTest(){
        driver = new FirefoxDriver();
    }

    @AfterSuite
    public void finishTest(){
        driver.quit();
    }

    public void navigateToMenu(String menu){
        WebElement element = driver.findElement(By.xpath("//XPATH_TO_MENU_WHERE=" + menu));
        //Here menu is something will be replaced with navbar menu name, just an example of what you can do using BaseTest class
    }
}

Hope this gives you idea on the approach you should go on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM