1. <legend id='zIBL4'><style id='zIBL4'><dir id='zIBL4'><q id='zIBL4'></q></dir></style></legend>
  2. <tfoot id='zIBL4'></tfoot>

  3. <small id='zIBL4'></small><noframes id='zIBL4'>

      <bdo id='zIBL4'></bdo><ul id='zIBL4'></ul>
    <i id='zIBL4'><tr id='zIBL4'><dt id='zIBL4'><q id='zIBL4'><span id='zIBL4'><b id='zIBL4'><form id='zIBL4'><ins id='zIBL4'></ins><ul id='zIBL4'></ul><sub id='zIBL4'></sub></form><legend id='zIBL4'></legend><bdo id='zIBL4'><pre id='zIBL4'><center id='zIBL4'></center></pre></bdo></b><th id='zIBL4'></th></span></q></dt></tr></i><div id='zIBL4'><tfoot id='zIBL4'></tfoot><dl id='zIBL4'><fieldset id='zIBL4'></fieldset></dl></div>
    1. Java 和 Selenium:页面对象中的静态方法

      时间:2023-09-28
    2. <legend id='hT5rK'><style id='hT5rK'><dir id='hT5rK'><q id='hT5rK'></q></dir></style></legend>

        <i id='hT5rK'><tr id='hT5rK'><dt id='hT5rK'><q id='hT5rK'><span id='hT5rK'><b id='hT5rK'><form id='hT5rK'><ins id='hT5rK'></ins><ul id='hT5rK'></ul><sub id='hT5rK'></sub></form><legend id='hT5rK'></legend><bdo id='hT5rK'><pre id='hT5rK'><center id='hT5rK'></center></pre></bdo></b><th id='hT5rK'></th></span></q></dt></tr></i><div id='hT5rK'><tfoot id='hT5rK'></tfoot><dl id='hT5rK'><fieldset id='hT5rK'></fieldset></dl></div>
      • <small id='hT5rK'></small><noframes id='hT5rK'>

              <tbody id='hT5rK'></tbody>

              <bdo id='hT5rK'></bdo><ul id='hT5rK'></ul>

              1. <tfoot id='hT5rK'></tfoot>
                本文介绍了Java 和 Selenium:页面对象中的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                当我尝试在页面对象中使用静态方法时,我遇到了 NullPointerExceptions 问题.如果我使用非静态方法,它工作正常.

                I'm having trouble with NullPointerExceptions when I try using static methods in a page object. If I do it with non-static methods, it works fine.

                非静态版本:

                public class ComplaintPage {
                
                    private ExtendedWebDriver driver;
                
                    @FindBy(css = "[data-selector=date-received-complaint]")
                    public WebElement dateComplaintReceoved;
                
                    public ComplaintPage() {
                        driver = Browser.extendedDriver();
                        PageFactory.initElements(driver, this);
                    }
                
                    public void setComplaintDate() {
                        dateComplaintReceoved.sendKeys(LocalDate.now().toString());
                    }
                }
                
                Calling code:
                
                ComplaintPage complaintPage = new ComplaintPage;
                complaintPage.setComplaintDate();
                

                这很好用.日期字段已设置.

                This works fine. The date field is set.

                静态版

                public class ComplaintPage {
                
                    private static ExtendedWebDriver driver;
                
                    @FindBy(css = "[data-selector=date-received-complaint]")
                    public static WebElement dateComplaintReceoved;
                
                    public ComplaintPage() {
                        driver = Browser.extendedDriver();
                        PageFactory.initElements(driver, this);
                    }
                
                    public void static setComplaintDate() {
                *       dateComplaintReceoved.sendKeys(LocalDate.now().toString());
                    }
                }
                
                Calling code:
                
                ComplaintPage.setComplaintDate();
                

                这不起作用,并在标有*"的行(访问 WebElement 的行)上导致 java.lang.NullPointerException.

                This does not work, and results in a java.lang.NullPointerException on the line marked with "*" (the line accessing the WebElement).

                我有点喜欢在测试中使用像这样的静态方法,因为我真的看不出它有什么问题,而且它使代码更易于阅读.我以前在 C#/VS 中做过,但由于某种原因,我在这里遗漏了一些重要的东西.

                I kind of like using static methods like this in test, since I don't really see a problem with it, and it makes the code even more easy to read. And I've done it before, in C#/VS, but for some reason I'm missing something important here.

                推荐答案

                NullPointerException 因为 PageFactory 的工作方式而被抛出.你看,当你创建一个 ComplaintPage 类的实例时,你正在调用它的构造函数:

                NullPointerException is thrown because of how PageFactory works. You see, when you create an instance of ComplaintPage class, you are invoking its constructor:

                public ComplaintPage() {
                        driver = Browser.extendedDriver();
                        PageFactory.initElements(driver, this);
                    }
                

                构造函数调用PageFactory类的initElements方法.此方法使用 Java 反射 API 初始化所有 WebElementList> 字段.它基本上将默认的 null 值更改为接口的实现.它还提供了一种 WebElement 的惰性实例化,这意味着 - WebElement 仅在需要时才被发现(寻找?) - 当您调用它们的操作时.

                The constructor calls initElements method of PageFactory class. This method initializes all of WebElement and List<WebElement> fields with Java Reflection API. It basically changes the default null values to implementations of the interface. It also provides sort of Lazy instantiation of the WebElement which means - WebElements are found (looked for?) only when needed - when you invoke operations on them.

                当您创建静态方法和静态 WebElements 时 - 您没有调用类的构造函数,这导致不调用 initElements 方法.

                When you created static methods and static WebElements - you did not call the constructor of the class, which lead to NOT invoking the initElements method.

                使用 @FindBy 注释的所有元素均未初始化.这就是为什么将 PageFactory 与静态方法一起使用并不是一个好主意.

                All of the elements annotated with @FindBy were not initialized. That's why it's not a good idea to use PageFactory with static methods.

                您可以在静态方法中找到带有经典 driver.findElement 的元素,而不是使用 PageFactory.

                Instead of using PageFactory you can just find the element with classic driver.findElement inside the static method.

                    public void static setComplaintDate(WebDriver driver) {
                        driver.findElement(By.cssSelector("[data-selector=date-received-complaint]")).sendKeys(LocalDate.now().toString());
                    }
                

                这篇关于Java 和 Selenium:页面对象中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                • <small id='Y66yk'></small><noframes id='Y66yk'>

                    <bdo id='Y66yk'></bdo><ul id='Y66yk'></ul>
                    <tfoot id='Y66yk'></tfoot>

                      1. <i id='Y66yk'><tr id='Y66yk'><dt id='Y66yk'><q id='Y66yk'><span id='Y66yk'><b id='Y66yk'><form id='Y66yk'><ins id='Y66yk'></ins><ul id='Y66yk'></ul><sub id='Y66yk'></sub></form><legend id='Y66yk'></legend><bdo id='Y66yk'><pre id='Y66yk'><center id='Y66yk'></center></pre></bdo></b><th id='Y66yk'></th></span></q></dt></tr></i><div id='Y66yk'><tfoot id='Y66yk'></tfoot><dl id='Y66yk'><fieldset id='Y66yk'></fieldset></dl></div>
                        <legend id='Y66yk'><style id='Y66yk'><dir id='Y66yk'><q id='Y66yk'></q></dir></style></legend>
                          <tbody id='Y66yk'></tbody>