calling a test method from another test class

6,622 views
Skip to first unread message

Elangovan Ganesh

unread,
Mar 23, 2012, 10:25:31 AM3/23/12
to webdriver
Hi All,

I am using Page Object Design Pattern (java+webdriver+TestNG)

I want to call a test method (which presents in one test class) from
another test class. When i call this i get NullPointerException.

Let me explain in clear

class testclass1{

// launching browser, initiating elements using age factory
as well creating objects for the page classes
@Test
public void testmethod1(){
}
}

Now i want to access the testmethod1 from another test class named
testclass2

class testclass2{
// launching browser, initiating elements using age factory
as well creating objects for the page classes
testmethod1 obj = new testmethod1();
@Test
public void testmethod1(){
obj.testmethod1(); // this line gives null
pointer exception
}
}

Please help out me guys.


Thanks in advance,
Elangovan G

Mark Collin

unread,
Mar 23, 2012, 10:30:05 AM3/23/12
to webd...@googlegroups.com
Why do you want to do this?

Tests depending upon other tests is a major code smell!

Hi All,

class testclass1{

--
You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.


Elangovan Ganesan

unread,
Mar 23, 2012, 10:39:34 AM3/23/12
to webd...@googlegroups.com
Hi Mark,

I accept your point. If i do like this i will reduce my LOC. 

Actually My application is collection of 8 different application. For each application i want to login.
I have login() in the TestLogin.class. Since all application needs to be login, i want to call this.

Thanks,
Elangovan

ramesh

unread,
Mar 23, 2012, 11:11:42 AM3/23/12
to webdriver


Make the login and rest other common functions in baseclass as static
methods and use your testclass to extend baseclass and access the
same.

On Mar 23, 2:39 pm, Elangovan Ganesan <golagan...@gmail.com> wrote:
> Hi Mark,
>
> I accept your point. If i do like this i will reduce my LOC.
>
> Actually My application is collection of 8 different application. For each
> application i want to login.
> I have login() in the TestLogin.class. Since all application needs to be
> login, i want to call this.
>
> Thanks,
> Elangovan
>
> On Fri, Mar 23, 2012 at 8:00 PM, Mark Collin
> <mark.col...@lazeryattack.com>wrote:

Mark Collin

unread,
Mar 23, 2012, 11:15:58 AM3/23/12
to webd...@googlegroups.com

It sounds like you need a login functions that you can call in your tests.

Richard Lavoie

unread,
Mar 23, 2012, 11:17:27 AM3/23/12
to webd...@googlegroups.com
In fact the login process shouldn't be a test in itself, it'S a behavior (imo).

You could just include the login process in a Page object, and from your login test, you validate if the login execution now points to the right following page.

pseudo code :

LoginPage {

    public LoginPage(WebDriver wd) {
        load the login page on the web driver...
    }

    public MainPage login(user, pass) {
        .... login ...
        wait for distinct element on the main page to appear
        return new MainPage();
    }
}

public class Tests {

LoginPage login;

@Before
public void setup() {
page = new LoginPage(new WebDriver...);
}


@Test
public void loginTest() {
login.login();
}

public void otherTest() {
MainPage main = login.login();
your test on the main page.
}

}

Something like this could help you, or as other mentioned, a base class with login methods to enter information on the current page.
Richard Lavoie
IT consultant / consultant en informatique

Karthik

unread,
Mar 23, 2012, 11:37:23 AM3/23/12
to webd...@googlegroups.com

Hi Elangovan,

Login is not a test its an operation on the page. So make login a method in the Loginpage class and you will have to call this operation from any other testclass. This is the basic principle behind Page Object Pattern.

Also, in your code you are trying to create an object for a method - ideally you should be instantiating the class and not the method.

Thanks, Karthik

Danny Guerrier

unread,
Mar 23, 2012, 9:10:33 PM3/23/12
to webd...@googlegroups.com
Create an API for each of the 8 apps and provide a login method for each one.

Elangovan Ganesan

unread,
Mar 24, 2012, 12:50:10 PM3/24/12
to webd...@googlegroups.com
Hi Danny,

I am not getting your point. Can you explain in clear?


Thanks,
Elangovan G

SANTHOSH BABY

unread,
Mar 25, 2012, 3:02:39 AM3/25/12
to webd...@googlegroups.com
Hi Elogovan , 

Automation is very simple and not so complicated  at all . 
From my understanding why you are getting Null pointer exception , it purely a  programmatic error or coding error 

 you have created webdriver instance  in one class now you have to pass this instance in all the remaining class or test methods  

Before going to selenium automation have fair knowledge on OOPS concepts .
Now how you can  implement  it Learn JAVA programming basics :) 
Well You can impleement this is abstract class  or create global instance in every class . 



Thank you
Cheers.
rsz_santhosh.jpg
Santhosh Baby

Santhosh Software Automation  Engineer
 



Think GREEN. Please consider the environment before printing this email

"When The Winds of Change Blow..... Some people Build Walls & Others Windmills.... Attitude Matters...."
image001.gif
rsz_santhosh.jpg

Mike Riley

unread,
Mar 26, 2012, 12:50:49 PM3/26/12
to webdriver
Let me take a whack at it...

I have a a Helper class that handles creating the Selenium session, as
well as login, error reporting, and other utility functions.

My tests keep a global copy of the Helper class instance:

public Helper helper;

This instance is created by my main() method, since my tests get
started from the command line, but could just as easily be created by
your first test method if TestNG or JUnit is being used. All the
tests then use the same instance. If you can have multiple instances
of your test classes you might want to make it a static, so there is
only one copy for all the classes.

If my tests involve multiple classes then I create a root class where
I keep the helper instance and all the other classes extend that root
class, so it is easy to address as helper.whatever from any of the
classes, but is only declared in the one place.

Mike


On Mar 24, 9:50 am, Elangovan Ganesan <golagan...@gmail.com> wrote:
> Hi Danny,
>
> I am not getting your point. Can you explain in clear?
>
> Thanks,
> Elangovan G
>
>
>
>
>
>
>
> On Sat, Mar 24, 2012 at 6:40 AM, Danny Guerrier <dguerr...@gmail.com> wrote:
> > Create an API for each of the 8 apps and provide a login method for each
> > one.
>
> > On Mar 23, 2012, at 10:30 AM, Mark Collin <mark.col...@lazeryattack.com>

darrell

unread,
Mar 27, 2012, 11:31:03 AM3/27/12
to webd...@googlegroups.com
Elangovan,

A Page Object Design means creating a class for each 'page'. So if your application has a login page, you create a class to represent the actions and locators for that page. For example, on http://www.gmail.com you might have:

class LoginPage {

WebDriver driver = null;
By usernameInput = By.cssSelector("input#Email");
By passwordInput = By.cssSelector("input#Passwd");
By signInButton = By.cssSelector("input#signIn");

public LoginPage(WebDriver driver) {
this.driver = driver;
 }

public void login(String username, String password) {
driver.findElement(usernameInput).sendKeys(username);
driver.findElement(passwordInput).sendKeys(password);
driver .findElement(signInButton).click();
}
}

You would also add methods for checking the "Stay signed in" checkbox, clicking the "Can't access your account?" link, clicking the "Create an Account" link, etc.

If you have testcase1 and it just tests logging in then testcase2 tests getting the number of new messages. Obviously, testcase2 requires you to login before you can count the number of new messages. So testcase1 might be:

@Test
public void testLoggingIn() {
LoginPage login = new LoginPage(driver);
LoggedInPage main = new LoggedInPage(driver); 
login.login("dar...@gmail.com", "not my real password");
assertTrue(main.isLoggedIn());
 }

then testcase2 might be:

@Test
public void testMessageCount() {
 
int expectedNewMessages = 3;
 
LoginPage login = new LoginPage(driver);
LoggedInPage main = new LoggedInPage(driver);
login.login("dar...@gmail.com", "not my real password");
int actualNewMessages = main.getNewMessageCount();
assertEquals(expectedNewMessages, actualNewMessages);
}

I left out the code for the class LoggedInPage but it would be created in a similar fashion as LoginPage.

In other words, you put the code for testcase1 in a class, instantiate the class in testcase1 and call the method. This way if you need the method for testcase2 you can instantiate the class in testcase2, exactly like testcase1, call the method THEN have the code for testcase2.

You want to put the bulk of your code in these Page classes and instantiate them as you need them. Your test cases should be a simple:

setUp
a few lines of code to get a result
an assert statement to confirm you got the result you expected
tearDown.
Reply all
Reply to author
Forward
0 new messages