我无法将变量传递给Main方法(Cucumber)

问题描述 投票:1回答:3

我曾尝试创建方法并将其从另一个文件调用到主类但它将无法正常工作错误消息“java.lang.NullPointerException”

Main.class

Keywords kw = new Keywords();

@When("^gmailDD$") 
     public void gmailDD() throws Throwable{
     WebDriverWait wait5s = new WebDriverWait(driver, 5);
     String regis = "/html/body/div[2]/div[1]/div[5]/ul[1]/li[3]/a";
     String dd = "/html/body/div[1]/div/footer/div/div/div[1]";
     String empty = "/html/body/div[1]/div/footer";


     kw.clickbyxpath(regis);


     String handle= driver.getWindowHandle();
     System.out.println(handle);       
        // Store and Print the name of all the windows open               
        Set handles = driver.getWindowHandles();
        System.out.println("Log window id: "+handles);
        driver.switchTo().window("6442450949");

     kw.clickbyxpath(empty);   
     kw.clickbyxpath(dd);

}`

Method.class

WebDriver saddriver;

public void clickbyxpath (String xpathvalue) throws InterruptedException, IOException 
    {   
            WebDriverWait sad   =   new WebDriverWait(saddriver, 10); 

              //To wait for element visible
            System.out.println(xpathvalue);
            String x = xpathvalue;
            sad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x)));

            wowdriver.findElement(By.xpath(x)).click();                 
    }

我试图在同一个文件中进行相同的编码,没有问题但是当我将Method.class移动到新文件时,错误消息说“java.lang.NullPointerException”但我可以获得“xpathvalue”值。

java selenium selenium-webdriver methods parameter-passing
3个回答
1
投票

发生此错误是因为它无法找到您的驱动程序实例。

请参阅下面的代码段。这不是黄瓜的例子,但你可以通过这个获得想法。

Method.class

package testing.framework;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Method {

    public WebDriver driver;
    WebElement _clickForSearch;
    public Method(WebDriver driver) {
        this.driver = driver;
    }
    public Method clickByXpath(String xpathValues) {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        _clickForSearch = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpathValues)));
        _clickForSearch.click();    
        return this;
    }


}

Testing.class

package testing.framework;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Testing {
    public static WebDriver driver;

    public static void main(String[] args) {

        getWebDriver();
        String xpathValues= "//div[@class='FPdoLc VlcLAe']//input[@name='btnK']";
        Method m1 = new Method(driver);
        m1.clickByXpath(xpathValues);

    }

    public static void getWebDriver() {
        System.setProperty("webdriver.chrome.driver", "Your chrome driver path");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }

}

您需要将驱动程序实例传递给另一个。


1
投票

所以我建议你让webdriver等待你的方法,并在实例化你的webdriver时实例化它。然后我会创建这样的方法:

司机班

private final String USER_DIRECTORY = System.getProperty("user.dir");
private final int GLOBAL_TIMEOUT = 30;
private WebDriver webDriver;
private WebDriverWait webDriverWait;


public Driver(String browserName) {
    this.browserName = browserName;
    System.out.println(browserName);
    switch (this.browserName.toUpperCase()) {

        case "CHROME":
            initializeChromeDriver();
            break;
    }
}
private void initializeChromeDriver() {
    System.setProperty("webdriver.chrome.driver", USER_DIRECTORY.concat("\\drivers\\chromedriver.exe"));
    webDriver = new ChromeDriver();
    webDriver.manage().window().maximize();
    webDriverWait = new WebDriverWait(webDriver, GLOBAL_TIMEOUT);
}

单击方法

   public void buttonClickByXpath(String xpath) {
    try {
        WaitForPreseneOfElement(xpath);
        webDriver.findElement(By.xpath(xpath)).click();
    } catch (Exception e) {
        takeScreenshot();
        AllureLog("Failed to click on the button object. Please check your xpath. | xpath used = " + xpath + "");
        Assert.fail();

    }
}

测试类导入您的驱动程序类

import Base.Driver;

然后你需要声明你的驱动程序类,如下所示:

Driver driver; 

现在您可以使用访问方法了

driver.buttonClickByXpath(//YourXpathHere)

0
投票

问题是“方法m1 =新方法(驱动程序);”关键字,我在main方法之外编写了这一行。先生非常感谢您

© www.soinside.com 2019 - 2024. All rights reserved.