0

Launching Application from One Class and using it in another class.

Hi All,

I am new to selenium and Java. I just trying to work on selenium. I am facing some problem. I wanted to create a class in which I just want to launch application(say google.com), and the next other tasks like executing test cases has to be done by other class. Can this be done, Here I am pasting my code. Help me please in understanding this.

Here are my two classes

This class Launches Google on browser.

public class GoogleTest extends SeleneseTestCase
{
    private static Selenium selenium;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception 
{
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
        selenium.start();
        selenium.windowMaximize();
    }

    @Test
    public void testSearch() throws InterruptedException
{
      selenium.open("http://www.google.com");
      selenium.waitForPageToLoad("50000");    
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        selenium.stop();
    }
}

This class should type in search field

public class FirstSeleniumTest extends GoogleTest
{         private static Selenium selenium;

    @Test
    public void testSearch()
{
  selenium.type("q", "Selenium OpenQA");
      selenium.waitForPageToLoad("50000");
    }
}

When I run "GoogleTest" first class it runs perfectly, when I try to run "FirstSeleniumTest" second class it gives error like this. FAILED: testSearch on null(com.dev.rao.FirstSeleniumTest) java.lang.NullPointerException at com.dev.rao.FirstSeleniumTest.testSearch(FirstSeleniumTest.java:18)

1 Answers1

0

Its normally difficult to do what you are trying to achieve. That's why new Selenium instance is better per test. and in the longer run easy to debug. But as per what you are trying I see two issues:

1.) selenium.stop() should be in the second script not the first. If you kill the instance how can it be extended in 2nd test.

2.) Declaring Selenium twice. (I think probably Selenium instance in 2nd test should also extend from first test.)

Manpreet Singh
  • 3,643
  • 2
  • 18
  • 14