• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

我没办法变量传递把Main方法(黄瓜)

用户头像
it1352
帮助1

问题说明

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

正确答案

#1

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

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.谷歌.com");
    }

}

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

You need to pass your driver instance to another.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhaec
系列文章
更多 icon
同类精品
更多 icon
继续加载