简体   繁体   中英

I can not passing variable to Main method (Cucumber)

I had tried to create method and call it from another file to the main class but It won't work the error message said "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();                 
    }

I had tried to do the same coding in the same file, It has no problem but when I move Method.class to the new file, error message said "java.lang.NullPointerException" but I can get "xpathvalue" value.

This Error occur because of it will not able to find your driver instance.

refer below code snippet. this is not cucumber example but you can get idea by this.

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

}

You need to pass your driver instance to another.

So I would suggest you take the webdriver wait out of your method and instantiate it when instantiating your webdriver. I would then create methods like so:

Driver class

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

Click method

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

    }
}

Test Class Import your driver class

import Base.Driver;

Then you would need declair your driver class like so:

Driver driver; 

Now you will have access to your method using

driver.buttonClickByXpath(//YourXpathHere)

The problem is "Method m1 = new Method(driver);" keyword, I had coded this line outside the main method. thank you very much, Sir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM