Home > Software engineering >  I am trying to declare explicit wait in my constructor, but getting error incompatible types. How to
I am trying to declare explicit wait in my constructor, but getting error incompatible types. How to

Time:10-05

package PageObject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Amazon {

    WebDriver driver;
    WebDriver wait;

    public Amazon(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
        wait = new WebDriverWait(driver, 20);
    }

    @FindBy(xpath = "//input[@type = 'text']")
    WebElement search;

This is my code and i am getting error in the line wait = WebDriverWait(driver, 20); And the error is Incompatible Type, Required : org.openqa.selenium.WebDriver, Found : org.openqa.selenium.support.ui.WebDriverWait Have tried this in previous code but never came across an error like this. Have imported the necessary libraries also, but not able to solve. If anyone can help to solve the error. I don't want to use Fluent wait or thread.sleep. I want the error to solve with explicit wait itself

CodePudding user response:

This

WebDriver wait;

should be

WebDriverWait wait;

WebDriverWait is a class, and you are creating object of that class like this, so you should declare the same class.

wait = new WebDriverWait(driver, 20);

Full code should be :

package PageObject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Amazon {

    WebDriver driver;
    WebDriverWait wait;

    public Amazon(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
        wait = new WebDriverWait(driver, 20);
    }

    @FindBy(xpath = "//input[@type = 'text']")
    WebElement search;

CodePudding user response:

Please change like below. wait should be declared as WebDriverWait not WebDriver

WebDriver driver;
WebDriverWait wait;

    public Amazon(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
        wait = new WebDriverWait(driver, 20);
    }
  • Related