2014-02-13 45 views
0
  • 目前正在對Selenium WebDriver和使用Java。在TestNG幀產生的報告。
  • 當前爲Web應用程序編寫腳本。它包含許多下拉菜單,每個下拉菜單包含許多選項。
  • 我有屬性文件(以Login.txt文件的形式)。在這我已經存儲我的用戶名密碼和URL然後將得到字符串uid=p.getProperty("loginUsername");像這樣。
  • 該場景與其需要通過下拉ID查找元素相同的方式,然後它需要從文本文件中獲取選項以在下拉列表中進行選擇。
  • 請建議我有沒有可能這樣工作。

的HTML標記,如下所示:如何從屬性文件Selenium WebDriver獲取下拉選項?

<select id="periodId" name="period" style="display: none;"> 
<option value="l4w">Last 4 Weeks</option> 
<option value="l52w">Last 52 Weeks</option> 
<option value="daterange">Date Range</option> 
<option value="weekrange">Week Range</option> 
<option selected="" value="monthrange">Month Range</option> 
<option value="yeartodate">Year To Date</option> 
</select> 

我如何能在文本文件以及硒的webdriver代碼給這些值。

回答

0

如果您有屬性文件:那麼你可以不喜歡

  1. 進入產權

periodId =過去52周

,並使用

WebElement periodId = driver.findElement(By.xpath("xpath expression")); 
String periodIdvalue =p.getProperty("periodId "); 
periodId.sendKeys(periodIdvalue); 

這樣就可以使用屬性從下拉列表中選擇值。

,或者您可以使用

dropdown.selectByValue(periodIdvalue); 

相反的SendKeys

1
// Get dropdown id from properties file and find element 
WebElement element = driver.findElement(By.id(p.getProperty("dropdown.id"))); 
// Make a Select object using element 
Select select = new Select(element); 
// Get option from properties file and use it 
select.selectByVisibleText(p.getProperty("dropdown.option")); 

在屬性,你可以將其存儲像
dropdown.option=Last 52 Weeks

dropdown.option=Week Range

+0

請任何人建議我的解決方案 – Amirdha

+0

@Amir:你還在找什麼?如果我正確理解你的問題,你想從屬性文件中得到下拉選項並且想要使用它。對? – xyz

+0

是的。如何在屬性文件中顯示該屬性。是我需要存儲的正常html格式 – Amirdha

2

你可以一個類似於下面的代碼:

Select dropdown = new Select(driver.findElement(By.id("designation"))); 
// To select its option say 'Programmer' you can do 
dropdown.selectByVisibleText("Programmer "); 
// or 
dropdown.selectByIndex(1); 
// or 
dropdown.selectByValue("prog"); 

(從here拍攝)

+0

我猜OP想要從屬性文件中檢索值以選擇值。 – xyz

+0

我嘗試使用上述所有方法仍然無法選擇下拉值:(使用** Karna's **方法時出現錯誤,因爲**無法找到具有以下值的選項:Last 52 Weeks ** and when嘗試** Sumitbit2005 **得到的錯誤如下**找不到具有null id屬性的元素**。任何人都可以幫助我解決此問題。 – Amirdha

+0

請參閱我的另一個答案,它工作:我已經使用dropdown.selectByVisibleText (「value」);而不是dropdown.selectByValue(「」); – Sumitbit2005

0

我試圖用我上面的回答的:我收到錯誤「無法與價值定位選項:過去52周」,所以我用

dropdown.selectByVisibleText(period); 

它工作成功。

下面是我的步驟:

  1. 我有應用。屬性文件列出的屬性:

    PeriodId =過去52周

  2. ,其讀取從application.properties值
  3. ReadProperty.java文件:

    包com.util;

    import java.io.*; 
    import java.util.*; 
    
    public class ReadProperty 
    {      
         public String readApplicationFile(String key) 
         {   
          String path = this.getPath(); 
          System.out.println(path); 
          String value = ""; 
          try{     
            Properties prop = new Properties(); 
            File f = new File(path + "//src//com//util//application.properties"); 
            System.out.println(f.getPath()); 
            if(f.exists()) 
            { 
             prop.load(new FileInputStream(f)); 
             value = prop.getProperty(key); 
           } 
          } 
          catch(Exception e) 
          { 
           System.out.println("Failed to read from application.properties file."); 
          } 
          return value; 
         }  
         public String getPath() 
         { 
          String path ="";   
          File file = new File(""); 
          String absolutePathOfFirstFile = file.getAbsolutePath(); 
          path = absolutePathOfFirstFile.replaceAll("\\\\+", "/");   
          return path; 
         } 
    
        } 
    
  4. 我的代碼來選擇值:

package com; 

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 org.testng.annotations.Test; 

import com.util.ReadProperty; 


public class NewTest4 { 
    protected WebDriver driver; 
    ReadProperty readpr = new ReadProperty(); 

    @Test 
    public void testfile() throws Exception 
    { 
    driver = new FirefoxDriver(); 
    driver.get("file:///C:/Users/Sumit.Mittal/Desktop/ff.html"); 
    String period = readpr.readApplicationFile("PeriodId"); 
    System.out.println("the value is" + period); 
    WebElement element = driver.findElement(By.xpath("//*[@id='periodId']")); 
    Select dropdown = new Select(element); 
    //dropdown.selectByValue(period); 
    dropdown.selectByVisibleText(period); 


    } 
    public void killDriver() 
    { 
    driver.close(); 

    } 

} 

試試這個它會工作。

相關問題