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

时间:2019-04-21 15:33:05

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

我曾尝试创建方法并将其从另一个文件调用到主类,但它无法正常工作,并显示错误消息“ 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”值。

3 个答案:

答案 0 :(得分: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 Xpath");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }

}

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

答案 1 :(得分: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;

然后,您需要像这样对驱动程序类进行declair:

Driver driver; 

现在您将可以使用

访问您的方法
driver.buttonClickByXpath(//YourXpathHere)

答案 2 :(得分:0)

问题是“方法m1 =新方法(驱动程序);”关键词, 我已经在main方法之外对此行进行了编码。 主席先生,非常感谢