jacob1989
jacob1989

Reputation: 17

Why I am getting Selenium init error on constructor?

sorry, I don't know if I constructed the title properly. But the problem I have is that when I created page object Tile and instantiazed it on Test page it gives error just after opening the page. code below:

public class Tile {
    private WebDriver driver;
    private String title;
    private WebElement titleEl;

    public Tile(WebDriver driver) {
        this.driver = driver;
        this.titleEl = driver.findElement(By.xpath("//div[@id='xpathGoesHere']"));
        this.title = titleEl.getText();
     }

    public WebElement getTitleEl() {
        return titleEl;
    }

    public void setTitleEl(WebElement titleEl) {
        this.titleEl = titleEl;
    }

   public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

and there's a separate page for Tiles (as a component)

public class Tiles {
    private WebDriver driver;

    public Tiles(WebDriver driver) {
        this.driver = driver;
    }

another page:

public class ThePage extends BasePage {

    private final Tile tile;
    private final Tiles tiles;

    public ThePage(WebDriver driver) {
        super(driver);
        this.tile = new Tile(driver);
        this.tiles = new Tiles(driver);
    }

    public Tile tile() {
        return tile;
    }

    public Tilesresults() {
        return tiles;
    }

}

and on Test page:

public class SomeTest extends BaseTest {
    private static String URL = "https://www.page.com/home/";
private Tile tile;
private ThePage page;
private String link = "link";

@BeforeMethod
public void setup() {
    driver.get(URL);
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    page = new ThePage(driver);
}

@Test
public void test() throws Exception {
    // code goes here
    tile = new Tile(driver);
    tiles.results().getTiles();
    tile.getTitle();
}

}

I tried many things and I just no longer have ideas, I also checked POM pages online but couldnt find why it doesnt work. Help please, I am new to this P.S. the page goes like this:

enter image description here

Upvotes: 0

Views: 526

Answers (1)

Amit Jain
Amit Jain

Reputation: 4597

Add explicit wait in constructor, because it might be possible that element is taking time to load. If this does not work then xpath needs to be updated.

WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ur xpath here")));

As per image Tiles Page Class contains Tile Class so this is kind of HAS A relationship So we need to add Tile Class object reference in Tiles Page Class

public class Tiles {
    private WebDriver driver;
    public Tile tile;
    public Tiles(WebDriver driver) {
        this.driver = driver;
    }

Upvotes: 1

Related Questions