java 无法将参数从一个类方法调用到另一个类方法

ehxuflar  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(121)

我已经尝试了下面的代码有两个类一个是Class 1-TestData和Method 1-excel(),另一个调用访问参数Class 2-AdminLoginAction和Method 2-Admin_Login().
这里的问题是我需要调用的字符串参数,如UID和PWD,因为我在屏幕截图中标记的附件。但脚本显示了一些错误,无法访问它。那么,我该如何解决这个问题,我去正确的方法或任何其他方式?请尽快需要充分。
Script image for two classes

import java.io.IOException; 
import java.util.Date;

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.*;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import com.aventstack.extentreports.reporter.ExtentSparkReporter;

public class AdminLoginAction extends TestData{

WebDriver d;
Date currentdate = new Date();
String Screenshotdate = currentdate.toString().replace(" ", "-").replace(":", "-");
ExtentSparkReporter spark = new ExtentSparkReporter("ExtentReport.html");
ExtentReports extent = new ExtentReports();
@Test()
public void Admin_Login() throws InterruptedException, IOException {
    TestData excel = new TestData();
    
    extent.attachReporter(spark);
    ExtentTest test = extent.createTest("Launch browswer and access the WeClean Login page");
    test.log(Status.PASS, "Launch browser success...!!!");
    test.pass("Verified launching browser");
    System.setProperty("webdriver.chrome.driver", "D:\\backup\\selenium\\chromedriver.exe");
    d = new ChromeDriver();
    d.manage().window().maximize();
    d.get(URL);
    Utils.CaptureScreenshot(d, Screenshotdate + "_Login.png");
    d.findElement(By.id("loginUser")).sendKeys(UID);
    d.findElement(By.id("password")).sendKeys(PWD);
    d.findElement(By.id("loginButton")).click();
    Thread.sleep(5000);
    Utils.CaptureScreenshot(d, Screenshotdate + "_HomePage.png");
    test.log(Status.PASS, "Admin Logged in Successful");
    test.pass("Verified Admin logged in");
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;

public class TestData{
    public void excel() throws IOException {
     String filePath = System.getProperty("user.dir")+"\\Inputfiles";
     File file =    new File(filePath + "\\TestData.xlsx");
     FileInputStream inputStream = new FileInputStream(file);
     XSSFWorkbook wb=new XSSFWorkbook(inputStream);
     XSSFSheet sheet=wb.getSheet("Admin_inputs");
     XSSFRow row2=sheet.getRow(1);
     XSSFCell cell=row2.getCell(0);
     double UID= cell.getNumericCellValue();
     XSSFCell cell2 = row2.getCell(1);
     String PWD = cell2.getStringCellValue();
    }
wz3gfoph

wz3gfoph1#

你可以这样尝试:

public class TestData {
    double UID;
    String PWD;
    
    public void excel() throws IOException {
        String filePath = System.getProperty("user.dir") + "\\Inputfiles";
        File file = new File(filePath + "\\TestData.xlsx");
        FileInputStream inputStream = new FileInputStream(file);
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = wb.getSheet("Admin_inputs");
        XSSFRow row2 = sheet.getRow(1);
        XSSFCell cell = row2.getCell(0);
        UID = cell.getNumericCellValue();
        XSSFCell cell2 = row2.getCell(1);
        PWD = cell2.getStringCellValue();
    }
}

Admin_Login()方法中,我只是打印UIDPWD,根据您的要求更改代码:

public class AdminLoginAction extends TestData {
    @Test()
    public void Admin_Login() throws InterruptedException, IOException {
        // TestData excel = new TestData();  // you don't need to create this object because you are already inheriting `TestData` class.

        excel();  // before using UID and PWD you have to call excel() method.
        System.out.println(UID);
        System.out.println(PWD);
    }
}

相关问题