4

I have questions about clean code. I'm writing a test in Java selenium page factory. I'm a beginner.

I have two "actions", input text into search field and click button search. Now the question is, how should I do it:

  1. In page object class make 2 separate methods:
    1st: input text to search, and
    2nd: click button.
    In main class (test class) make one method which runs these two methods.

  2. In main class (test class) make two separate methods which run each of this two methods (input text and click button).

  3. In page object class make only one method where I input text into search and click search button. And later run this one method in the test class.

Which solution is right and/or the best?

2
  • ok but, i was talking about using page factory, and methods from page classes which i later use in main class - test class
    – BV360
    Commented Feb 1, 2016 at 13:51
  • two problems - 1) which post would you like this moved to? I know it's supposed to be a comment. 2) Don't keep creating new accounts - if you need me to merge the existing (you're up to 3 so far) I can, but making a new account for every comment is going to get exhausting...
    – corsiKa
    Commented Feb 1, 2016 at 17:38

1 Answer 1

3

As I understood the question, it is required to define the reusable methods and use those methods in different test cases based on the requirement.

Here I am writing the code for two individual methods to work with,...

public void inputKeys(WebElement we,String ip){
    we.sendKeys(ip);
}

The above code just type the string value referred by ip variable.

Next method to click on a button is below,...

public void clickOn(WebElement we){
    we.click();
}

Now lets write the main program...

public class Sample{

public WebDriver driver;
public WebElement tf,but;

//Here you have to include the above two helper methods.
public void inputKeys(WebElement we,String ip){
    we.sendKeys(ip);
}
public void clickOn(WebElement we){
    we.click();
}

@BeforeClass
public void befC(){
    driver=new FirefoxDriver();
    driver.get("url of web page");
    tf=driver.findElement(By.name("Name of the TextField"));
    but=driver.findElement(By.xpath("The xpath of button element"));
}
@Test
public void test1(){
    inputKeys(tf,"Hello");
    clickOn(but);
}
}

The helper methods we created can be used in any test case method we write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.