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

时间:2017-06-28 20:51:43

标签: java selenium selenium-webdriver testng ui-automation

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

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

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方式,

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

}