将一个java类的变量用于另一个java类
java类中有一个全局变量。此变量从该java类中的函数获取值。 我必须在另一个java类中使用这个变量及其值 请检查以下代码: Java类别1:实用函数 Java类2:team.Java 我想在team.java中使用的变量:search\u project\u id UtilityFunctions.java将一个java类的变量用于另一个java类,java,function,variables,selenium-webdriver,Java,Function,Variables,Selenium Webdriver,java类中有一个全局变量。此变量从该java类中的函数获取值。 我必须在另一个java类中使用这个变量及其值 请检查以下代码: Java类别1:实用函数 Java类2:team.Java 我想在team.java中使用的变量:search\u project\u id UtilityFunctions.java public class UtilityFunctions { public WebDriver driver; public String bas
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\u project\u id的值,我必须将其作为web应用程序中字段的输入
请帮忙
提前谢谢
团队将变量声明为静态变量,并在其他按类名引用的类中使用它,例如utilityFunctions.search\u project\u id 那会像
public static String search_project_id;
使用“public”作为访问说明符可以在其他类中访问它,而“Static”修饰符只保留变量的一个副本
使用该函数向varibale写入一些值。以后在其他类中使用它。
注意:在使用之前,不要忘记调用将值写入“search\u project\u id”的方法 您好,谢谢您的回答,我已经尝试了您建议的方法,但是在应用程序中,当类team.java给出变量search\u project\u id的值时,它不输入任何内容,并且执行下一行代码。您的问题在这里有点混乱。。。你能解释一下你到底想要什么吗?我必须在team.java中使用search\u project\u id(从UtilityFunctions.java获得)的值。我已经试过你建议的方法了。在team.java中,我喜欢:string search\u term=UtilityFunctions.search\u project\u id,然后进一步使用它。在UtilityFunctions.java中,我将变量声明为:publicstaticstringsearch\u project\u id;。。它不起作用……那样的话,我已经回答了你的问题。使用“静态公共变量”,例如<代码>公共静态字符串搜索\u项目\u id=“12345”在“team.java”中,您可以简单地将其用作
实用函数。搜索\u project\u id
任何您想要使用变量值的地方。请记住,在使用变量之前,您需要初始化它。@SamreenAdil如果对您有效,请接受答案。:)