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

Home / Uncategorized / How to enter date, month, year in 3 different dropdowns more efficiently

Question:
So, I have just started out on automation and I am stuck on how I can have my date, month, year which are in 3 different drop downs with different xpaths entered in more efficient way such that I do not have to use select class for every single one of them

Here is the 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 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 is the Json File:::::::::::::{
"baseurl": "http://demoqa.com/",
"fname": AKASHDEEP,
"lname": SINGH,
"mstatus": single,
"hobby": ["Dance", "Reading", "Cricket"]

}


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *