Java 如何在同一类的不同方法中使用显式等待对象?

Java 如何在同一类的不同方法中使用显式等待对象?,java,selenium-webdriver,Java,Selenium Webdriver,在SeleniumJava程序中,我在类中创建了一个显式的等待条件。但无法在同一类的不同方法中调用此等待对象 以下是我编写的代码: public class AutomationScenarioWithCustomWaits { public static WebDriverWait wait = new WebDriverWait(driver, 10); public static void main(String[] args) throws IOExce

在SeleniumJava程序中,我在类中创建了一个显式的等待条件。但无法在同一类的不同方法中调用此等待对象

以下是我编写的代码:

public class AutomationScenarioWithCustomWaits {

        public static WebDriverWait wait = new WebDriverWait(driver, 10);
        public static void main(String[] args) throws IOException {
            System.setProperty("webdriver.chrome.driver", "C:\\SDET_Certification_2020\\SDETCertification2020\\chromedriver.exe");
        login();
        clickPIMforActions();

    public static void login() throws IOException {

        driver = new ChromeDriver();
        driver.get("https://opensource-demo.orangehrmlive.com/");

        System.out.println("OrangeHRM is launched successfully...");
        System.out.println("");

         // Specify the path of file
          File src=new File("C:\\SDET_Certification_2020\\SDETCertification2020\\DataSet.xlsx");
         // load file**strong text**
         FileInputStream fis=new FileInputStream(src);
        // Load workb`enter code here`ook
        XSSFWorkbook wb=new XSSFWorkbook(fis);         
        // Load sheet- Here we are loading first sheetonly
        XSSFSheet sh1= wb.getSheetAt(0);

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("txtUsername"))).sendKeys(sh1.getRow(1).getCell(3).getStringCellValue());
        System.out.println("User name is entered successfully...");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("txtPassword"))).sendKeys(sh1.getRow(1).getCell(4).getStri`enter code here`ngCellValue());
        System.out.println("Password is entered successfully...");

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("btnLogin"))).click();
        System.out.println("Login Button is clicked..");
        System.out.println("User is in: " + driver.getTitle() + " page");
        System.out.println("");
    }

    public static void clickPIMforActions() {
//      WebDriverWait wait2 = new WebDriverWait(driver, 10);
        pim = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"menu_pim_viewPimModule\"]/b")));
        Actions actions = new Actions(driver);
        actions.moveToElement(pim).click().build();
        System.out.println("PIM menu is clicked...");

    }

}
当我尝试执行时,它给出以下错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:782)
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
    at seleniumAssignment_3.AutomationScenarioWithCustomWaits.<clinit>(AutomationScenarioWithCustomWaits.java:33)
线程“main”java.lang.ExceptionInInitializeError中的异常 原因:java.lang.NullPointerException 位于com.google.common.base.premissions.checkNotNull(premissions.java:782) 位于org.openqa.selenium.support.ui.FluentWait。(FluentWait.java:96) 位于org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71) 位于org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:45) 在seleniumAssignment_3.AutomationScenarioWithCustomWaits.(AutomationScenarioWithCustomWaits.java:33)
请建议我如何在同一类的所有方法中使用单个显式等待对象。

以下是我为解决此问题所做的:

  • 已创建驱动程序初始化前的等待
  • 静态WebDriverWait等待

  • 创建驱动程序并初始化它
  • WebDriver驱动程序=新的ChromeDriver()

    等待=新的WebDriverWait(驱动程序,10)

  • 然后在我的整个代码中使用wait,包括来自同一类的所有方法

  • “驱动程序”是否已初始化?WebDriverWait是否依赖于“webdriver.chrome.driver”(您在尝试创建WebDriverWait后初始化它)?
    public static WebDriverWait wait wait=new WebDriverWait(driver,10)它是静态的,这意味着它在程序开始时初始化。由于David和Fenio的原因,您的驱动程序当时是空的。。。我初始化了驱动程序初始化之前的等待,这是导致问题的原因。这就是我要解决的问题:1。驱动程序初始化前的板条箱等待变量。2.然后初始化驱动程序初始化后的等待。成功了。