2016-04-29 39 views
0

您也可以在配置文件中的數據作爲 test_data_path = C:\\Sudheer\\Sudheer\\Selinium scripts\\Webdriverscrip\\Automation_Project\\TestData\\Nlpapplication.xlsx不從配置文件中硒的webdriver

運行我下面的腳本結果顯示有一個斜線失蹤

C:\Sudheer\Sudheer\Selinium scripts\Webdriverscrip\Automation_Project\TestData\Nlpapplication.xlsx

public class Testconfigvalues { 
    public static void main(String[] args) throws IOException { 

     FileInputStream fs = null; 
     fs = new FileInputStream(System.getProperty("user.dir")+"\\config.properties"); 
     Properties property=new Properties(); 
     property.load(fs); 
     String data_test_data_path = property.getProperty("test_data_path"); 
     System.out.println("value is " +data_test_data_path); 
    } 
} 

回答

1

接收正確的值您應該覆蓋函數加載函數,因爲它正在轉義\字符。

import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.util.Properties; 

public class NetbeansProperties extends Properties { 

@Override 
public synchronized void load(Reader reader) throws IOException { 
    BufferedReader bfr = new BufferedReader(reader); 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 

    String readLine = null; 
    while ((readLine = bfr.readLine()) != null) { 
     out.write(readLine.replace("\\", "\\\\").getBytes()); 
     out.write("\n".getBytes()); 
    } 

    InputStream is = new ByteArrayInputStream(out.toByteArray()); 
    super.load(is); 
    } 

    @Override 
    public void load(InputStream is) throws IOException { 
     load(new InputStreamReader(is)); 
    } 
} 

類來讀取配置文件數據:

import java.io.FileInputStream; 
import java.io.IOException; 

public class ReadConfig { 

    public static void main(String[] args) throws IOException { 

     FileInputStream fs = null; 
     fs = new FileInputStream(System.getProperty("user.dir") + "\\config.properties"); 
     NetbeansProperties property = new NetbeansProperties(); 
     property.load(fs); 
     String data_test_data_path = property.getProperty("test_data_path"); 
     System.out.println("value is " + data_test_data_path); 
    } 
} 
+0

感謝阿里,剛纔看到你的帖子,會盡量讓你知道 – Sudheer

+0

你能接受的答案,如果它爲你工作。 –

+0

而且我還發現了另一種使用TESTNG的方法,我在Testng.xml文件中直接給出了調用參數的路徑。這有助於我的代碼可以重用到我的團隊,因爲他們可以在xml文件中提供路徑而不是代碼 Sudheer