我无法将变量传递给Main方法(黄瓜) [英] I can not passing variable to Main method (Cucumber)

查看:54
本文介绍了我无法将变量传递给Main方法(黄瓜)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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.

这篇关于我无法将变量传递给Main方法(黄瓜)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆