2

I'm writing automated scripts using page factory and I want to use visibilityOfElementLocated with page factory instead of visibilityOf I tried to use visibilityOf but some times it not work with my element

the problem here that visibilityOfElementLocated take By parameter, and I have WebElment

@FindBy(id = "test")
WebElement locator;
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Why don't you use `visibilityOf` ? [javadoc](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOf-org.openqa.selenium.WebElement-) – Arnaud Claudel Sep 08 '19 at 12:31
  • I want to check if the element is present on the DOM of a page and visible. however the `visibilityOf` method check the visiblity of the element only – Abdallah Shaban Sep 08 '19 at 12:57

2 Answers2

1

You can't use it directly with @FindBy, but you can call it from a method that will run before PageFactory.initElements

public abstract class BasePage {

    protected WebDriverWait wait;

    public BasePage(WebDriver driver) {
        wait = new WebDriverWait(driver, 10);
        assertInPage();
        PageFactory.initElements(driver, this); 
    }

    public abstract void assertInPage();
}

public class DerivedPage extends BasePage {

    @FindBy(id = "test")
    WebElement locator;

    public DerivedPage(WebDriver driver) {
        super(driver);
    }

    @Override
    public void assertInPage() {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("test")));
    }
}

assertInPage() in DerivedPage will be executed right before PageFactory.initElements.

Guy
  • 46,488
  • 10
  • 44
  • 88
0

visibilityOfElementLocated(By locator)

visibilityOfElementLocated(By locator) takes a __By locator_ as an argument and is the expectation for checking that an element is present on the DOM of a page and visible.


visibilityOf(WebElement element)

visibilityOf(WebElement element) takes an WebElement as an argument and is the expectation for checking that an element, known to be present on the DOM of a page, is visible.


When using PageFactory in PageObjectModel if you expect the element to be dynamic and loaded through some JavaScript and might not be present on the page already you can use the Explicit Wait support with a normal locator factory as follows:

  • Code Block:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class TestPage {
    
        WebDriver driver;
    
        //constructor
        public TestPage(WebDriver driver)
        {
            PageFactory.initElements(driver, this);
        }
    
        //Locator Strategy
        @FindBy(id = "test")
        WebElement locator;
    
        //Test Method
        public void testMethod()
        {
            WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(TestPage.getWebElement()));
            //perform your desired action here e.g element.click()
        }
    
        public WebElement getWebElement()
        {
            return locator;
        }
    
    }
    

You can find a detailed discussion in How to add explicit wait in PageFactory in PageObjectModel?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352