Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在3个不同的下拉列表中更有效地输入日期、月份、年份_Java_Selenium_Selenium Webdriver_Automation - Fatal编程技术网

Java 如何在3个不同的下拉列表中更有效地输入日期、月份、年份

Java 如何在3个不同的下拉列表中更有效地输入日期、月份、年份,java,selenium,selenium-webdriver,automation,Java,Selenium,Selenium Webdriver,Automation,我刚刚开始学习自动化,我一直在思考如何让我的日期、月份、年份以更高效的方式输入3个不同的下拉列表中的不同XPath,这样我就不必为每个XPath都使用select类 代码如下: package com.singh.assignment; import java.io.FileReader; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org

我刚刚开始学习自动化,我一直在思考如何让我的日期、月份、年份以更高效的方式输入3个不同的下拉列表中的不同XPath,这样我就不必为每个XPath都使用select类

代码如下:

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();
}
    }

}

我认为您需要完全修改测试的实现方法。理想的测试用例不应该知道任何关于WebDriver、定位器或硬编码数据的信息。您应该尝试构建几个抽象层来封装框架中的驱动程序调用、页面对象中的定位器、外部存储中的测试数据以及实体和数据提供者

如果我们讨论您的特定场景的一些基本优化,我将从创建一些抽象页面开始,它可能隐藏与WebDriver的显式交互:

public abstract class AbstractPage {

    private final WebDriverWait wait;

    public AbstractPage() {
        // assuming some external driver provider
        this.wait = new WebDriverWait(getDriver(), 10);
    }

    public void selectByVisibleText(final By locator, final String text) {
        new Select(waitFor(locator, ExpectedConditions::visibilityOfElementLocated)).selectByVisibleText(text);
    }

    public void selectByVisibleText(final By locator, final int number) {
        selectByVisibleText(locator, String.valueOf(number));
    }

    private WebElement waitFor(final By locator, final Function<By, ExpectedCondition<WebElement>> condition) {
        return wait.until(condition.apply(locator));
    }
}

在您的测试用例中,您只需调用selectDatedate,这是一种更简洁、更具可读性的方式来表达业务逻辑。

比什么更有效?你没有发布你的代码。比为日期月份和年份的每个下拉列表创建一个select类更有效共享你的代码以及HTML源代码示例共享代码和json文件在你共享的内容中我看不到与出生日期有任何关系
public abstract class AbstractPage {

    private final WebDriverWait wait;

    public AbstractPage() {
        // assuming some external driver provider
        this.wait = new WebDriverWait(getDriver(), 10);
    }

    public void selectByVisibleText(final By locator, final String text) {
        new Select(waitFor(locator, ExpectedConditions::visibilityOfElementLocated)).selectByVisibleText(text);
    }

    public void selectByVisibleText(final By locator, final int number) {
        selectByVisibleText(locator, String.valueOf(number));
    }

    private WebElement waitFor(final By locator, final Function<By, ExpectedCondition<WebElement>> condition) {
        return wait.until(condition.apply(locator));
    }
}
public class HomePage extends AbstractPage {

    private By dropdownDate = By.id("date");
    private By dropdownMonth = By.id("month");
    private By dropdownYear = By.id("year");

    public HomePage selectDate(final LocalDate date) {
        selectByVisibleText(dropdownMonth, date.getMonthValue());
        selectByVisibleText(dropdownDate, date.getDayOfMonth());
        selectByVisibleText(dropdownYear, date.getYear());
        return this;
    }
}