Selenium TestNG- 创建配置属性并读取属性文件,现在我需要在我的测试用例中调用它

Selenium TestNG- created config properties and read properties file, now I need to call it in my test case

我在 selenium 中创建了一个属性文件和 config.properities 文件。现在我需要在我的测试用例中调用这些属性。我坚持代码应该如何在我的测试用例中调用它。 属性文件

package config;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties; 
public class PropertiesFile {

public static void main(String[] args){
    readPropertiesFile();
}

public static void readPropertiesFile(){
    Properties prop = new Properties();
    try {


        InputStream input = new 
FileInputStream("C:\Users\chetan.patel\git\uvavoices-
automation\config.properties");
        prop.load(input);
        System.out.println(prop.getProperty("url"));
        System.out.println(prop.getProperty("username"));
        System.out.println(prop.getProperty("password"));


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

这是我的测试用例:

public class AcceptanceTest {

public WebDriver driver;
public String baseURL;
private static final Logger log = 
LogManager.getLogger(AcceptanceTest.class.getName());
ExtentReports report; 
ExtentTest test; 
WaitTypes wt;
LoginPageFactory login;
VoicesPageFactory voices;
SoftAssert sa;

@BeforeSuite
public void beforeSuite(){
report = new 
ExtentReports(System.getProperty("user.dir")+"/Reports/AcceptanceTest.html", 
true);
test = report.startTest("Acceptance test");

}

@BeforeMethod
public void beforeMethod() {
  PropertiesFile data = new  PropertiesFile();
  driver = new FirefoxDriver();
  wt = new WaitTypes(driver);
  login = new LoginPageFactory(driver);
  voices = new VoicesPageFactory(driver);
  sa = new SoftAssert();



  //Starting the Web Browser and navigating to the UVA Voices development 
 environment


    data.readPropertiesFile();
    baseURL = "url";
        test.log(LogStatus.INFO, "Browser Started");
        log.info("Browser started");
        driver.manage().window().maximize();
        driver.get(baseURL);

}

 @Test
 public void VerifyingBubbles() throws InterruptedException {

  //User Login and Password

        login.username("chetan.patel"); 
            test.log(LogStatus.INFO, "Entered Username");

你可以试试下面的方法,

package config;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties; 
public class PropertiesFile {

public static void main(String[] args){
   // readPropertiesFile();
}

private java.util.Properties prop=null;

public void readPropertiesFile(){
    prop = new Properties();
    try {


        InputStream input = new 
FileInputStream("C:\Users\chetan.patel\git\uvavoices-
automation\config.properties");
        prop.load(input);
        System.out.println(prop.getProperty("url"));
        System.out.println(prop.getProperty("username"));
        System.out.println(prop.getProperty("password"));


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
public String getPropertyValue(String key){
    return prop.getProperty(key);
 }

}

然后在 before 方法中获取基数 URL,

@BeforeMethod
public void beforeMethod() {
  PropertiesFile data = new  PropertiesFile();
  driver = new FirefoxDriver();
  wt = new WaitTypes(driver);
  login = new LoginPageFactory(driver);
  voices = new VoicesPageFactory(driver);
  sa = new SoftAssert();



  //Starting the Web Browser and navigating to the UVA Voices development environment


    data.readPropertiesFile();
    String baseURL = data.getPropertyValue("url");
        test.log(LogStatus.INFO, "Browser Started");
        log.info("Browser started");
        driver.manage().window().maximize();
        driver.get(baseURL);

}