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

        <small id='O4sNx'></small><noframes id='O4sNx'>

      1. <legend id='O4sNx'><style id='O4sNx'><dir id='O4sNx'><q id='O4sNx'></q></dir></style></legend>

        Java 和 Selenium:页面对象中的静态方法

        Java and Selenium: Static methods in Page Objects(Java 和 Selenium:页面对象中的静态方法)
            <tbody id='OZT3g'></tbody>
          <tfoot id='OZT3g'></tfoot>
            • <bdo id='OZT3g'></bdo><ul id='OZT3g'></ul>
              1. <small id='OZT3g'></small><noframes id='OZT3g'>

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

                  本文介绍了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:页面对象中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lucene Porter Stemmer not public(Lucene Porter Stemmer 未公开)
                  How to index pdf, ppt, xl files in lucene (java based or python or php any of these is fine)?(如何在 lucene 中索引 pdf、ppt、xl 文件(基于 java 或 python 或 php 中的任何一个都可以)?)
                  KeywordAnalyzer and LowerCaseFilter/LowerCaseTokenizer(KeywordAnalyzer 和 LowerCaseFilter/LowerCaseTokenizer)
                  How to search between dates (Hibernate Search)?(如何在日期之间搜索(休眠搜索)?)
                  How to get positions from a document term vector in Lucene?(如何从 Lucene 中的文档术语向量中获取位置?)
                  Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)
                      <tfoot id='qRcfj'></tfoot>
                      <legend id='qRcfj'><style id='qRcfj'><dir id='qRcfj'><q id='qRcfj'></q></dir></style></legend>

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

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

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