How to use By.parameter in Selenium Java code?
Clash Royale CLAN TAG#URR8PPP
How to use By.parameter in Selenium Java code?
I am new to selenium, I am stuck in a specific situation code wise. I am trying to write a generic code for checkboxes, like below.
public void CheckBox(By by, String element)
driver.findElement(By.by(""));
The above generic code, which later can be used in Tests. Where user will use this function will pass the element and by what he needs to search. (by Xpath, id or name etc.)
I can write if_else or Switch but, that will too much of a code, is there any way where can use a direct parameter with By object or is there any other way to do this. Help is appreciated.
2 Answers
2
findElement
receives By
as parameter
findElement
By
public void CheckBox(By by)
driver.findElement(by);
And call it like this
CheckBox(By.id("id")); // or any other locator
public void CheckBox(By by, String element)
driver.findElement(by).sendKeys(element);
now you can call this CheckBox method into @Test code
@Test
public void fillLoginDetails()
CheckBox(By.id("ap_customer_name"),"bonn");
CheckBox(By.id("ap_email"),"absvdgdhdh@gmail.com");
CheckBox(By.name("password"),"bo123456");
CheckBox(By.id("ap_password_check"),"bon123456");
@ajinkyaJahagirdar yes it is possible.public void CheckBox(String cam, String value) driver.findElement(By.id(cam)).sendKeys(value); and Test code is CheckBox("ap_email","ra@gmail.com");
– RamanaMuttana
Aug 8 at 12:02
ajinkya Check @Guy 's answer for your requirement.
– Shivam Mishra
Aug 8 at 12:05
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hi Ramana, Your answer is good and it worked, but I was wondering if its possible to, I mean in the test, I just want to pass, type and element identifier(value), like This : PageObject po = new PageObject(driver); po.CheckBox(id, element); Can I do this, I want to make is simple and less complicated for whoever write the test automation.
– ajinkya Jahagirdar
Aug 8 at 11:53