黄瓜在功能文件中给出具有多个场景的空指针异常



测试步骤

public class TestSmoke {
        WebDriver driver;
        @Given("^open firefox and start application$")jjj
        public void open_firefox_and_start_application() throws Throwable {
            driver=new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
            driver.get("https://example.com");
            
        }
    **Scenario 1**
        @When("^I click on Login$")
        public void I_click_on_Login() throws Throwable {
            driver.findElement(By.xpath("//a[contains(.,'Login')]")).click();
            
        }
    
        @When("^enter valid "([^"]*)" and valid "([^"]*)"$")
        public void enter_valid_and_valid(String uname, String pass) throws Throwable {
            driver.findElement(By.id("Username")).sendKeys(uname);
            driver.findElement(By.id("Password")).sendKeys(pass);
            
        }
    
        @Then("^Click on login and User should be able to login successfully$")
        public void Click_on_login_and_User_should_be_able_to_login_successfully() throws Throwable {
            driver.findElement(By.id("loginUser")).click();
            
        }

jkjbjkkjhjknjkbjkhjkbjbjbbjbnmbmbmb

        **Scenario 2:**
        @Given("^Click on shop for carts$")
        public void Click_on_shop_for_carts() throws Throwable {
            hhjbhbhgjbkjbhlhihjbhbb
            driver.findElement(By.xpath("//span[text()='Shop for Parts']")).click();
            
        }
    
        @Given("^select plates$")
        public void select_plates() throws Throwable {
            driver.findElement(By.xpath("//a[contains(.,'Plates ')]")).click();
            
        }
    
        
        @When("^I click on drsired product$")
        public void I_click_on_drsired_product() throws Throwable {
            driver.findElement(By.xpath("//a[@data-itemnumber='PLT01096096046']")).click();
        }
    
        @When("^click on item increment$")
        public void click_on_item_increment() throws Throwable {
            WebElement ele=driver.findElement(By.xpath("//i[contains(@class,'fa fa-caret-up')]"));
            for(int i=0;i<=3;i++)
            {
                ele.click();
            }
            
        }
    
        @When("^Click on buy now$")
        public void Click_on_buy_now() throws Throwable {
            driver.findElement(By.xpath("//button[contains(.,'Buy Now')]")).click();
            
        }
    
        @Then("^Product should be added to the cart successfully$")
        public void Product_should_be_added_to_the_cart_successfully() throws Throwable {
            
            
        }

功能文件功能:测试测试烟雾场景

 Scenario: Test login with valid credentials
    Given open firefox and start application
    When I click on Login
    And enter valid "s@yopmail.com" and valid "passw0rd"
    Then Click on login and User should be able to login successfully
    
    
    Scenario: Test shop for cart
    Given Click on shop for carts
    And select plates
    When I click on drsired product 
    And click on item increment 
    And Click on buy now
    Then Product should be added to the cart successfully
  

测试运行程序

@RunWith(Cucumber.class)
@Cucumber.Options(features="features",glue={"steps"})
public class TestRunnr { 

当我运行这个黄瓜脚本时,它会抛出一个空指针异常:

java.lang.NullPointerException
    at steps.testmoke.Click_on_shop_for_carts(testSmoke.java:47)
    at ?.Given Click on shop for carts(MyApplication.feature:11)

第一个方案成功执行,但第二个方案未执行。我登录电子商务网站并尝试点击购买零件。

每个方案都会创建所有步骤定义的新实例。在这种情况下,您可以在Given step public void open_firefox_and_start_application()中实例化驱动程序,以便第一个方案成功。现在,对于第二种情况,类的新实例具有一个 Web 驱动程序,该驱动程序为 null,并且您没有调用前面的步骤来实例化它。

您可以使用静态 Web 驱动程序,但会遇到并行测试的问题。如果您计划进行并行测试,请查找ThreadLocal以确保您的 Web 驱动程序已附加到特定线程。

另一种方法是将登录测试移动到单独的文件中。对于其他方案,将登录步骤移动到Background黄瓜标记中。这样,将为每个方案实例化 Web 驱动程序。但是,您需要决定是否要跨场景保持登录状态,删除cookie或为每个场景使用新浏览器。

最新更新