请帮助使用Properties(Java)。 如何在另一种方法中使用getProperty? 在这里,我想在@Test(测试方法)中使用它。
其可见性受创建方法的限制。我需要创建更高的对象吗?
我将非常感谢你的帮助。 谢谢!
代码:
public class Main {
public WebDriver driver = null;
@BeforeTest
public void login() throws IOException {
//Connect properties file for data driven tests.
Properties properties = new Properties();
FileInputStream file = new FileInputStream("//Users//macbook//IdeaProjects//WebElements//src//dataDriven.properties");
properties.load(file);
//Option to choose browser from our properties file.
if (properties.getProperty("browser").equals("Chrome")) {
System.setProperty("webdriver.chrome.driver", "/Users/macbook/Downloads/Selenium files/chromedriver");
driver = new ChromeDriver();
} else if (properties.getProperty("browser").equals("Firefox")) {
System.setProperty("webdriver.gecko.driver", "/Users/macbook/Documents/Selenium/Browser Drivers/Firefox GecoDeriver/geckodriver");
driver = new FirefoxDriver();
}
//Option to chose url from our properties file.
driver.get(properties.getProperty("url"));
}
@Test
public void test() {
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(properties.getProperty("Username"));
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(properties.getProperty("Password"));
driver.findElement(By.xpath("//*[@name = 'event_login']")).click();
}
@AfterTest
public void end() {
driver.quit();
}
}
答案 0 :(得分:0)
仅在login
中完成加载属性的代码,因此假设在您想要使用login
之前调用propeties
,那么只需移动
Properties properties = new Properties();
从login
的局部变量到类字段,即与
public WebDriver driver = null;