java - How to return a WebElement in this scenario -


i wanted utilize page objects , following looking at:

i have created package 'pageobjects' , created class 'homepage' element below:

public class homepage  {     private static webelement element=null;      public static webelement txt_username(webdriver driver)     {         element=driver.findelement(by.name("username"));         return element;     } } 

here in test case, when use homepage.txt_username(driver).sendkeys("uday") lets enter username "uday". works perfect.

but need implement above in method entervalue("homepage.txt_username","uday")

public void entervalue(string strobjid,string strvalue) {   //how should use above parameter here?? below should work   strobjid(driver).sendkeys(strvalue);  } 

based on comment think should not write generic cucumber step definition handle types of input fields. makes hard read , understand feature files. writing html id or css selector html not fulfil bdd because write requirements @ first , because of can not know ids using. @ principles of bdd.

remember, purpose of feature file link requirement, written user story, test code. write feature files user stories. example:

given loaded home page when enter username <username> , enter password <password> , press login in button should redirected <some url> 

based on that, should implement page object pattern thoroughly. here example:

public class homepage {     private final webelement usernameelement;     // declare more page elements here      public homepage(webdriver driver) {         usernameelement = driver.findelement(by.name("username"));         // init more page elements here     }      public void enterusername(string username) {         usernameelement.sendkeys(username)     }      // more handler here: e.g. public void enterpassword(string password) ... } 

in cucumber step definition can use page object:

@then("^i enter username \"(.+)\"$") public void i_enter_my_username(string username) {     homepage.enterusername(username); } 

this pattern allows understanding of feature files , how connected test code.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -