Search code examples
appium

Cannot invoke "io.appium.java_client.android.AndroidDriver.manage()" because "this.driver" is null


I am new to Appium and just getting started, I would like to call methods from other classes by creating an object. but when I call methods it show "this.driver" is null. how to call methods from other class by creating object?? is it possible?? Noted that: I already use 'extends class', so I didn't use it in multiple classes.

//Here, I call methods from other classes......
 import java.net.MalformedURLException;
 import java.time.Duration;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
//import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
import static java.time.Duration.ofSeconds;

import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

@Test(priority = 11)
public void IncompleteTask() {

    
    String ser="Update For Testing";
    Searching(ser);
    AllMethods scr=new AllMethods(driver);
    scr.Scroll();
    //Scroll();
    Point point1=driver.findElementByXPath("//android.widget.TextView[@text='Update For Testing']").getCenter();
    Point point2=driver.findElementByXPath("//android.widget.TextView[@text='Update For Testing']").getLocation();                  
    Swipping(point1,point2);
    
    driver.findElementById("bd.com.cslsoft.kandareeliteapp:id/ll_reAssign").click();
    //driver.findElementById("bd.com.cslsoft.kandareeliteapp:id/noButton").click();
    String mass=driver.findElementById("bd.com.cslsoft.kandareeliteapp:id/tvMessage").getText();
    driver.findElementById("bd.com.cslsoft.kandareeliteapp:id/yesButton").click();
    
    String mText="Are you sure you want to undo this Task?";
    if(mass.contains(mText))
    {
        driver.findElementById("bd.com.cslsoft.kandareeliteapp:id/yesButton").click();
            
    }

    System.out.println("IncompleteTask Executed!");
    driver.quit();

}

//Here is my allMethods class

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.support.PageFactory;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;

import java.io.IOException;
import java.lang.NullPointerException;
public class AllMethods  {
   public AndroidDriver<AndroidElement>  driver;
   public AllMethods(AndroidDriver driver)
   {
    PageFactory.initElements(driver, this);
   }

   public void searching(String ser) {
       driver.findElementById("bd.com.cslsoft.kandareeliteapp:id/ll_search").click(); 
       driver.findElementById("android:id/search_src_text").sendKeys(ser);  
       driver.hideKeyboard();
       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

public void Scroll() throws NullPointerException
{
    for(int i=0; i<3; i++)
    {
        
        Dimension dimension=driver.manage().window().getSize();
        int start_x=(int) (dimension.width*0.5);
        int start_y=(int) (dimension.height*0.2);
        
        int end_x=(int) (dimension.width*0.5);
        int end_y=(int) (dimension.height*0.8);
        
        TouchAction tcD=new TouchAction(driver);
        tcD.press(PointOption.point(start_x, 
          start_y)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
        .moveTo(PointOption.point(end_x, end_y)).release().perform();
    }

  }
 }

Solution

  • The methods in AllMethods class are referring to the driver which is not yet initialized. i.e they are referring to this driver : public AndroidDriver<AndroidElement> driver;

    You will need to add this.driver=driver inside AllMethods constructor, this will set the driver with the reference of the driver that is passed in the constructor.