Page Factory - Unable to use javascript executor | Selenium Forum
M
Posted on 20/03/2016
I am using Page Factory to initiate a common driver for the entire test. So created a page class and a test which will use the functionality of the page class to find total deals but need to scroll down the window to load all the deals. So thought of using JS Executor but ended up with casting error("java.lang.ClassCastException: Pages.CourseListPage cannot be cast to org.openqa.selenium.JavascriptExecutor
").

If you can please help me with how to use JS Executor with Page Factory.


-----Page Class-----
package TestCases;

import java.text.ParseException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Pages.CourseListPage;
import Pages.FindTeeTimesPage;
import Pages.LogInPage;
import Pages.TopMenuPage;

public class CourseListTest{

public String driverPath = "C:\\Eclipse\\Driver\\chromedriver_win32\\";
//public static WebDriver driver;
static WebDriver driver;

@BeforeTest
public void setUp() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://qa.teeoff.com/"); //http://qa.teeoff.com/search#location=arizona&view=course&date=03-24-2016
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//JavascriptExecutor js = (JavascriptExecutor)driver;
}

@Test
public static void main() throws InterruptedException, ParseException{

CourseListPage CLpage = new CourseListPage(driver);
CLpage.FindDeals();


}

@AfterTest
public void tearDown() {
if(driver!=null) {
driver.quit();
}

}

}




-------Test Case ----------------
package Pages;

import java.util.List;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class CourseListPage{

public CourseListPage(WebDriver driver){
PageFactory.initElements(driver, this);
}

JavascriptExecutor js = (JavascriptExecutor)this;

@FindBy(xpath = "//*[@id='Search']/div[1]/div[1]/div/div[3]/div[2]/section/div/div[1]/div[2]/div[1]/h3/span[1]")
public WebElement TotalCoursesAvailable;
@FindAll({@FindBy(xpath = "//*[@id='course-view']/li") })
public List<WebElement> CourseTag;
@FindAll({@FindBy(css = "tag.sprite.green-tag") })
public List<WebElement> GreenTag;
@FindAll({@FindBy(css = "tag.sprite.green-tag") })
public List<WebElement> ScrollBar;

@Test
public void FindDeals() throws InterruptedException{

//Total Courses Available
int TCA = Integer.parseInt(TotalCoursesAvailable.getText());
System.out.println("Total Courses Available: "+TCA);

//Scroll down to get all the Available Courses
CourseTag.size();
while(true){

int BeforeSrollingSize = CourseTag.size();
//System.out.println("Before Scroll Courses: "+ BeforeSrollingSize);
int y = CourseTag.get(CourseTag.size()-1).getLocation().y;
//System.out.println("Y Location Before Scroll Deals: "+ y);
js.executeScript("window.scrollTo(0,"+y+")");
Thread.sleep(5000);
CourseTag.size();
int AfterSrollingSize = CourseTag.size();
//System.out.println("After Scroll Courses: "+ AfterSrollingSize);
if (AfterSrollingSize==BeforeSrollingSize)
break;
}

//Find Total TEEOFF Deals
GreenTag.size();
System.out.println("Total TEE OFF DEALS: "+ (GreenTag.size()/2));

M
Replied on 21/03/2016

send a screen shot.