2015-06-12 133 views
1

java類中有一個全局變量。這個變量從這個java類的函數中獲取值。 我必須在另一個java類中使用它的值。使用一個java類的變量到另一個java類

請檢查下面的代碼:

的Java類class1:實用功能 的Java類2:team.java 變量,我想在team.java使用:search_project_id

UtilityFunctions.java

public class UtilityFunctions { 
     public WebDriver driver; 
     public String baseUrl; 

     @Before 
     public void setUp() throws Exception { 
      driver = new FirefoxDriver(); 
      baseUrl = "http://www.groupten.com/"; 
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     } 

    public void LeedOnlineLogin(WebDriver driver, String userName, String password) throws IOException { 
      //driver.get("http://qas.leedon.io/"); 

      //driver.findElement(By.id("modal_trigger")).click(); 
      //driver.findElement(By.id("userName")).clear(); 
     // driver.switchTo().frame(driver.findElement(By.id("login_iFrame"))); 
      driver.findElement(By.name("username")).sendKeys("[email protected]"); 
      driver.findElement(By.name("password")).clear(); 
      driver.findElement(By.name("password")).sendKeys("leedonline"); 
      driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div[1]/form/p[3]/input")).click(); 

     } 


    //public void QASURL(WebDriver driver) throws Exception { 
      //driver.get("http://qas.leedon.io/"); 
     //} 
     public void ScrollUp(WebDriver driver) { 
       JavascriptExecutor jse = (JavascriptExecutor)driver; 
       jse.executeScript("scroll(0, 0)"); 
      } 

     public void LeedOnlineURL(WebDriver driver) throws Exception { 
      driver.get("http://stg.new.leedonline.com/"); 
     } 

    public void create_project (WebDriver driver) throws IOException, Exception 

      { 


       // Click on the Projects 
       WebElement Projects= driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div[2]/div/div[1]/ul[1]/li[1]/a")); 
       Projects.click(); 


       // Click on the Create New Project 
       WebElement Create_Project= driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div[2]/div/div[1]/div[4]/a")); 
       Create_Project.click(); 


       // name of the project 
        int random_num = 0; 
        Random t = new Random(); 
        // random integers in [0, 1000] 
        random_num= (t.nextInt(1000)); 
        String random_str = String.valueOf(random_num); 

        WebElement Name_Project= driver.findElement(By.xpath("/html/body/div[1]/form/fieldset/div[1]/div[1]/div/input")); 
        Name_Project.sendKeys(random_str); 
        Thread.sleep(1000); 

        search_project_id= driver.findElement(By.xpath("/html/body/div[1]/form/fieldset/div[1]/div[1]/div/input")).getAttribute("value"); 


        // Select the anticipated type from the dropdown 
        //Select anticipatedType = new Select(driver.findElement(By.id("anticipatedType"))) ; 
        //anticipatedType.selectByVisibleText("Circulation Space"); 


        //Gross area 
        WebElement gross_area= driver.findElement(By.id("grossFootage")); 
        gross_area.sendKeys("9876543"); 
        Thread.sleep(1000); 



        //Enter Owner Orgnization 
        WebElement owner_org= driver.findElement(By.xpath("/html/body/div[1]/form/fieldset/div[1]/div[16]/div/input")); 
        owner_org.sendKeys("test owner org"); 
        Thread.sleep(1000); 



        //Enter Owner name 
        WebElement owner_name= driver.findElement(By.id("primcontactname")); 
        owner_name.sendKeys("test owner name"); 
        Thread.sleep(1000); 



        // Select the owner type from the dropdown 
        Select ownertype = new Select(driver.findElement(By.xpath("/html/body/div[1]/form/fieldset/div[1]/div[18]/div/select"))) ; 
        ownertype.selectByVisibleText("Business Improvement District"); 
        Thread.sleep(1000); 

        //Enter Email 
        WebElement email= driver.findElement(By.id("email")); 
        email.sendKeys("[email protected]"); 
        Thread.sleep(1000); 

        //Enter Address1 
        WebElement address= driver.findElement(By.xpath("/html/body/div[1]/form/fieldset/div[1]/div[22]/div[1]/div/input")); 
        address.sendKeys("Test street"); 
        Thread.sleep(1000); 

        //Enter City 
        WebElement city= driver.findElement(By.id("city")); 
        city.sendKeys("Test city"); 
        Thread.sleep(1000); 
    } 
    public void search (WebDriver driver) throws IOException, Exception{ 


        //String search_project_id= driver.findElement(By.xpath("/html/body/div[1]/form/fieldset/div[1]/div[1]/div/input")).getAttribute("value"); 
        System.out.println(search_project_id); 
        Thread.sleep(2000); 


        WebElement search_field= driver.findElement(By.id("searchAutoComplete")); 
        search_field.sendKeys(search_project_id); 
        Thread.sleep(2000); 

        WebElement search_button = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div[1]/div/div[2]/div/form/input[3]")); 
        search_button.click(); 

        WebElement search_click= driver.findElement(By.linkText(search_project_id)); 
        search_click.click(); 
        Thread.sleep(5000); 



       } 
    } 




team.java: 
package LOAutomation; 

import static org.junit.Assert.*; 
import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.fail; 

import java.io.File; 
import java.io.IOException; 
import java.util.concurrent.TimeUnit; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.By.ByXPath; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.firefox.FirefoxProfile; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.Select; 
import org.openqa.selenium.support.ui.WebDriverWait; 
//import org.sikuli.script.Pattern; 
//import org.sikuli.script.Screen; 
import org.openqa.selenium.JavascriptExecutor; 
import org.junit.Test; 

import LOAutomation.UtilityFunctions; 

public class team { 
    private WebDriver driver; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 


    //File profileDirectory = new File("C:\\Users\\Yogaan\\Roaming\\Mozilla\\Firefox\\Profiles\\m9mvvvna.QA"); 
    //FirefoxProfile profile = new FirefoxProfile(profileDirectory); 
    driver = new FirefoxDriver(); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void team_procedure() throws IOException, Exception { 



     UtilityFunctions obj = new UtilityFunctions(); 
     obj.LeedOnlineURL(driver); 
     WebDriverWait wait = new WebDriverWait(driver, 5); // wait for max of 5 seconds 
     obj.LeedOnlineLogin(driver, "[email protected]", "leedonline"); 
     Thread.sleep(10000); 
} 
} 

我必須使用團隊.java中的變量:search_project_id的值,我必須將此作爲輸入傳遞給Web應用程序中的字段。

請幫忙。

在此先感謝。

回答

1

變量聲明爲靜態和類的名稱引用如使用其他類utilityFunctions.search_project_id

它WUD像使用

public static String search_project_id; 

「公共性」,訪問說明符使其可以在其他類中訪問,而「靜態」修飾符僅維護變量的一個副本 使用函數向varibale寫入一些值。稍後在其他課程中使用它。 注意:在使用之前,不要忘記調用將值寫入「search_project_id」的方法。

+0

嗨,謝謝你的回答,我已經嘗試了你的建議,但是在應用程序中,變量search_project_id的值由類team.java給出,它什麼都不輸入,下一行代碼被執行。 –

+0

我希望它的工作方式,你想它... –

+0

不,它不工作:( –

相關問題