[英]using variable of one java class to another java class
There is a global variable in a java class. java类中有一个全局变量。 This variable gets value from the function in this java class. 该变量从该java类中的函数获取值。 I have to use this variable with its value in another java class. 我必须在另一个java类中使用此变量及其值。
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 Java class1:实用程序函数Java class2:team.java我想在team.java中使用的变量:search_project_id
UtilityFunctions.java 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. 我必须在team.java中使用变量的值:search_project_id,我必须将其作为Web应用程序中字段的输入。
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 将变量声明为静态变量,并通过类名称引用在其他类中使用它,例如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. 使用“ public”作为访问说明符使其可以在其他类中访问,而“ Static”修饰符仅维护变量的1个副本,请使用该函数将一些值写入变量。 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. 注意:在使用之前,请不要忘记调用将值写入“ search_project_id”的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.