0

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();
    }

  }
 }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i facing another issue, suddenly my apps don't open by appium server but in the morning i am running it so many time. show--Encountered internal error running command: Error: Could not acquire lock on C:\Users\SYEDYU~1\AppData\Local\Temp\uia2_device_port_guard. Original error: Error: EEXIST: file already exists, open
try removing uia2_device_port_guard file from that location (take the backup of that file and then delete)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.