how to initialize entFacade object?

142 views
Skip to first unread message

ibmer921150

unread,
Mar 21, 2017, 5:22:39 PM3/21/17
to Serenity BDD Users Group
Hi there,

If I define the WebElementFacade on PageObject class as following. The WebElementFacade will be initialized and work.

@FindBy(linkText = "About us")
    WebElementFacade aboutUs;
   
    public void navigateToSubMenu(String submenu) {
            this.shouldBeVisible(aboutUs);
            aboutUs.click();
        }
    }

If I defined the WebElementFacade on PageObject class in HashMap. The WebElementFacade object will return null. May I know how to initialize the WebElementFacade before put it into HashMap or where I should call PageFactory.initElements(getDriver(), this) ? Thanks

    HashMap<String, WebElementFacade> hmap = new HashMap<String, WebElementFacade>();

    @FindBy(linkText = "About us")
    WebElementFacade aboutUs;
   
    public void navigateToSubMenu(String submenu) {
            setHash();
            getHash(submenu).click();
        }
    }

    public void setHash() {
        hmap.put("About us", aboutUs);
    }

    public WebElementFacade getHash(String submenue) {
        WebElementFacade wf = hmap.get(submenue);
        return wf;
    }

John Smart

unread,
Mar 22, 2017, 11:17:47 AM3/22/17
to ibmer921150, Serenity BDD Users Group
You can add a web element dynamically (not using the @FindBy annotation) using the findBy() method (or the "$" shortcut).

--
You received this message because you are subscribed to the Google Groups "Serenity BDD Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thucydides-users+unsubscribe@googlegroups.com.
To post to this group, send email to thucydides-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
___________________________________________________
John Smart | Wakaleo Consulting  |  +44 7398 832273
Making smart teams collaborate better
http://johnfergusonsmart.com  |  john....@wakaleo.com
___________________________________________________

We love breaking down silos and helping smart teams collaborate better! Ask about our tailored on-site workshops in Agile Product Planning, BDD Requirements Discovery,  BDD, TDD and Clean Coding, and Advanced BDD Test Automation.

Need some help with Serenity BDD? Check out our Serenity BDD training and support packages here.
___________________________________________________

SeleniumWhat?

unread,
Mar 27, 2017, 8:00:40 PM3/27/17
to Serenity BDD Users Group
Your code was incorrect....that's why you have a null pointer return.
I see you put the "aboutUs" to the hmap, then you tried to retrieve the element using "submenu"?  I do not see when/where you set (or pass( the "submenu" = "aboutUs".
That's where you got the null pointer.

ibmer921150

unread,
Apr 3, 2017, 11:05:40 PM4/3/17
to Serenity BDD Users Group, liuy...@gmail.com
findBy() or $ is working. Awesome, thanks John!

在 2017年3月23日星期四 UTC+13上午4:17:47,John Smart写道:
You can add a web element dynamically (not using the @FindBy annotation) using the findBy() method (or the "$" shortcut).
On 22 March 2017 at 02:52, ibmer921150 <liuy...@gmail.com> wrote:
Hi there,

If I define the WebElementFacade on PageObject class as following. The WebElementFacade will be initialized and work.

@FindBy(linkText = "About us")
    WebElementFacade aboutUs;
   
    public void navigateToSubMenu(String submenu) {
            this.shouldBeVisible(aboutUs);
            aboutUs.click();
        }
    }

If I defined the WebElementFacade on PageObject class in HashMap. The WebElementFacade object will return null. May I know how to initialize the WebElementFacade before put it into HashMap or where I should call PageFactory.initElements(getDriver(), this) ? Thanks

    HashMap<String, WebElementFacade> hmap = new HashMap<String, WebElementFacade>();

    @FindBy(linkText = "About us")
    WebElementFacade aboutUs;
   
    public void navigateToSubMenu(String submenu) {
            setHash();
            getHash(submenu).click();
        }
    }

    public void setHash() {
        hmap.put("About us", aboutUs);
    }

    public WebElementFacade getHash(String submenue) {
        WebElementFacade wf = hmap.get(submenue);
        return wf;
    }

--
You received this message because you are subscribed to the Google Groups "Serenity BDD Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thucydides-use...@googlegroups.com.
To post to this group, send email to thucydid...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages