Use attribute of element in the page object repository

I have created page objects in the repository. Now I need the xpath of one of the elements.
I want this function to be dynamic, so I can reuse it. How am I able to use the xpath of a defined object in the repository?
This problem arose when I needed a function to scroll down to an element, so the element is NOT visible yet.

Hopefully this example will help us to have a clear answer.
In the repository I have created the object “image” in the folder “images”, which is given an xpath ( findTestObject(“images/image”) ).
Now I need to use the xpath of this image, in order to find the element on the page and when it is not visible yet, the test needs to scroll to it.

Since you have the object defined, you would use that as the reference. It sounds like you might need to use:



WebUI.waitForElementVisible(findTestObject('Page_Login/btn_Login'), 10)

Hi Tim

In the repository I have created the object “image” in the folder
“images”, which is given an xpath ( findTestObject(“images/image”) ).

Let’s be super clear here… that is not an xpath, that is the path to an object called image in your Object Repository.

Then you can use it to scroll the image into view, like this:


It doesn’t (shouldn’t) matter if the object is already in view.

If you want to use a variable (to cut down on the number of calls to findTestObject), you can store your object in a local variable, image, like this:

def image = findTestObject("images/image")

Hope that helps…

Hi Peter & Russ, thank you for your answers. I have had some problems with the “scrollToElement” option. It didn’t scroll (but will try again). I am aware findTestObject(“images/image”) is not an xpath. I will copy the code I used for scrolling to an element.

The WebElement element = driver.findElement(By.xpath(’…’)); has now a hardcoded xpath, but I want to use the xpath in the defined element in my object repository.

public static void findElementByScrolling(elementPoLink) {
WebDriver driver = DriverFactory.getWebDriver()
// Maximize browser
// Pass application URL
// Create instance of JavaScript executor
JavascriptExecutor je = (JavascriptExecutor) driver;
//Identify the WebElement which will appear after scrolling down
WebElement element = driver.findElement(By.xpath('...'));
// now execute query which actually will scroll until that element is not appeared on page.
// Extract the text and verify

I need this to work…I can’t get it to work. Driving me nuts. I did come up with a work around though which I’ll share below. What I want is to dynamically be able to scroll to ANY object until its in view…however I did find a work around. the object I’m trying to scroll to at the moment is a dynamic object that has the same path except a key number combination to identify it. So I pass that.

Here is what I did. I wrote this into a keyword and then imported the keyword in my test case:

   /*with import statements:
     * import org.openqa.selenium.JavascriptExecutor
     * import org.openqa.selenium.WebDriver;
     * import

    public static void scrollToObjectInView(String variableHere){
        WebDriver driver = DriverFactory.getWebDriver();
        WebElement element = driver.findElement(By.xpath('firstPartXpath' + variableHere+ 'secondPartXpath'));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

then I just use my method name from keyword I imported and call the method