java - How to enter date, month, year in 3 different dropdowns more efficiently -


i have started out on automation , stuck on how can have date, month, year in 3 different drop downs different xpaths entered in more efficient way such not have use select class every single 1 of them

here code :

package com.singh.assignment;  import java.io.filereader; import java.util.list;  import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.firefox.firefoxdriver; import org.openqa.selenium.support.ui.select;  import com.google.gson.jsonobject; import com.google.gson.jsonparser;  public class json  {       public static void main(string args[])  {     jsonparser parser = new jsonparser();  try  {     object obj = parser.parse(new filereader("c:\\users\\dell\\eclipse- workspace\\assignment\\data.json"));     jsonobject jsonobject = (jsonobject) obj;      string fname = (string) jsonobject.get("fname").getasstring();    string lname = (string) jsonobject.get("lname").getasstring();    string baseurl = (string) jsonobject.get("baseurl").getasstring();    string mstatus = (string) jsonobject.get("mstatus").getasstring();    system.setproperty("webdriver.gecko.driver","e:\\work\\geckodriver.exe\\");    webdriver driver = new firefoxdriver();     driver.get(baseurl);     driver.findelement(by.partiallinktext("registration")).click();     driver.findelement(by.xpath("//input[@id =  'name_3_firstname']")).sendkeys(fname);     driver.findelement(by.xpath("//input[@id =  'name_3_lastname']")).sendkeys(lname);     list<webelement> martial = driver.findelements(by.name("radio_4[]"));    {        for(webelement radio : martial)        {            if(radio.getattribute("value").equalsignorecase(mstatus))            {                radio.click();            }        }    }     driver.findelement(by.xpath("//input[@value = 'reading']")).click();     webelement cntry = driver.findelement(by.xpath("//select[@id =    'dropdown_7']"));     thread.sleep(3000);     select index = new select(cntry);     index.selectbyvisibletext("india");     webelement month = driver.findelement(by.id("mm_date_8"));     select index1 = new select(month);     index1.selectbyvisibletext("9");     webelement date = driver.findelement(by.id("dd_date_8"));     select index2 = new select(date);     index2.selectbyvisibletext("15");     webelement year = driver.findelement(by.id("dd_date_8"));     select index3 = new select(year);     index3.selectbyvisibletext("1995");  } catch (exception e)  {     e.printstacktrace(); }     }  } 

and here json file :

  {  "baseurl": "http://demoqa.com/",  "fname": akashdeep,  "lname": singh,  "mstatus": single,  "hobby": ["dance", "reading", "cricket"]    } 

i'd need revise tests' implementation approach. ideal test case shouldn't know webdriver, locators or hardcoded data. should try building several abstraction layers encapsulate driver calls within framework, locators within page objects, test data within external storage (plus entities , data providers).

if talk basic optimization particular scenario, i'd start creating abstract page, hide explicit interactions webdriver:

public abstract class abstractpage {      private final webdriverwait wait;      public abstractpage() {         // assuming external driver provider         this.wait = new webdriverwait(getdriver(), 10);     }      public void selectbyvisibletext(final locator, final string text) {         new select(waitfor(locator, expectedconditions::visibilityofelementlocated)).selectbyvisibletext(text);     }      public void selectbyvisibletext(final locator, final int number) {         selectbyvisibletext(locator, string.valueof(number));     }      private webelement waitfor(final locator, final function<by, expectedcondition<webelement>> condition) {         return wait.until(condition.apply(locator));     } } 

then can create page object domain logic, reuse common dropdown selection method:

public class homepage extends abstractpage {      private dropdowndate = by.id("date");     private dropdownmonth = by.id("month");     private dropdownyear = by.id("year");      public homepage selectdate(final localdate date) {         selectbyvisibletext(dropdownmonth, date.getmonthvalue());         selectbyvisibletext(dropdowndate, date.getdayofmonth());         selectbyvisibletext(dropdownyear, date.getyear());         return this;     } } 

and in test case you'd call selectdate(date), more concise , human readable way express business logic.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -