1

I'm using page factory and need to explicitly wait until particulate elements load. Please advise how to do that. up to now I have following..

public class HomePage {
final WebDriver driver;
WebDriverWait wait;


@FindBy(xpath = "//*[contains(@id,'ctl00_LeftPanel_LeftNavigationMenu_nbMenu_GHE')]")
List<WebElement> Navi_Elements ;

and my constructor looks like

public  HomePage(WebDriver driver){
        this.driver = driver;
        wait = new WebDriverWait(driver,45);

now I'm trying to do in my method (in home page class) is.... something like...

    public void clickNavigator(String sMyNode){
                try{ wait.until(ExpectedConditions.presenceOfAllElements(Navi_Elements));
                   ............
    }}

But in ExpectedConditions class there is no method to check presence which accepts Element/s

But according to This question it can be done

can not use "visibilityOfAllElements" since there is no visibility attribute in element

I'm using selenium 3.11

3 Answers 3

0

Page factory creates pages. How do you create elements?

Element factory is one way to create elements.

Once created, you use that instance and wait for any condition. Waiting is ignorant (and not interested) about the way you created the element.

Of course in a strictly typed language like Java you need to be careful about the types.

What are your Java OOP skills? If your skills are not on par, maybe you should avoid factories and just copy-paste some code? Follow some tutorials?

5
  • You are correct my Java OOP skills are beginner level. that's why I'm asking this question. So what you are telling me is it is impossible to define wait condition in my home page class? if possible could you please direct me to how....yes even good tutorial would be fine.
    – 1234
    Apr 22, 2018 at 23:44
  • @Studeera - you can define whatever you want wherever you want. And deal with the consequences of course :-) I would suggest to spend more time following free tutorials on Java itself, and on OOP design. Only after you master the basics, dive deeper into Selenium. "Head-first" series of books by Kathy Sierra are excellent intro books (Java, OOP etc). Apr 23, 2018 at 2:21
  • Also, Python is much easier language to learn than Java as is widely considered as best first language. In fact, if you do not have any Java guru around and just self-learning, Python will get you into programming much faster, IMHO. Google for "free online course of X" Apr 23, 2018 at 2:22
  • Thanks ...I'm already in Python and comfy with it. Selecting java is to use TestNG where python unit test is useless for functionality testing. I found answer to my question in [this link] (stackoverflow.com/questions/9938986/…)
    – 1234
    Apr 23, 2018 at 4:15
  • @Studeera - I am using PyUnit for functionality and UI testing with Selenium and very happy with it, So I have hard time understanding why it will be useless for you. It is just a way to run tests. Create XML files with run results for Jenkins. PyUnit works like a charm for me. Apr 23, 2018 at 15:36
0

Use this construction when you intialize your page elements to achieve what you need:

public int timeOut = 10;
PageFactory.initElements(new AjaxElementLocatorFactory(driver, TimeoutValue), this);

Here you can find some more examples.

0

To wait for the presence of elements in page factory, you can use the @FindBy annotation to locate the elements and then wait for their presence using the ExpectedConditions class.

Here's an example:

public class HomePage {
    private final WebDriver driver;
    private final WebDriverWait wait;

    @FindBy(xpath = "//*[contains(@id,'ctl00_LeftPanel_LeftNavigationMenu_nbMenu_GHE')]")
    private List<WebElement> naviElements;

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

    public void clickNavigator(String sMyNode) {
        wait.until(ExpectedConditions.presenceOfAllElements(naviElements));
        // Perform your actions on the elements
    }
}

In this example, the HomePage class has a naviElements field, which is annotated with @FindBy to locate the elements. The wait field is initialized with a WebDriverWait instance to wait for the elements to be present.

In the clickNavigator method, the wait object is used to wait for the presence of all naviElements before performing any actions on them.

Note that you should also initialize the page factory using the PageFactory.initElements method in the constructor.

For more information, you can refer to the following resources:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.