空指针异常:在运行不同功能文件和步骤定义文件中的测试时发生。

huangapple go评论75阅读模式
英文:

NullPointerException when running tests in different feature files and step definitions files

问题

我在一个特性文件和一个步骤定义文件中拥有所有测试时没有遇到这个问题然而我决定开始拆分测试创建了2个特性文件和2个步骤定义文件然而我遇到了NullPointerException错误

我正在使用一个全局变量类来初始化浏览器并设置驱动程序如下所示

    public class globalVariables {
    public WebDriver driver;
    public Properties prop;

    public WebDriver initializeDriver() throws IOException
    {
        prop= new Properties();

        String browserName= "chrome";
        String pathToDriver = "";

        if(browserName.equals("chrome"))
        {
            pathToDriver = "C://Repositories//webDrivers//chromedriver_win32_85.83//chromedriver.exe";
            System.setProperty("webdriver.chrome.driver", pathToDriver);
            driver= new ChromeDriver();
            driver.manage().window().maximize();
            //在chrome驱动中执行
        }
        
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
}

登录特性文件因为要打开浏览器等所以首先运行
      
     @SmokeTest
     @Login
     Feature: Queries SmokeTest
     Scenario Outline: 使用Chrome登录
    Given 我打开Chrome
    And 我浏览Queries
    When 我使用"<username>""<password>"登录Queries
    And 验证用户成功登录
    Then 关闭浏览器

    Examples:
    | username |  password |
    | user1|  pass1 |
    | user2|  pass2 |

登录特性文件的步骤定义

    public class login_steps extends globalVariables {
    @Given("我打开Chrome")
    public void iOpenChrome() throws Throwable {
        driver = initializeDriver();
       // throw new PendingException();
    }

    @When("我浏览Queries")
    public void iBrowseToQueries() {
        driver.get("https://qry.com/");
       // throw new PendingException();
    }

    @When("我使用{string}和{string}登录Queries")
    public void i_login_to_queries_using_and(String string, String string2) {
        driver.findElement(By.id("UserName")).sendKeys(string);
        driver.findElement(By.id("Password")).sendKeys(string2);
        driver.findElement(By.id("btnLogin")).click();
    }

    @Then("验证用户成功登录")
    public void verifyThatUserIsSuccessfullyLoggedIn() {
        WebElement HomeButton = driver.findElement(By.id("divHomeLink"));
        Assert.assertEquals(true, HomeButton.isDisplayed());
    }

    @And("关闭浏览器")
    public void closeBrowser() {
        driver.quit();
    }
}

搜索特性文件

     @SmokeTest
     @Search
    Feature: Queries SmokeTest
    Scenario: 搜索功能
    Given 我打开Chrome
    And 我浏览Queries
    Then 我使用"user1""pass1"登录Queries
    And 点击Queries搜索按钮
    And 输入关键词
    And 点击搜索
    Then 关闭浏览器

搜索步骤定义文件

    public class search_steps extends globalVariables {

    @And("点击Queries搜索按钮")
    public void clickOnQueriesSearchButton() {
        driver.findElement(By.id("imgSearchQueries")).click(); //错误发生在这里
    }

    @And("输入关键词")
    public void enterKeyword() {
        driver.findElement(By.id("txtKeywords")).sendKeys("Smoke Query");
    }

    @And("点击搜索")
    public void clickSearch() {
        driver.findElement(By.className("btn-search")).click();
    }
}
英文:

I did not have this issue when I had all the tests in 1 feature file and 1 step definition file. However I decided to start splitting the tests and created 2 feature files and 2 step definitions files. However, I am getting NullPointerException error.

I am using a global variables class to initialize browser and set driver, as per the below.

public class globalVariables {
public WebDriver driver;
public Properties prop;
public WebDriver initializeDriver() throws IOException
{
prop= new Properties();
String browserName= &quot;chrome&quot;;
String pathToDriver = &quot;&quot;;
if(browserName.equals(&quot;chrome&quot;))
{
pathToDriver = &quot;C://Repositories//webDrivers//chromedriver_win32_85.83//chromedriver.exe&quot;;
System.setProperty(&quot;webdriver.chrome.driver&quot;, pathToDriver);
driver= new ChromeDriver();
driver.manage().window().maximize();
//execute in chrome driver
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}

Login Feature File (which runs first, because of opening of browser etc..)

 @SmokeTest
@Login
Feature: Queries SmokeTest
Scenario Outline: Login using Chrome
Given I open Chrome
And I browse to Queries
When I login to Queries using &quot;&lt;username&gt;&quot; and &quot;&lt;password&gt;&quot;
And Verify that user is successfully logged in
Then Close Browser
Examples:
| username |  password |
| user1|  pass1 |
| user2|  pass2 |

Step definition for the Login Feature file

public class login_steps extends globalVariables {
@Given(&quot;I open Chrome&quot;)
public void iOpenChrome() throws Throwable {
driver = initializeDriver();
// throw new PendingException();
}
@When(&quot;I browse to Queries&quot;)
public void iBrowseToQueries() {
driver.get(&quot;https://qry.com/&quot;);
// throw new PendingException();
}
@When(&quot;I login to Queries using {string} and {string}&quot;)
public void i_login_to_queries_using_and(String string, String string2) {
driver.findElement(By.id(&quot;UserName&quot;)).sendKeys(string);
driver.findElement(By.id(&quot;Password&quot;)).sendKeys(string2);
driver.findElement(By.id(&quot;btnLogin&quot;)).click();
}
@Then(&quot;Verify that user is successfully logged in&quot;)
public void verifyThatUserIsSuccessfullyLoggedIn() {
WebElement HomeButton = driver.findElement(By.id(&quot;divHomeLink&quot;));
Assert.assertEquals(true, HomeButton.isDisplayed());
}
@And(&quot;Close Browser&quot;)
public void closeBrowser() {
driver.quit();
}

}

Search Feature file

 @SmokeTest
@Search
Feature: Queries SmokeTest
Scenario: Search Function
Given I open Chrome
And I browse to Queries
Then I login to Queries using &quot;user1&quot; and &quot;pass1&quot;
And Click on Queries Search button
And Enter keyword
And Click Search
Then Close Browser

Search step definition file

public class search_steps extends globalVariables {
@And(&quot;Click on Queries Search button&quot;)
public void clickOnQueriesSearchButton() {
driver.findElement(By.id(&quot;imgSearchQueries&quot;)).click(); //ERROR IS HERE
}
@And(&quot;Enter keyword&quot;)
public void enterKeyword() {
driver.findElement(By.id(&quot;txtKeywords&quot;)).sendKeys(&quot;Smoke Query&quot;);
}
@And(&quot;Click Search&quot;)
public void clickSearch() {
driver.findElement(By.className(&quot;btn-search&quot;)).click();
}
}

答案1

得分: 0

请确保在所有类中都使用相同的 Chrome Driver 实例,否则将会出现空指针异常。

请使用全局变量来存储驱动实例。

private static Webdriver driver;(在每个类的顶部实例化此变量)

英文:

You need to make sure you are using same chrome driver instance for all classes otherwise Null point exception will occur

Use global variable for driver instance

private static Webdriver driver;( Instatiate this on top of every classes)

huangapple
  • 本文由 发表于 2020年10月12日 18:17:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64315862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定