简体   繁体   中英

using variable of one java class to another java class

There is a global variable in a java class. This variable gets value from the function in this java class. I have to use this variable with its value in another java class.

Please check the code below:

Java class1: Utility functions Java class2: team.java A variable which I want to use in 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);
}
}

I have to use the value of variable: search_project_id in team.java, I have to give this as input to field in web application.

Please help.

Thanks in advance.

Team

Declare the variable as static and use it in other class by class name reference eg utilityFunctions.search_project_id

it wud be like

public static String search_project_id;

Using "public" as access specifier makes it accessible in other class whereas "Static" modifier maintains just 1 copy of the variable use the function to write some value to the varibale. Later use it in other class. Note: Dont forget to call the method which writes the value to the "search_project_id" before u use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM