I am using Selenium+JUnit+Eclipse I have 3 classes in 3 packages. Test class as A(in default Package), Activity class as B(In activity package), Repository class as C(in objectRepository package).
If i do all activities in class A then its working fine. But if I separate the activities in class B and calling classB methods in classA then its throwing java.lang.NullPointerException error...
Code for ClassA.java
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
import org.junit.Test;
import activityPkg.ClassB;
public class ClassA extends TestCase {
ClassB objB = new ClassB();
public void setUp() throws Exception
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://www.google.com");
selenium.start();
selenium.windowFocus()开发者_运维知识库;
selenium.windowMaximize();}
@Test
public void testA() throws Exception
{
selenium.open("/");
try
{
Thread.sleep(5000);
String result = objB.MethodB();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Code for ClassB.java
package activityPkg;
import com.thoughtworks.selenium.Selenium;
public class RegressionTools {
Selenium selenium;
ObjectRepository objRep = new ObjectRepository();
public String MethodB() throws Exception
{
String value=null;
try
{
selenium.start();
if(selenium.isElementPresent("//input[@name='btnG' and @value='Google Search']"))
{
System.out.println("Element is present");
value = pass;
}
else
{
System.out.println("Element is not present");
value = Fail;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return value;
}
}
But everytime its stopping from IF condition of MethodB and coming to catch block. Why it's not even entering into IF or ELSE condition. Did I miss anything there?
you don't have any selenium instance in your class B. I guess the exception you have is NullPointerException
You start your selenium instance in your first class, and got no reference pointing to it in your class B
Good practice is to have a SeleniumFixture
utility which holds a reference to the started instance and manage the lifecycle of the selenium server during the test suite
精彩评论