2017-09-05 7 views
-2

プロパティ(Java)で助けてください。 別のメソッドでgetPropertyを使用するには? ここでは@Test(テストメソッド)でも使用します。他のメソッドからgetPropertyを使用するには? (Java)

可視性は作成方法によって制限されます。より高いオブジェクトを作成する必要がありますか?

私は非常に助けに感謝します。 ありがとう!

コード:

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

コードのみloginで行うのプロパティをロードするので、あなたはpropetiesを使用する前に、単純から

Properties properties = new Properties(); 

の範囲を移動loginが呼び出されると仮定loginのローカル変数は、クラスフィールドと同じレベル、すなわち、

public WebDriver driver = null; 
と同じレベルになります
関連する問題