如何从另一个方法使用getproperty( java 语)

c86crjj0  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(340)

请帮助了解属性(java)。如何在其他方法中使用getproperty?这里我想在@test(test method)中也使用它。
它的可见性受到创建它的方法的限制。我需要创建一个更高的对象吗?
我将非常感谢你的帮助。谢谢!
代码:

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

hivapdat1#

要在中加载属性的代码仅在中完成 login 所以假设 login 在您要使用之前调用 propeties 然后简单地移动

Properties properties = new Properties();

从局部变量 login 类字段,即与

public WebDriver driver = null;

相关问题