简体   繁体   English

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

[英]How to use explicit wait object in different methods from the same class?

In a selenium java program, I created an explicit wait condition inside the class.在 selenium java 程序中,我在类中创建了一个显式的等待条件。 But unable to call this wait object in different methods from the same class.但是无法在同一类的不同方法中调用此等待对象。

Below is the code I have written:下面是我写的代码:

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...");

    }

}

When I try to execute, it is giving the below error:当我尝试执行时,它给出了以下错误:

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)

Please suggest me how to used single explicit wait object in all the methods from same class.请建议我如何在同一类的所有方法中使用单个显式等待对象。

Here is what i did to solve the issue:这是我为解决问题所做的工作:

  1. Created wait before initialization of driver.在驱动程序初始化之前创建等待。

static WebDriverWait wait;静态 WebDriverWait 等待;

  1. Created driver and initialized it.创建驱动程序并初始化它。

WebDriver driver = new ChromeDriver(); WebDriver 驱动程序 = new ChromeDriver();

wait = new WebDriverWait(driver, 10);等待=新的WebDriverWait(驱动程序,10);

  1. Then used wait in my entire code including all methods from the same class.然后在我的整个代码中使用 wait ,包括来自同一类的所有方法。

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

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