gpt4 book ai didi

java - 有没有一种方法可以传递方法的返回值,而在引用时不会执行整个方法?

转载 作者:行者123 更新时间:2023-12-01 09:31:02 25 4
gpt4 key购买 nike

在下面的代码中,您将看到 beforetest 方法返回字符串 srnumber。该变量是 beforetest 方法执行期间生成的唯一标识符。我想要做的是存储该唯一编号并在其他方法中引用它。我遇到的问题是当我做类似的事情时

public void ReferenceMethod(){
String InvoiceReference = beforetest()
Searchfield.sendkeys(InvoiceReference)
}

由于各种原因,我不希望整个beforetest方法运行,主要原因是它创建了一个唯一编号。

package fcstestingsuite.fsnrgn;

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.openqa.selenium.WebDriver;
import pageobjectfactory.*;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;


public class E2ESRInvoiceTest {
WebDriver driver;
Ourfsnlogin LoginPage;
SRCreate SRCreatePage;
InvoicingPOF Invoicing;
HomePagePOF HomePage;
HomePagePOFSC HomePageSC;
Utilities UtilClass;


@BeforeSuite
public String beforeTest() throws InterruptedException {
SRCreate.webdrive();
InvoicingPOF.webdrive();
HomePagePOFSC.webdrive();
PageFactory.initElements(new AjaxElementLocatorFactory(Utilities.driver, 60), this);
Utilities.driver.get("fsndevweb:81");
SRCreatePage = PageFactory.initElements(Utilities.driver, SRCreate.class);
LoginPage = PageFactory.initElements(Utilities.driver, Ourfsnlogin.class);
Invoicing = PageFactory.initElements(Utilities.driver, InvoicingPOF.class);
HomePage = PageFactory.initElements(Utilities.driver, HomePagePOF.class);
HomePageSC = PageFactory.initElements(Utilities.driver, HomePagePOFSC.class);

LoginPage.sendUserName("ebluth");
LoginPage.sendPassword("password");
LoginPage.clicksubmit();
LoginPage.USclick();
SRCreatePage.NavigateToSRCreatebtn();
SRCreatePage.SRCreationTestHVAC();
String srnumber = SRCreatePage.SRNumber.getText();
LoginPage.Logout();
LoginPage.sendUserNameSP("4335701");
LoginPage.sendPassword("password");
LoginPage.clicksubmit();
LoginPage.SPSRSearch(srnumber);
Thread.sleep(3000);
Invoicing.ActivtityCompletition();
HomePage.HomePageNaviate();
LoginPage.SPSRSearch(srnumber);
return srnumber;
}

我知道这是当您引用方法返回对象时的预期行为。有办法改变吗?我真的不想在每个需要 SR 编号的方法中都包含之前测试的代码。任何帮助表示赞赏!我对 java 也很陌生,所以如果我的代码中有任何致命的罪过,我提前道歉,哈哈。

最佳答案

将值存储在字段中,并在每次后续调用时返回先前计算的值怎么样?

public class E2ESRInvoiceTest {
WebDriver driver;
Ourfsnlogin LoginPage;
SRCreate SRCreatePage;
InvoicingPOF Invoicing;
HomePagePOF HomePage;
HomePagePOFSC HomePageSC;
Utilities UtilClass;

private String cachedSrnumber;

@BeforeSuite
public String beforeTest() throws InterruptedException {
if(cachedSrnumber != null)
return cachedSrnumber;

// Your beforeTest() code

cachedSrnumber = srnumber;
return srnumber;
}
}

关于java - 有没有一种方法可以传递方法的返回值,而在引用时不会执行整个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39398262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com