appium / java-client

Java language binding for writing Appium Tests, conforms to W3C WebDriver Protocol
Apache License 2.0
1.22k stars 757 forks source link

Exception is Cannot invoke "io.appium.java_client.android.AndroidDriver.navigate()" because "this.driver" is null #2197

Closed Aman782739 closed 3 months ago

Aman782739 commented 3 months ago

Description

Create a maven project by using testng framework, try to run two test scripts together, first test case run successfully but when the second test case runs it gives error message.

Code To Reproduce Issue [ Good To Have ]

Base class - package com.Serviceapp.Testcase;

import java.awt.SplashScreen; import java.net.MalformedURLException; import java.net.URL;

import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.junit.runners.Parameterized.BeforeParam; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Optional; import org.testng.annotations.Parameters;

import com.Serviceapp.Utilities.Readconfiguration;

import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.remote.MobileCapabilityType;

public class BaseClass {

public AndroidDriver driver;
public static Logger logger;

Readconfiguration readconfig = new Readconfiguration();

public String number = Readconfiguration.getnumber();
public String number2 = Readconfiguration.getnumber2();

@BeforeTest
@Parameters("browser")
public <AndroidElement> void setup(@Optional("Android") String br) throws MalformedURLException {
    logger = Logger.getLogger("Baseclass.class");

    PropertyConfigurator.configure("src/main/resources/log4j.properties");

    DesiredCapabilities dc = new DesiredCapabilities();

    dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
    dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "14");
    dc.setCapability(MobileCapabilityType.DEVICE_NAME, "63fc090e");
    dc.setCapability("appPackage", "com.kentcustomerapp.com");
    dc.setCapability("appActivity", "com.kentcustomerapp.view.activity.SplashActivityKT");
    dc.setCapability(MobileCapabilityType.NO_RESET, true);
    System.out.println("Driver start");
    logger.info("Build has been run successfully");

    // dc.setCapability(MobileCapabilityType.APP, "C:\\Users\\atyagi\\OneDrive -
    // KROSL\\Desktop\\Automated Build\\Practice_Builds\\Service app.apk");

// URL url = new URL("http://127.0.0.1:4723/wd/hub");

    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), dc);
    //System.out.print(SplashScreen);

}

@AfterTest
public void teardown() {
    System.out.println("driver close");
    /*if (driver != null) {
        driver.quit();
    }
    logger.info("Driver closed");*/
}

}

Test case 1

package com.Serviceapp.Testcase;

import java.io.IOException; import java.io.IOException; import java.time.Duration;

import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.Test;

import com.Serviceapp.Pageobjects.Login_Page;

import com.Serviceapp.Utilities.Readconfiguration;

import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver;

public class TC_001_Login extends BaseClass {

@Test(groups = { "Sanity", "Regression" })
public void Enternumber() throws InterruptedException {
    System.out.println("Test1");
    Login_Page lp = new Login_Page(driver);
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5000));

    try {
        Thread.sleep(5000);

        // System.out.println(driver.findElement(By.id("com.kentcustomerapp.com:id/log_in")).getText());
        lp.loginpage();

        logger.info("user comes in login page");
        lp.setmobilenumberfield(number);
        // driver.findElement(By.id("com.kentcustomerapp.com:id/ed_mobile_number")).sendKeys(number);
        logger.info("user has enter number successfully");
        lp.setsubmitbuttononlogin();
        logger.info("User click on submit button successfully");
        //Thread.sleep(10000);
        lp.VerifyOTPPage();
        logger.info("User redirect on verify OTP page successfully");
    } catch (Exception e) {
        System.out.println("Exception is " + e.getMessage());
    }
}

}

Test case 2

package com.Serviceapp.Testcase;

import java.io.IOException; import java.io.IOException; import java.time.Duration;

import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test;

import com.Serviceapp.Pageobjects.Login_Page; import com.Serviceapp.Pageobjects.Verify_OTP_Page; import com.Serviceapp.Utilities.Readconfiguration;

import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver;

public class TC_002_VerifyOTP extends BaseClass {

@Test(groups = { "Regression", "Sanity" })
public void changenumber() {
    System.out.println("Test2");
    Verify_OTP_Page vp = new Verify_OTP_Page(driver);
     //Login_Page lp = new Login_Page(driver);
    try {
        //lp.driver.navigate().back();
        driver.navigate().back();
        logger.info("its work");
        // System.out.println(driver.findElement(By.id("com.kentcustomerapp.com:id/tvEdit")).getText());

// vp.Change_number(); // logger.info("User click on number change option"); // lp.setmobilenumberfield(number2); // logger.info("user has enter number successfully"); // lp.setsubmitbuttononlogin(); // logger.info("User click on submit button successfully"); // //Thread.sleep(10000); // lp.VerifyOTPPage(); // logger.info("User redirect on verify OTP page successfully"); } catch (Exception e) { System.out.println("Exception is " + e.getMessage()); } }

}

Testng.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

Exception Stacktraces

[RemoteTestNG] detected TestNG version 7.7.1 SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details. Driver start 2024-07-10 23:49:02 INFO class:54 - Build has been run successfully Test1 2024-07-10 23:49:18 INFO class:37 - user comes in login page 2024-07-10 23:49:18 INFO class:40 - user has enter number successfully 2024-07-10 23:49:19 INFO class:42 - User click on submit button successfully 2024-07-10 23:49:43 INFO class:45 - User redirect on verify OTP page successfully Test2 Exception is Cannot invoke "io.appium.java_client.android.AndroidDriver.navigate()" because "this.driver" is null driver close

=============================================== Suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

itkhanz commented 3 months ago

This does not seem to be issue with Appium but rather your handling of driver in code. Ensure that driver is properly initialised at the start of test.

Also Please update to latest Appium Java Client and use Driver specific options classes to initiate driver session instead of Desired Capabilities, and there is no need to pass /wd/hub if you are running Appium Server 2+. See the migration guide:- https://github.com/appium/java-client/blob/master/docs/v7-to-v8-migration-guide.md https://github.com/appium/java-client/blob/master/docs/v8-to-v9-migration-guide.md