Calling a method from another class not working....

198 views
Skip to first unread message

Nipun

unread,
Nov 7, 2011, 6:16:45 AM11/7/11
to testng-users
Hi Friends,

I have a method as follows in class 'AvailabilitySearch'

@Test
public void testAvailability(){
System.out.println("<<in the @Test of Availability Search>>");
selenium.open("/reservation/ibe/ibe/booking?locale=en_US");
System.out.println("IBE loaded...");
Assert.assertEquals("Far East", selenium.getTitle());
}


Now from another class 'AvailabilitySearchDuplicate' i am trying to
call the method 'testAvailability' of the first class in the following
manner:

@Test
public void testAvailabilityDuplicate(){
System.out.println("<<in the @Test of Availability Search
Duplicate>>");
AvailabilitySearch search = new AvailabilitySearch();
search.testAvailability();

}


This is how the console shows after execution:


16:37:12.870 INFO - Preparing Firefox profile...
16:37:15.182 INFO - Launching Firefox...
16:37:18.339 INFO - Got result: OK,f7b5a53fecb84fd9b585597e56310ec1 on
session f7b5a53fecb84fd9b585597e56310ec1
16:37:18.401 INFO - Command request: open[, ] on session
f7b5a53fecb84fd9b585597e56310ec1
16:37:18.464 INFO - Got result: OK on session
f7b5a53fecb84fd9b585597e56310ec1
<<in the @Test of Availability Search Duplicate>>----------> Sys out
from Method of Second class
<<in the @Test of Availability Search>>---->Sys out from Method of
First class
<<in the After Test of Availability Search Duplicate>>
16:37:18.464 INFO - Command request: close[, ] on session
f7b5a53fecb84fd9b585597e56310ec1


The method gets called and the first line in the method, ie the
System.out.println() works, but the very next line which is
'selenium.open("/reservation/ibe/ibe/booking?locale=en_US");' gets
failed. From the above console log you can see the Firefox browser is
launched aswell...but selenium.open is not getting executed....
Can you please tell me where I am going wrong....




Simon Knott

unread,
Nov 7, 2011, 6:38:56 AM11/7/11
to testng...@googlegroups.com
Hi Nipun,

This is a Selenium issue, not TestNG, so I'd advise you to post your question in the Selenium forum: https://groups.google.com/forum/#!forum/selenium-users

Have you set the base URL on the selenium object instance?  If not, then it won't be able to open the relative URL.

Cheers,
Simon

Nipun

unread,
Nov 7, 2011, 7:12:55 AM11/7/11
to testng-users
Hi Simon,

Thank you for your valuable suggestions...
If i am not wrong, i have given base url in the selenium object
instance....
First Class:
-----------------
public class AvailabilitySearch {

public Selenium selenium;
public SeleniumServer seleniumServer;

@BeforeSuite
public void beforeSuite() throws Exception {
seleniumServer = new SeleniumServer();
seleniumServer.start();
System.out.println("<<in the Before Suite of Availability
Search>>");
}

@BeforeTest
@Parameters({ "selenium.host", "selenium.port",
"selenium.browser","selenium.url" })
public void beforeTest(String host, String port, String browser,
String url)throws Exception {
System.out.println("<<in the Before Test of Availability Search>>");
this.selenium = new DefaultSelenium(host,
Integer.parseInt(port),browser, url);
this.selenium.start();
this.selenium.open(url);

}

@Test
public void testAvailability(){
System.out.println("<<in the @Test of Availability Search>>");
selenium.open("/reservation/ibe/ibe/booking?locale=en_US");
System.out.println("IBE loaded...");
Assert.assertEquals("Far East", selenium.getTitle());
}

@AfterTest
public void afterTest() {
System.out.println("<<in the After Test of Availability Search>>");
this.selenium.close();
this.selenium.stop();

}

Second Class
----------------
public class AvailabilitySearchDuplicate {

public Selenium selenium;
public SeleniumServer seleniumServer;

/*@BeforeSuite
public void beforeSuite() throws Exception {
System.out.println("<<in the Before Suite of Availability Search
Duplicate>>");
seleniumServer = new SeleniumServer();
seleniumServer.start();

}*/

@BeforeTest
@Parameters({ "selenium.host", "selenium.port",
"selenium.browser","selenium.url" })
public void beforeTest(String host, String port, String browser,
String url)throws Exception {
System.out.println("<<in the Before Test of Availability Search
Duplicate>>");
this.selenium = new DefaultSelenium(host,
Integer.parseInt(port),browser, url);
this.selenium.start();
this.selenium.open(url);

}

@Test
public void testAvailabilityDuplicate()throws Exception{
System.out.println("<<in the @Test of Availability Search
Duplicate>>");
AvailabilitySearch search = new AvailabilitySearch();
search.testAvailability();

}


@AfterTest
public void afterTest() {
System.out.println("<<in the After Test of Availability Search
Duplicate>>");
this.selenium.close();
this.selenium.stop();
}


but still no progress.... :(

Nipun

unread,
Nov 7, 2011, 7:15:42 AM11/7/11
to testng-users
Hi Simon,

Thanks a lot for your support,
If i am not worng, i have set the base url on the selenium object
instance...

First Class:
----------------------

public class AvailabilitySearch {

public Selenium selenium;
public SeleniumServer seleniumServer;

@BeforeSuite
public void beforeSuite() throws Exception {
seleniumServer = new SeleniumServer();
seleniumServer.start();
System.out.println("<<in the Before Suite of Availability
Search>>");
}

@BeforeTest
@Parameters({ "selenium.host", "selenium.port",
"selenium.browser","selenium.url" })
public void beforeTest(String host, String port, String browser,
String url)throws Exception {
System.out.println("<<in the Before Test of Availability Search>>");
this.selenium = new DefaultSelenium(host,
Integer.parseInt(port),browser, url);
this.selenium.start();
this.selenium.open(url);

}

@Test
public void testAvailability(){
System.out.println("<<in the @Test of Availability Search>>");
selenium.open("/reservation/ibe/ibe/booking?locale=en_US");
System.out.println("IBE loaded...");
Assert.assertEquals("Far East", selenium.getTitle());
}




@AfterTest
public void afterTest() {
System.out.println("<<in the After Test of Availability Search>>");
this.selenium.close();
this.selenium.stop();

}


Second Class:
-------------------------

public class AvailabilitySearchDuplicate {

public Selenium selenium;
public SeleniumServer seleniumServer;

/*@BeforeSuite
public void beforeSuite() throws Exception {
System.out.println("<<in the Before Suite of Availability Search
Duplicate>>");
seleniumServer = new SeleniumServer();
seleniumServer.start();

}*/

@BeforeTest
@Parameters({ "selenium.host", "selenium.port",
"selenium.browser","selenium.url" })
public void beforeTest(String host, String port, String browser,
String url)throws Exception {
System.out.println("<<in the Before Test of Availability Search
Duplicate>>");
this.selenium = new DefaultSelenium(host,
Integer.parseInt(port),browser, url);
this.selenium.start();
this.selenium.open(url);

}

@Test
public void testAvailabilityDuplicate()throws Exception{
System.out.println("<<in the @Test of Availability Search
Duplicate>>");
AvailabilitySearch search = new AvailabilitySearch();
search.testAvailability();

}


@AfterTest
public void afterTest() {
System.out.println("<<in the After Test of Availability Search
Duplicate>>");
this.selenium.close();
this.selenium.stop();
}


But still it is not executing the selenium.open when the method is
called from the second class.... :(

On Nov 7, 4:38 pm, Simon Knott <knott.si...@gmail.com> wrote:

Simon Knott

unread,
Nov 7, 2011, 7:35:16 AM11/7/11
to testng...@googlegroups.com
Hi Nipun,

I suggest you add logging to all of the BeforeTest and BeforeSuite methods to try and understand how TestNG is calling your different methods.

If you're just creating an instance of AvailabilitySearch through its constructor, it will be a different object to that which has been instantiated by TestNG.  The former will not have called the @BeforeSuite and @BeforeTest methods of AvailabilitySearch and therefore your selenium object won't exist - I assume you're still getting a NullPointerException.

Cheers,
Simon

Nipun

unread,
Nov 7, 2011, 9:28:02 AM11/7/11
to testng-users
Yes Simon, I am getting Null Pointer Exception while executing this
script... :)
I shall try as us said... Thanks again....

Nipun

unread,
Nov 7, 2011, 2:10:01 PM11/7/11
to testng-users
Hi Simon,

I am relatively new to testNg and Selenium.... finding it hard to call
methods....
can you please give me a sample code on how to do this....
I appreciate your support a lot.... God Bless....

Nipun

unread,
Nov 8, 2011, 4:34:11 AM11/8/11
to testng-users
Any one please help...

Filus

unread,
Nov 8, 2011, 10:58:34 AM11/8/11
to testng-users
On 8 Lis, 10:34, Nipun <nipu...@gmail.com> wrote:
> Any one please help...

But what is your intention? Because for me it seems, no offence, that
problem is with design of your tests, not Selenium or TestNg. Why do
you want to call this testAvailability() method from other methods?

--
Filus

Jason McSwain

unread,
Nov 8, 2011, 11:25:21 AM11/8/11
to testng...@googlegroups.com
I don't see any problem with testng.  please look at this generic code here.

========================================
package org.example;

import org.testng.annotations.Test;

public class TestClassA {

    @Test
    public void testMethodA() {
        System.out.println("testMethodA - 1");
        TestClassB bbb = new TestClassB();
        bbb.testMethodB();
        System.out.println("testMethodA - 2");
    }

}
========================================
package org.example;

import org.testng.annotations.Test;

public class TestClassB {

    @Test
    public void testMethodB() {
        System.out.println("testMethodB");
    }

}
========================================
when TestClassA is invoked via testng, you get the following output

testMethodA - 1
testMethodB
testMethodA - 2
PASSED: testMethodA
========================================

Since this works, i'm guessing that your problem is with Selenium usage, and you should maybe ask the selenium group.

I hope this helps,
Thanks,
-Jason-




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


Cédric Beust ♔

unread,
Nov 8, 2011, 12:27:40 PM11/8/11
to testng...@googlegroups.com
I thought someone had already explained what's going on: you're initializing your object in a @Before method, so when you instantiate the class and call the test method yourself, it hasn't been initialized since nobody called these before methods.

-- 
Cédric




Reply all
Reply to author
Forward
0 new messages