0

I have superclass named BaseTest with code

public class BaseTest {
    public AndroidDriver<AndroidElement> driver;
    DesiredCapabilities cap;
    @BeforeTest
    @Parameters("device")
    public void SetUpForDriver(String device) throws MalformedURLException{
        if(device.equalsIgnoreCase("emulator")){
            cap = new DesiredCapabilities();
            cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
            cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.1");
            cap.setCapability(MobileCapabilityType.DEVICE_NAME, "SlingEmulator");
            //cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
            cap.setCapability("appPackage", "com.edmobilelabs.sling.dev");
            //cap.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.edmobilelabs.sling.ui.ScrSplash");
            cap.setCapability("appActivity", "com.edmobilelabs.sling.ui.ScrSplash");
            driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }
        else{
            cap = new DesiredCapabilities();
            cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
            cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "6.0");
            cap.setCapability(MobileCapabilityType.DEVICE_NAME, "28A4AA040IG4012");
            cap.setCapability("appPackage", "com.edmobilelabs.sling.dev");
            cap.setCapability("appActivity", "com.edmobilelabs.sling.ui.ScrSplash");
            driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        }
    }
}

My Ist Test class which I am able to execute successfully

public class LogInPageTest extends BaseTest {
    LogInPage log;

    @Test(priority=1)
    public void navigateToLoginPageTest() throws MalformedURLException{
        log = new LogInPage(driver);
        boolean isTestCaseVerified = log.navigateToLoginPage();
        Assert.assertTrue(isTestCaseVerified,"Test Case is Passed");
    }
}

My 2nd Test class In which I want to use driver object

public class HomePageTest extends BaseTest{

    @Test(priority=2)
    public void firstChatWithBots(){
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//android.widget.Button[@text='How Sling Works']")).click();
    }
}

I have two Test classes My test class where I perform the login operation passed successfully but in my 2nd Test Class not able to use my driver object

Can somebody what is going on with my code

abishek kachroo
  • 35
  • 1
  • 11
  • Can you please give me the link of the question so I can see – abishek kachroo Jun 12 '17 at 06:52
  • I dont think this is a duplicate of [this](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) question. The fix for this problem is to get rid of the "@BeforeTest" annotation and instead replace it with "@BeforeClass" annotation because "@BeforeTest" gets executed only once per "" tag. That explains why the second test always skips the initialisation. – Krishnan Mahadevan Jun 12 '17 at 07:03
  • Just by seeing Null Pointer doesn't mean that this is answered over there No such type of scenario is present over there So please read the question 1st and then mark it as duplicate – abishek kachroo Jun 12 '17 at 07:04
  • Please follow the recommendations that I have given in my comment. That will fix your problem. I have also briefly explained the cause of this. – Krishnan Mahadevan Jun 12 '17 at 07:05
  • @KrishnanMahadevan I have one question I am already logged in So if I use BeforeClass Then it would re-instantiate the whole app As per I think – abishek kachroo Jun 12 '17 at 07:08
  • Yes it would. If you don't want that to happen, then you would need to re-engineer your test such that this doesn't happen. But using "@BeforeTest" to initialise your webdriver is not going to work here. – Krishnan Mahadevan Jun 12 '17 at 07:11

0 Answers0