Learner
Learner

Reputation: 33

How to call global methods to class from main class in Selenium using TestNG

package com.MavenLearning.Login;

import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class LoginOne {
 
	@Test
  public void LoginTestOne() 
	
	{
		{
			System.setProperty("webdriver.gecko.driver","C:\\Webdriver\\geckodriver.exe");
			WebDriver driver = new FirefoxDriver();
			driver.get("http://www.demo.guru99.com/v4/");
			driver.findElement(By.name("uid")).sendKeys("mngr105709");
			driver.findElement(By.name("password")).sendKeys("jajeten");
			driver.findElement(By.name("btnLogin")).click();
			assertEquals(driver.getTitle(), "Guru99 Bank Manager HomePage");
			String A = driver.getTitle();
			System.out.println(A);
			String B = "Guru99 Bank Manager HomePage";
			System.out.println(B);
			if (A.equals(B))
				System.out.println("Page Title matches");
			else
				System.out.println("Page Title Doesn't Match");
		}
  }
}

Seniors, I have written my code in Selenium and stored it in a global method. I don't know how to call the method in the other class. When I was learning Selenium with Java I was easily calling methods to different class but stuck now with TestNG. I have tried importing the method package so everything should work but no success. Thanks in advance for help.

Upvotes: 0

Views: 1351

Answers (1)

Shubham Jain
Shubham Jain

Reputation: 17593

From Global if you mean public static then you just need to call it like below:

ClassName.functionName()

If it is not static then you need to create an object of that class and then call the function like:

MyClass my = new MyClass();
my.MyFunctionName();

Updated

You need to create a xml file and specifically all classes which you need to execute.

Example of xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <classes>
      <class name="com.first.example.demoOne"/>
      <class name="com.first.example.demoTwo"/>
      <class name="com.second.example.demoThree"/>
    </classes>
 </test>
</suite>

Source:

http://www.seleniumeasy.com/testng-tutorials/testngxml-example-to-execute-with-class-names

Video tutorial:

https://www.youtube.com/watch?v=cNhnqVWD_54

Upvotes: 1

Related Questions