java - Using WebElements from PageFactory POMs in my test class -


basically, make assertion (from test class) webelement contains text(). since of webelements defined in page.class, think have make them public this.

i running few problems webdriver , elements, , think may because multiple test classes accessing webelements page class simultaneously. question is: there reason webelements must private?

code example:

all pagefactory tutorials have seen make webelements private,

    @findby(xpath = "//*[@id='searchstringmain']")     private webelement searchfield; 

but assert element contains text (from class), have define them this:

    @findby(xpath = "(//*[contains (text(),'hrs')])[2]")     public static webelement yourloggedtime; 

consider example:

package org.openqa.selenium.example;  import org.openqa.selenium.by; import org.openqa.selenium.support.findby; import org.openqa.selenium.support.how; import org.openqa.selenium.webelement;  public class googlesearchpage {     // element looked using name attribute     @findby(how = how.name, using = "q")     private webelement searchbox;       public void searchfor(string text) {         // continue using element before         searchbox.sendkeys(text);         searchbox.submit();     } } 

searchbox private method searchfor public. test use searchfor never searchbox.

i put page factory initelements call in last line of page constructor. makes public functions available tests. so

package org.openqa.selenium.example;  import org.openqa.selenium.by; import org.openqa.selenium.support.findby; import org.openqa.selenium.support.how; import org.openqa.selenium.webelement;  public class googlesearchpage {     public webdriver driver;     // element looked using name attribute     @findby(how = how.name, using = "q")     private webelement searchbox;      public googlesearchpage(webdriver driver) {         this.driver = driver         pagefactory.initelements(driver, this);     }       public void searchfor(string text) {         // continue using element before         searchbox.sendkeys(text);         searchbox.submit();     } } 

in test can like:

new googlesearchpage(webdriver).searchfor("foo"); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -