简体   繁体   English

无法从Selenium Webdriver中的配置文件接收正确的值

[英]Not receiving correct value from Config file in Selenium webdriver

Have data in config file as test_data_path = C:\\\\Sudheer\\\\Sudheer\\\\Selinium scripts\\\\Webdriverscrip\\\\Automation_Project\\\\TestData\\\\Nlpapplication.xlsx 将配置文件中的数据作为test_data_path = C:\\\\Sudheer\\\\Sudheer\\\\Selinium scripts\\\\Webdriverscrip\\\\Automation_Project\\\\TestData\\\\Nlpapplication.xlsx

when I run my below script result is shown with one Slash missing 当我运行下面的脚本结果时,显示缺少一个斜杠

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

You should override function load function as it is escaping \\ character. 您应该覆盖函数加载功能,因为它转义了\\字符。

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

Class to read data from config file: 从配置文件读取数据的类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM