2017-08-15 41 views
0

我剛剛開始使用自動化功能,並且遇到了如何在3種不同的下拉菜單中輸入不同xpath的日期,月份和年份以更有效的方式,這樣我就不必使用select類爲他們如何更有效地在3個不同的下拉菜單中輸入日期,月份和年份

這裏的每一個是代碼:

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

} 

這裏是JSON文件:

{ 
"baseurl": "http://demoqa.com/", 
"fname": AKASHDEEP, 
"lname": SINGH, 
"mstatus": single, 
"hobby": ["Dance", "Reading", "Cricket"] 

    } 
+2

比什麼更有效的方法?你沒有發佈你的代碼。 – Guy

+0

比爲每個日期月份和年份的下拉列表選擇一個班級效率更高 –

+1

將您的代碼與HTML源樣本一起分享 – Andersson

回答

1

我想說你需要徹底修改你的測試的實現方法。一個理想的測試案例不應該對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)); 
    } 
} 

然後,你可以創建一個頁面對象爲您的域邏輯,這將重用公共下拉選擇方法:

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

並在測試情況下,你只需要調用selectDate(date),這是更爲簡潔和人力開展快遞業務邏輯易讀的方式。

相關問題