简体   繁体   English

尝试从 java 类中获取值返回 null

[英]Trying to get value from a java class returns null

I am trying to create a java file that will include all the IDs I'll need to call in my selenium with java project, but each time I call one of the values I get the message that it is null.我正在尝试创建一个java文件,其中将包含我需要在我的 selenium with java 项目中调用的所有 ID,但每次我调用其中一个值时,我都会收到它为空的消息。

Cannot read field "username" because "testScenarios.Actions.testStepActions.testIds" is null无法读取字段“用户名”,因为“testScenarios.Actions.testStepActions.testIds”为空

my class is:我的课是:

public Class testIds{
   @FindBy (id="username")
   public WebElement username;
}

and the way i call it in my code is:我在代码中调用它的方式是:

public static void iEnterUsername(){
   testIds.username.sendKeys("username");
}

I am sure that I imported the class in the file I am trying to call but I am not entirely sure what I am doing wrong and few searches didn't bear fruit.我确信我在我试图调用的文件中导入了类,但我不完全确定我做错了什么,而且很少有搜索没有结果。

Can you please help in what I am doing wrong?你能帮我解决我做错了什么吗?

You have to understand the concept of static in Java.您必须了解Java中的静态概念。

A static method is a method that belongs to a class but does not belong to an instance of that class and this method can be called without the instance or object of that class.静态方法是属于某个类但不属于该类的实例的方法,并且可以在没有该类的实例或对象的情况下调用该方法。

Here you are accessing non-static method staticly.在这里,您正在静态访问非静态方法。

public static void iEnterUsername(){
   testIds.username.sendKeys("username");
}

Also, you are using PageFactory, so you need to use PageFactory. initElements()此外,您正在使用 PageFactory,因此您需要使用PageFactory. initElements() PageFactory. initElements() static method which takes the driver instance of the given class and the class type, and returns a Page Object with its fields fully initialized. PageFactory. initElements()静态方法,它采用给定类的驱动程序实例和类类型,并返回一个页面对象,其字段已完全初始化。

Solution:解决方案:

Using Static Approach: Bad Approach Not Recommended使用静态方法:不推荐使用不好的方法

You have to create a constructor and pass the driver instance to it, also initializing PageFactory.initElements(driver, this);您必须创建一个构造函数并将驱动程序实例传递给它,同时初始化PageFactory.initElements(driver, this);

public Class testIds{ WebDriver driver;公共类 testIds{ WebDriver 驱动程序;

   public testIds (WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
   }


   @FindBy (id="username")
   public static WebElement username;

}

In Test Class:在测试类:

public static void iEnterUsername(){
       testIds t = testIds(driver);
       testIds.username.sendKeys("username");
    }

Other Solution: Standard and Correct Approach其他解决方案:标准和正确的方法

public Class testIds{
   WebDriver driver;

   public testIds (WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
   }


   @FindBy (id="username")
   public WebElement username;

   public WebElement getUserName(){
    return username;
  }
}

Test Class:测试类:

public static void iEnterUsername(){
   testIds t = testIds(driver);
   testIds.getUserName().sendKeys("username");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM