1

我正在尝试用 selenium java 学习 Cucumber 。已经编写了两个场景,当我运行包含两个场景的功能文件时,只有场景 #1 正在执行,对于场景 #2,它的抛出Java null pointer exception

Feature: POC of my framework works

Scenario: Login test
Given  I navigate to the Bugzilla website
When  I click on login
And I enter the values
Then  I check to see if i was successfully loged in or not

Scenario: File a bug test
Given  I navigate to the File a bug page
When  I click on widgets
And I enter the bug details
Then  Bug should be submited succefully

我的步骤定义文件:

package cucumber.features;

import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinitions1 {

       protected WebDriver driver;
       protected String baseUrl;


       // Scenario 1
    @Given("^I navigate to the Bugzilla website$")
    public void I_navigate_to_the_Bugzilla_website() throws Throwable {

           driver = new FirefoxDriver();

            baseUrl ="https://landfill.bugzilla.org/bugzilla-4.4-branch/index.cgi";
             driver.get(baseUrl);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @When("^I click on login$")
    public void I_click_on_login() throws Throwable {

        driver.findElement(By.id("login_link_top")).click();

    }

    @And("^I enter the values$")
    public void I_enter_the_values() throws Throwable {
        driver.findElement(By.id("Bugzilla_login_top")).clear();
        driver.findElement(By.id("Bugzilla_login_top")).sendKeys("jeevan.anekal@gmail.com");
        driver.findElement(By.id("Bugzilla_password_top")).clear();
        driver.findElement(By.id("Bugzilla_password_top")).sendKeys("testuser@123");
        driver.findElement(By.id("log_in_top")).click();

    }

    @Then("^I check to see if i was successfully loged in or not$")
    public void I_check_to_see_if_i_was_successfully_loged_in_or_not() throws Throwable {
        System.out.println("Login Successfull");

    }


    // Scenario 2
    @Given("^I navigate to the File a bug page$")
    public  void I_navigate_to_the_File_a_bug_page() throws Throwable {

         driver.findElement(By.id("enter_bug")).click();

    }

    @When("^I click on widgets$")
    public void I_click_on_widgets() throws Throwable {

         driver.findElement(By.linkText("Widgets")).click();

    }

    @And("^I enter the bug details$")
    public void I_enter_the_bug_details() throws Throwable {
        new Select(driver.findElement(By.id("bug_severity"))).selectByVisibleText("trivial");
        new Select(driver.findElement(By.id("cf_drop_down"))).selectByVisibleText("---");
        new Select(driver.findElement(By.id("rep_platform"))).selectByVisibleText("Macintosh");
        new Select(driver.findElement(By.id("op_sys"))).selectByVisibleText("Mac OS X 10.0");
        driver.findElement(By.id("short_desc")).clear();
        driver.findElement(By.id("short_desc")).sendKeys("OS crashed");
        driver.findElement(By.id("comment")).clear();
        driver.findElement(By.id("comment")).sendKeys("Os debugging issue");

    }

    @Then("^Bug should be submited succefully$")
    public void Bug_should_be_submited_succefully() throws Throwable {
        System.out.println("Bug submitted successfully");

    }

}
4

2 回答 2

0

看起来您没有调用浏览器并导航到场景 2 中的页面

你需要做类似的事情

@Given("^I navigate to the File a bug page$")
    public  void I_navigate_to_the_File_a_bug_page() throws Throwable {
 driver = new FirefoxDriver();


            baseUrl ="https://landfill.bugzilla.org/bugzilla-4.4-nch/index.cgi";
             driver.get(baseUrl);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

         driver.findElement(By.id("enter_bug")).click();
    enter code here
    }

或者使用 @Before 钩子来初始化浏览器

于 2015-03-26T17:23:57.203 回答
0

我想我会在这里解决您的问题。我以前遇到过类似的问题。

因此,在运行功能文件时,可以通过单击栏中的运行按钮或右键单击功能文件然后运行...,查看实际单击时显示的内容。因为如果您将鼠标悬停在某个场景上并单击运行,它只会运行该场景,而不是运行超过 1 个场景的整个功能文件。

于 2020-05-14T02:48:34.137 回答