【问题标题】:unable to run scenario outline example of cucumber file无法运行黄瓜文件的场景大纲示例
【发布时间】:2018-07-21 09:30:32
【问题描述】:

我在学黄瓜。在尝试执行黄瓜场景大纲时,我遇到了错误。以下是黄瓜特征文件

Feature: to test pages titles    
Scenario Outline: to check title of the mutliple pages
Given Open the browser
When navigate to <Link> page
Then check <Title> of the page
Then close the browser

Examples: 
  | Link                     | Title                  |
  | https://cucumber.io/     | Cucumber               |
  | https://cucumber.io/docs | Documentation·Cucumber |
  | https://cucumber.io/blog | Blog·Cucumber          |

以下是黄瓜文件的步骤定义

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class scenario_outline_sd 
{
static WebDriver driver;
@Given("^Open the browser$")
public void open_the_browser() throws Throwable 
{
    System.setProperty("webdriver.chrome.driver", "E:\\selenium bwosers\\chrome 2.35\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
}

@When("^navigate to \"([^\"]*)\" page$")
public void navigate_to_page(String page) throws Throwable 
{
    driver.get(page);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}

@Then("^check \"([^\"]*)\" of the page$")
public void check_title_of_the_page(String title) throws Throwable 
{
    if(driver.getTitle().equalsIgnoreCase(title))
    {
        System.out.println("Verified title of : "+title);
    }
}

@Then("^close the browser$")
public void close_the_browser() throws Throwable 
{
    driver.close();
    driver.quit();
}

}

在运行 cucmber 功能文件时,它打开浏览器 3 次,但没有获取 URL 参数。请帮我解决这个问题。

【问题讨论】:

    标签: java selenium cucumber


    【解决方案1】:

    因为您在步骤定义中给出了错误正则表达式。

    特征步骤中的步骤参数没有双重配额:

    但是您在步骤定义中的正则表达式中使用了双重配额:

    删除\" 如下应该可以工作

    @When("^navigate to ([^\"]*) page$")
    public void navigate_to_page(String page) throws Throwable 
    {
        System.out.println(page);
        driver.get(page);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
    
    @Then("^check ([^\"]*) of the page$")
    public void check_title_of_the_page(String title) throws Throwable 
    {
        System.out.println(title);
        if(driver.getTitle().equalsIgnoreCase(title))
        {
            System.out.println("Verified title of : "+title);
        }
    }
    

    我可以使用以下练习代码在本地运行它:
    功能文件:

    步骤定义和运行结果:

    【讨论】:

    • hi yong,通过在 stepdefination 中删除 \" 它在相应的方法上显示 NullPointerException。现在它不接受 Link 参数。
    • 我猜你不明白我的意思,但是要删除 ([^\"]*) 里面的 \",我的意思是删除 \"([^\"]*)\" 的标题和尾部 \" ,请参阅我的更新代码在上面的答案中
    • 是的,我之前也尝试过同样的事情。通过更改您建议的代码,它会在 navigate_to_page 方法上显示 NullPointerException。
    • 我无话可说如何进一步帮助您,我什至编写了示例代码并在我的本地运行它,证明它们可以按预期工作。获取并打印出参数'Link'和'Title'。您的 NullPointException 与获取参数 'Link' 和 'Title' 无关,如果变量 driver 为空,这是您的问题,这是一个非常基本的问题,您应该注意并可以解决它。而不是等待别人为你找出答案。
    • 嘿 yong,谢谢你给我一个代码。现在通过删除 WebDriver driver = new ChromeDriver(); 中的“WebDriver”关键字,它在我身边工作正常;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多