-1

Currently in my firefox driver, if I want to locate an element, I write the code as so:

WebDriver firefoxDriver = new FirefoxDriver(); ... firefoxDriver.findElement(By.id("testid")).isDisplayed(); or firefoxDriver.findElement(By.name("testname")).isDisplayed(); or firefoxDriver.findElement(By.class("testclass")).isDisplayed(); etc 

However, I have these elements within a separate page like so:

 @FindBy(id = "testid") public WebElement testIdElement; @FindBy(name = "testname") public WebElement testNameElement; @FindBy(class = "testclass") public WebElement testClassElement; 

I want to search my elements like the below but the below doesn't work because it expects a By:

firefoxDriver.findElement(elementsPage.testIdElement); or firefoxDriver.findElement(elementsPage.testNameElement); or firefoxDriver.findElement(elementsPage.testClassElement); 

What will be the best way to tackle this scenario? I don't really want to keep hard coding the elements, instead better using the elements I have already defined in my elements list.

3
  • This is alraedy answered. Please have a look here: stackoverflow.com/questions/18436102/… Commented May 15, 2019 at 16:59
  • 1
    Possible duplicate of Selenium @FindBy vs driver.findElement() Commented May 15, 2019 at 17:27
  • You don't have to find the elements, they are already found... that's what testIdElement and the others are, WebElements. Just use those like testIdElement.click(), etc. Commented May 17, 2019 at 20:10

1 Answer 1

1

Simple, declare them as a By like this in your pageObject:

public By testIdElement = By.id("testid"); public By testNameElement = By.name("testname"); public By testClassElement = By.className("testclass"); 

then call them at the findelements method the way you want to:

firefoxDriver.findElement(elementsPage.testIdElement); firefoxDriver.findElement(elementsPage.testNameElement); firefoxDriver.findElement(elementsPage.testClassElement); 

i personally like to put the driver.findElement method in the pageObject as a lambda expression, to avoid rewriting code:

what i do on C#:

public IWebElement Test => webdriver.FindElement(By.CssSelector("test")); 

converting to Java:

public WebElement test -> driver.findElement(By.cssSelector("test")); 

so, writing my code would be something like this:

page.Test.Click(); page.Test.SendKeys("..."); 

As i don't like using elements in the Test class, i write these methods in the pageobject (separating test logic from the script), example:

 public PageObjectClassExample ShowFilters() { showFiltersButton.Click(); return this; } public PageObjectClassExample ClearAllFilters() { finalDatePicker.Clear(); initialDatePicker.Clear(); searchButton.Click(); return this; } public PageObjectClassExample HideShowIssuerColunm() { ShowHideColumnBtn.Click(); IssuerColumnCheck.Click(); KeyColumnCheck.Click(); ShowHideColumnBtn.Click(); return this; } 

and in the Test file i would write something like this:

PageObjectClassExample page = new PageObjectClassExample(); page .ShowFilters() .ClearAllFilters() .HideShowIssuerColumn(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Valga, can I ask you to show an example on what you mean by this please? 'i personally like to put the driver.findElement method in the pageObject as a lambda expression, to avoid rewriting code, but each to their own!'
@BruceyBandit Sorry, now that i have read it again, it sounded bad, but i didn't mean anything bad by it, i edited the answer with how i code my selenium scripts

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.