【问题标题】:I can not passing variable to Main method (Cucumber)我不能将变量传递给 Main 方法(Cucumber)
【发布时间】:2019-04-21 15:33:04
【问题描述】:

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

主类

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);

}`

方法类

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”值.

【问题讨论】:

  • 能否请您发布您的错误堆栈跟踪以获取更多详细信息。可能是找不到你的窗口。
  • “kw.clickbyxpath(regis);”处显示的错误消息在 Main.class 文件中,第二个错误显示在 Method.class 文件中的“WebDriverWait sad = new WebDriverWait(saddriver, 10);”中
  • 我已经阅读了这些错误日志,它是关于 WebDriver 和 WebDriverWait 的。
  • 发生这种情况是因为它找不到您的驱动程序实例。

标签: java selenium selenium-webdriver methods parameter-passing


【解决方案1】:

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

参考下面的代码sn-p。这不是黄瓜的例子,但你可以从中得到灵感。

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");
    }

}

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

【讨论】:

    【解决方案2】:

    所以我建议您将 webdriver wait 从您的方法中取出并在实例化您的 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)
    

    【讨论】:

      【解决方案3】:

      问题是“方法 m1 = new Method(driver);”关键词, 我在 main 方法之外对这一行进行了编码。 非常感谢您,先生

      【讨论】:

        猜你喜欢
        • 2012-08-21
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-09
        相关资源
        最近更新 更多