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



在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)

请建议我如何在同一类的所有方法中使用单个显式等待对象。

以下是我解决问题的方法:

  1. 在初始化驱动程序之前创建了等待

静态WebDriverWait等待

  1. 创建驱动程序并对其进行初始化

WebDriver驱动程序=新ChromeDriver((

wait=新WebDriverWait(驱动程序,10(

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

最新更新