PageObjectModel- Not able to do object casting | Selenium Forum
M
Posted on 31/01/2017
Hi ,

This is regarding the Object Casting in Page Object Model Framework.
Please find the below code.
[i:1du5ez0g]
Object page=loginPage.doLogin("kannan789@gmail.com", "password1980");
if (page instanceof LandingPage)
[b:1du5ez0g]LandingPage landingPage[/b:1du5ez0g]=(LandingPage) page;

else if (page instanceof LoginPage)
Assert.fail("Login Failed");

MyAccountPage myAccountPage=landingPage.clickonMyaccount()[/i:1du5ez0g]

When try the above code getting an error "LandingPage cannot be resolved to a variable"

But if I added {} for the if statement then the error message goes off
if (page instanceof LandingPage)
{
LandingPage landingPage=(LandingPage) page;
}

But in both the time the code MyAccountPage myAccountPage=landingPage.clickonMyaccount() shows the error message "landingPage cannot be resolved"

Kindly clarify this.

M
Replied on 31/01/2017

do it like this.

[quote:3n47j0ip]LandingPage landingpage

if(//condition){
landingpage=(LandingPage )page
}
[/quote:3n47j0ip]

it will work.