Issue selenium code maintenance

109 views
Skip to first unread message

Raja sankar

unread,
Mar 16, 2010, 4:52:33 AM3/16/10
to seleniu...@googlegroups.com

I want to group the common methods in one file and use it. For example, login to a page using selenium may be used in multiple times. Define that in class A and call it in class B. However, it throws null pointer exception.

class A has

public void test_Login() throws Exception
   
{
       
try{
        selenium
.setTimeout("60000");
        selenium
.open("http://localhost");
        selenium
.windowFocus();
        selenium
.windowMaximize();
        selenium
.windowFocus();
        selenium
.type("userName", "admin");
        selenium
.type("password", "admin");
       
Result=selenium.isElementPresent("//input[@type='image']");
        selenium
.click("//input[@type='image']");
        selenium
.waitForPageToLoad(Timeout);
       
}
       
catch(Exception ex)
       
{  
           
System.out.println(ex);
            ex
.printStackTrace();
       
}
   
}

with all other java syntax

in class B

public void test_kk() throws Exception
   
{

       
try
       
{
            a
.test_Login();
       
}
       
catch(Exception ex)
       
{
           
System.out.println(ex);
            ex
.printStackTrace();
       
}
   
}

with all syntax.

When I execute class B, I got this error,

java.lang.NullPointerException
        at A
.test_Login(A.java:32)
        at B
.test_kk(savefile.java:58)
        at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun
.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun
.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java
.lang.reflect.Method.invoke(Unknown Source)
        at junit
.framework.TestCase.runTest(TestCase.java:168)
        at junit
.framework.TestCase.runBare(TestCase.java:134)
        at com
.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.j
ava
:212)
        at junit
.framework.TestResult$1.protect(TestResult.java:110)
        at junit
.framework.TestResult.runProtected(TestResult.java:128)
        at junit
.framework.TestResult.run(TestResult.java:113)
        at junit
.framework.TestCase.run(TestCase.java:124)
        at junit
.framework.TestSuite.runTest(TestSuite.java:232)
        at junit
.framework.TestSuite.run(TestSuite.java:227)
        at junit
.textui.TestRunner.doRun(TestRunner.java:116)
        at junit
.textui.TestRunner.doRun(TestRunner.java:109)
        at junit
.textui.TestRunner.run(TestRunner.java:77)
        at B
.main(B.java:77)


Let me know how to resolve this one.

Rajasankar



Raja sankar

unread,
Mar 17, 2010, 1:19:18 AM3/17/10
to seleniu...@googlegroups.com
As my previous mail is not explicitly clear, I will rephrase my post.

If some sequences of actions needs to done repeatedly in various places, I want to have that actions under a method, call that method wherever needed.

for example take the login method below. In every suite, instead writing the entire code, I want to have that in a class file say A and call that in class B, class C etc.

login()
{
   open webpage
   enter username
   enter password
   click submit
}

Class A contains that following,

import com.thoughtworks.selenium.*;
import junit.framework.*;
public class A extends SeleneseTestCase
{

   
public void test_Login() throws Exception
    {
        try{
        selenium.setTimeout("60000");
        selenium.open("http://localhost/LoginPage.do");

        selenium.windowFocus();
        selenium.windowMaximize();
        selenium.windowFocus();
        selenium.type("userName", "admin");
        selenium.type("password", "admin");
        selenium.click("//input[@type='image']");
         }
        catch(Exception ex)
        {   
            ex.printStackTrace();
        }
        }
    public static void main(String args[])
    {
    }
}


Class B contains the following

import com.thoughtworks.selenium.*;
import junit.framework.*;
import com.A.A;

public class B extends SeleneseTestCase
{
    A a = new A();
   
    public void setUp() throws Exception
    {
        setUp("http://localhost/", "*iexplore");

    }
    public void test_kk() throws Exception
    {
        try
        {
            a.test_Login();
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            ex.printStackTrace();
        }
    }
    public static Test suite()
    {
        return new TestSuite(B.class);
    }
    public static void main(String args[])
    {
              
        junit.textui.TestRunner.run(suite());

    }
}


When I execute class B, I got this error,

java.lang.NullPointerException
        at A
.test_Login(A.java:32)
        at B
.test_kk(savefile.java:58)
        at sun
.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun
.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun
.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java
.lang.reflect.Method.invoke(Unknown Source)
        at junit
.framework.TestCase.runTest(TestCase.java:168)
        at junit
.framework.TestCase.runBare(TestCase.java:134)

        at com
.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)

        at junit
.framework.TestResult$1.protect(TestResult.java:110)
        at junit
.framework.TestResult.runProtected(TestResult.java:128)
        at junit
.framework.TestResult.run(TestResult.java:113)
        at junit
.framework.TestCase.run(TestCase.java:124)
        at junit
.framework.TestSuite.runTest(TestSuite.java:232)
        at junit
.framework.TestSuite.run(TestSuite.java:227)
        at junit
.textui.TestRunner.doRun(TestRunner.java:116)
        at junit
.textui.TestRunner.doRun(TestRunner.java:109)
        at junit
.textui.TestRunner.run(TestRunner.java:77)
        at B
.main(B.java:77)


Rajasankar

Nirmal

unread,
Mar 18, 2010, 3:06:58 PM3/18/10
to Selenium Users
Where's your selenium object??

darrell

unread,
Mar 19, 2010, 11:28:54 PM3/19/10
to Selenium Users
The class A extends SeleneseTestCase. By doing this, there is an
instance of Selenium associated with class A. The selenium in class A
is never initialized. The class B also extends SeleneseTestCase. By
doing this, there is a SECOND instance of Selenium and it is
associated with class B. In class B you have a setUp() method which
calls the SeleneseTestCase.setUp() method. This initializes the
selenium associated with class B. The selenium associated with class A
is still null.

In class B you call a.test_Login(); but the test_Login() method will
call the selenium associated with class A, which is null. This results
in a NullPointerException.

The solution is to pass the selenium from class B to class A. I would
create a constructor in class A:

public A(Selenium selenium) {
super.selenium = selenium;
}

In class B I would have:

A a;

public void setUp() {
setUp("http://localhost", "*iexplore");
a = new A(super.selenium);
}

This will set the selenium associated with class A to be the same as
the selenium in class B.

Darrell

Raja sankar

unread,
Mar 22, 2010, 2:08:28 AM3/22/10
to seleniu...@googlegroups.com
Darrell,

Thanks for the help. I will try this and let you.

Rajasankar


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


Reply all
Reply to author
Forward
0 new messages