Issue
I'm following page object model where in I have placed all the WebElement addresses and methods i.e id, xpath etc in the page. Example: On a login page we have username field, password field and login button. So all these will be on the LoginPage.java. This page includes the methods that use the Webelements as well. So from the actual test case I'm just calling methods that are defined on the page. Now here's my dilemma. I have several test steps that need me to click on an element. So say i need to click on Link1, Link2 and Link3. Right now what I'm doing is writing a separate method for each step and keeping them on the pagelike below:
@FindBy(id = "someId1")
WebElement link1;
@FindBy(id = "someId2")
WebElement link2;
public void clickOnLink1(){
link1.click();
}
public void clickOnLink2(){
link2.click();
}
Testcase:
clickOnLink1();
clickOnLink2();
So if i have 50 steps that need me to click on 50 different elements i would need 50 different methods and this is highly improper and inefficient. I could use a generic method like below:
public void clickOnElement(WebElement element) {
element.click();
}
I can pass the element i want to be clicked to this method and it would do it for me.
Unfortunately as i said before I'm following page object model so all the WebElement addresses like id exist on the page and not on the test case hence i cannot use this method since i would need to pass the webelement from the test case.
Could someone please help me with a different approach.
Thank you
Solution
You can use something like following:
public class Login_Page {
@FindBy(id = "someId1")
WebElement link1;
@FindBy(id = "someId2")
WebElement link2;
//constructor
public Login_Page(driver) {
PageFactory.initElements(driver, this);
}
//Generic Click method
public boolean genericClick(WebDriver driver, WebElement elementToBeClicked)
{
try{
elementToBeClicked.click();
return true;
}
catch(Exception e){
return false;
}
}
Now in the Test method:
@Test
public void LoginTest()
{
Webdriver driver = new FirefoxDriver();
driver.get("someurl")
Login_Page lp = new Login_Page(driver);
lp.genericClick(driver, lp.link1);
}
Answered By - Kushal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.