使用一个java类的变量到另一个java类

时间:2015-06-12 06:46:36

标签: java function variables selenium-webdriver

java类中有一个全局变量。此变量从此java类中的函数获取值。 我必须在另一个java类中使用此变量及其值。

请检查以下代码:

Java class1:实用程序功能 Java class2: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("stguser3@gmail.com");
            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("test@testingorg.com");
                    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, "stguser@gmail.com", "leedonline");
        Thread.sleep(10000);
}
}

我必须在team.java中使用变量:search_project_id的值,我必须将此作为web应用程序中字段的输入。

请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:1)

将变量声明为static并在类名引用的其他类中使用它,例如utilityFunctions.search_project_id

它就像是

public static String search_project_id;

使用" public"作为访问说明符使其可以在其他类中访问,而"静态"修饰符只保留变量的一个副本 使用该函数将一些值写入varibale。后来在其他课堂上使用它。 注意:别忘了调用将值写入" search_project_id"在你使用它之前。