How to pass parameters to FindBy to locate element?

5,858 views
Skip to first unread message

js...@thoughtworks.com

unread,
Jul 27, 2015, 8:22:34 AM7/27/15
to getgauge

Hey gauge folks,

I am a Twer in Xi’an China. Nowadays I am playing with gauge. Now I am facing a problem.

Here’s my page segment:


    @FindBy(how = How.LINK_TEXT, linkText = category)

    public WebElement q_powerBankCategory;


    public void chooseCategory(String catetory) {

        q_powerBankCategory.click();

    }


My problem is category. I want to pass category as a parameter to FindBy to locate the element. How can I do this? 

Thanks

Srikanth V

unread,
Jul 27, 2015, 9:14:12 AM7/27/15
to getgauge, js...@thoughtworks.com, js...@thoughtworks.com
Hi,

Welcome to Gauge!

I am afraid, that your approach might not be feasible as it is. Annotations are static, so there isn't a way to pass the category to the FindBy annotation.

I see two ways to approach your problem:

1) Have a factory that would return the element for a given category. This will work if you have a limited number of options for the category, and if your category value is from a known set of values.

In this case you would need to define one element for every category, and your factory would return the element that matches the given category.

2) Implement your own finder. The webdriver forum has a thread on this topic - https://groups.google.com/d/msg/webdriver/awxOw0FoiYU/jrrLujsmLZUJ

Essentially, in this case, you would be writing your own annotation, that would dynamically build the finder. You can then use this instead of your regular finders.

Does this help?

regards
Srikanth

js...@thoughtworks.com

unread,
Jul 28, 2015, 11:22:20 PM7/28/15
to getgauge, js...@thoughtworks.com
Thanks for you reply. I will try your suggestions later.
But now I am having another question:

I've successfully run this piece of code:

@FindBy(how = How.LINK_TEXT, linkText = "linkTestText")
public WebElement q_powerBankCategory;
  
public void chooseCategory(String category) {
  q_powerBankCategory.click();
}




Now I don't want to use FindBy to locate an element because I need to pass category as a parameter to replace "linkTestText" , So I convert this piece of code to:
public WebElement q_powerBankCategory;

public void chooseCategory(String category) {
q_powerBankCategory.findElement(By.linkText("linkTestText"));
q_powerBankCategory.click();

}

But when I run this piece of code it failed. Say "org.openqa.selenium.NoSuchElementException: no such element"
I was wondering how should I use findElement(By.xxxx)
I've read thought all the gauge examples provide in git. But found nothing like findElement(By.xxx)




在 2015年7月27日星期一 UTC+8下午8:22:34,js...@thoughtworks.com写道:

Srikanth V

unread,
Jul 29, 2015, 1:58:50 AM7/29/15
to getgauge, js...@thoughtworks.com, js...@thoughtworks.com
Hi,

This particular problem is related to Webdriver and not anything that Gauge could contribute to. To your specific problem, can you try something like this:

FirefoxDriver firefoxDriver = new FirefoxDriver();

firefoxDriver.get("https://www.google.co.in");

WebElement settings = firefoxDriver.findElement(By.linkText("Settings"));

System.out.println(settings.getText());

firefoxDriver.close(); 

This works for me, and I can see that my webdriver instance is able to locate the "Settings" link and print out its text.

Does this help?

Thanks
Srikanth

Jie Shi

unread,
Jul 29, 2015, 3:53:47 AM7/29/15
to Srikanth V, getgauge
Thanks for your reply.
Your code works well for me. Now I understand the real problem of mine.
My question should be: How to locate elements in pages
So what I should do is pass driver as a parameter to the method in pages.
Now I’ve fixed my problem.
Thanks for your supporting!!

Now I am writing up an example of our own to do a introduction/workshop to our clients. 

Srikanth V

unread,
Jul 29, 2015, 4:14:32 AM7/29/15
to getgauge, js...@thoughtworks.com, js...@thoughtworks.com
Hi Again,

Great to know that this was helpful.

A related point to note: Beware of performance implications when choosing a Selenium selector. ID and Name selectors are often the best performant. In cases where your element does not have ID or Name, you can use the other selectors, but please beware that some selectors would traverse the entire DOM to find out your match, and this can be an expensive operation.

Good luck with your demo!

best regards
Srikanth
Reply all
Reply to author
Forward
0 new messages